java - 安全角色引用无法正常工作

标签 java jsp tomcat servlets web.xml

这些是我的 web.xml 的内容

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0" metadata-complete="true">
  <servlet>
    <security-role-ref>
      <role-name>MY_GROUP_NAME</role-name>
      <role-link>REGISTERED_USER</role-link>
    </security-role-ref>
  </servlet>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <resource-ref>
    <description>My datasource</description>
    <res-ref-name>jdbc/XXXXXXXX</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <security-constraint>
    <display-name>Example Security Constraint</display-name>
    <web-resource-collection>
      <web-resource-name>Protected Area</web-resource-name>
      <!-- Define the context-relative URL(s) to be protected -->
      <url-pattern>/protected/`*`</url-pattern>
      <!-- If you list http methods, only those methods are protected -->
      <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>
      <!-- Anyone with one of the listed roles may access this area -->
      <role-name>tomcat</role-name>
      <role-name>role1</role-name>
      <role-name>REGISTERED_USER</role-name>
    </auth-constraint>
  </security-constraint>
  <!-- Default login configuration uses form-based authentication -->
  <login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Example Form-Based Authentication Area</realm-name>
    <form-login-config>
      <form-login-page>/protected/login.jsp</form-login-page>
      <form-error-page>/protected/error.jsp</form-error-page>
    </form-login-config>
  </login-config>
  <!-- Security roles referenced by this web application -->
  <security-role>
    <role-name>role1</role-name>
  </security-role>
  <security-role>
    <role-name>tomcat</role-name>
  </security-role>
  <security-role>
    <role-name>REGISTERED_USER</role-name>
  </security-role>
</web-app>

当我使用 ldap 中 MY_GROUP_NAME 组中的有效用户执行登录时,request.getRemoteUser() 和 request.getUserPrincipal() 工作正常。针对 MY_GROUP_NAME 测试用户

String role = request.getParameter("role"); 
request.isUserInRole(role);

工作正常。

问题是针对角色 REGISTERED_USER 测试用户时不起作用。有人看到我在这里遗漏的东西吗?

一些附加信息

我使用 Apache Tomcat v7.0.22

LDAP 是 OpenDJ 2.4.5

Windows 7 操作系统

最佳答案

我没有使用 OpenDJ 的经验,但根据 https://wikis.forgerock.org/confluence/display/OPENDJ/Configure+Apache+Tomcat+with+OpenDJ+as+an+Identity+Store tomcat 角色和 LDAP 规则之间没有“映射”,因为映射是一对一的并且名称应该相同。 您将要使用的 LDAP 组应该被定义为 Tomcat 角色,并且您应该在 Web 描述符 (web.xml) 的安全角色部分使用它们的名称。

关于java - 安全角色引用无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981375/

相关文章:

java - JSP - 自定义 Taglib 的问题

Spring (数据、mvc、安全)依赖项 : IncompatibleClassChangeError

tomcat - 从 Ant 将应用程序重新加载到 Tomcat 中而不会遇到 Permgen 异常的正确方法是什么?

java - 为什么我的 Windows 突然遇到错误 'java.security.AccessControlException accessDeclaredMembers?

java - String 的子字符串并存储在 char 中

java - Swing 应用程序的 OnExit 事件?

java - 得到两个相同的陈述?

java - 从 Json 对象解析 Json 数组仅显示列表中的第一项 Java

java - 在一个 tomcat 上部署并运行两个应用程序

java - 我可以获得一个列表对象,但是如何在列表中显示值?