1、路径/www/server/bt_tomcat_web/网站项目/conf/tomcat-users.xml
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<role rolename="user"/>
<user username="admin" password="admin" roles="user"/>
</tomcat-users>
角色user 用户名admin 密码admin
2、网站项目/WEB-INF/web.xml ,添加如下代码:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<security-constraint>
<web-resource-collection>
<web-resource-name>force login</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>/*</url-pattern>
<!-- If you list http methods, only those methods are protected -->
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>force login</realm-name>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
完美源码 » 宝塔Java项目访问密码