Java--Servlet,@ServletSecurity,role,username,password

Servlet API中,对servlet安全考虑,可以使用@ServletSecurity

但是@ServletSecurity中,value需要返回@HttpConstraint

而@HttpConstraint中,rolesAllowed需要返回String[]类型的roles。

那么问题来了返回的roles与登录的用户名,密码在哪里建立联系呢?

使用tomcat时,位于tomcat安装目录下~/conf/tomcat-users.xml可以配置role与username,password的映射关系

配置如下:

  <role rolename="tomcat"/>

  <role rolename="role1"/>

  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>

  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>

  <user username="role1" password="<must-be-changed>" roles="role1"/>

 

这里就创建了两个role(tomcat,role1)

并且有三个username,password被映射到了roles上

原文地址:https://www.cnblogs.com/eoss/p/6627806.html