我新建的web project是FirstWeb,在这个FirstWeb中修改web.xml文件:
 <security-constraint>
   <web-resource-collection>
   <web-resource-name>Protected Area</web-resource-name>
   <url-pattern>/*</url-pattern>
   </web-resource-collection>
   <auth-constraint>
   <role-name>q</role-name> 
   </auth-constraint>
  </security-constraint>
  
  <login-config>
   <auth-method>FORM</auth-method>
   <form-login-config>
   <form-login-page>/login.jsp</form-login-page>
   <form-error-page>/Error.html</form-error-page>
   </form-login-config>
  </login-config>
  
  <security-role>
   <role-name>q</role-name>
  </security-role>
然后在tomcat/conf/tomcat-users.xml修改为:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="t"/>
  <role rolename="q"/>
  <user username="123" password="123" roles="t"/>
  <user username="yyt" password="yyt" roles="q"/>
</tomcat-users>
在浏览器输入http://localhost:8080/FirstWeb/index.jsp。弹出一个表单输入用户名为“yyt”密码为“yyt”时能正确访问,但是输入用户名为“123”密码为“123”时出现HTTP Status 403 - Access to the requested resource has been denied。
在这个例子中不是只有角色为q的用户名yyt能登陆成功,而角色t下的用户名123不能登录,应该跳转到Error.html,却没有跳转而是出现HTTP Status 403 - Access to the requested resource has been denied。