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就會彈出提示框須要輸入帳號密碼了