tomcat配置文件web.xml中</web-app>前加入如下内容web
<security-constraint> tomcat
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>My Test</web-resource-name>
<url-pattern>/test/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Test</realm-name>
</login-config>
而后在tomcat-users.xml中添加相关权限的帐号密码
<role rolename="role1"/>
<user username="role1" password="tomcat" roles="role1"/>
重启tomcat以后访问http://IP/test就会弹出提示框须要输入帐号密码了