HTTP 401 Unauthorized 状态码表示客户端请求的资源需要身份验证,但未提供有效的凭据或提供的凭据不正确。当您访问 http://localhost:8080/manager/html
时遇到401错误,说明Tomcat的管理界面需要认证。
Tomcat的用户认证信息配置在 conf/tomcat-users.xml
文件中。确保有以下内容:
<role rolename="manager-gui"/>
<user username="admin" password="password" roles="manager-gui"/>
访问时需要在浏览器中提供正确的用户名和密码,或者在URL中包含认证信息:
http://username:password@localhost:8080/manager/html
确保 webapps/manager/META-INF/context.xml
中的安全约束没有过于严格:
<Context antiResourceLocking="false" privileged="true">
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
</Context>
确保没有防火墙或网络设置阻止了本地连接。
查看 logs/catalina.out
文件获取更详细的错误信息。
401错误常见于:
// 使用fetch API处理401错误
fetch('http://localhost:8080/manager/html', {
headers: new Headers({
'Authorization': 'Basic ' + btoa('username:password')
})
})
.then(response => {
if (response.status === 401) {
throw new Error('Unauthorized: Invalid credentials');
}
return response.text();
})
.catch(error => {
console.error('Error:', error);
});
# Python requests处理401错误
import requests
from requests.auth import HTTPBasicAuth
try:
response = requests.get('http://localhost:8080/manager/html',
auth=HTTPBasicAuth('username', 'password'))
response.raise_for_status()
print(response.text)
except requests.exceptions.HTTPError as err:
if err.response.status_code == 401:
print("Unauthorized: Please check your credentials")
else:
print(f"HTTP error occurred: {err}")
没有搜到相关的文章