在tomcat-users.xml中定义了用户和角色:

<user username="admin" password="admin" roles="user,admin,APP_ADMIN"/>
  <role rolename="user"/>
  <role rolename="APP_ADMIN"/>
  <role rolename="admin"/>


应用程序安全性定义为:

<security-constraint>
        <web-resource-collection>
                <web-resource-name>Dynamic pages</web-resource-name>
                <url-pattern>*.jsp</url-pattern>
        </web-resource-collection>
        <auth-constraint>
                <description>These are the roles who have access.</description>
                <role-name>*</role-name>
        </auth-constraint>
        <user-data-constraint>
                <description></description>
                <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>




但是,当我以管理员身份登录到应用程序时,它总是使我得到未经授权的HTTP 403。
我用JSP scriplet检查了角色:

out.write(request.getUserPrincipal().toString());


它打印:


  用户username =“ admin”,角色=“ user,admin,APP_ADMIN”


但是当我检查isUserInRole时:

out.write(request.isUserInRole("APP_ADMIN") ? "Yep" : "nope");


得到:


  不


Tomcat版本是7.0.55

最佳答案

1:您可能必须在web.xml中定义角色。请参阅此问题Why do I list security roles in web.xml when they're in jdbcRealm database?

2:角色名称中的通配符'*'可能会引起麻烦。也许尝试使用角色名'user'看看是否可行。

对于通配符作为角色名称,必须启用allRolesMode


  此属性控制特殊角色名称*在以下情况下的处理方式
  在web.xml中处理授权约束。默认情况下,
  使用符合规范的严格值,这意味着
  必须为用户分配web.xml中定义的角色之一。的
  替代值是authOnly,这意味着用户必须
  已通过身份验证,但不检查分配的角色和
  strictAuthOnly,这意味着用户必须经过身份验证,并且没有
  除非在中定义了角色,否则将检查分配的角色
  web.xml,在这种情况下,必须为用户分配至少其中之一
  角色。


有关更多信息,请参见Tomcat文档:https://tomcat.apache.org/tomcat-7.0-doc/config/realm.html

10-07 19:52
查看更多