【问题标题】:CAS authentication for a glassfish 3.1 web app. What about authorization?glassfish 3.1 Web 应用程序的 CAS 身份验证。授权呢?
【发布时间】:2012-03-27 06:48:45
【问题描述】:

我正在尝试建立一个基于 glassfish 3.1 + JSF2 的 Web 应用程序。授权是使用 CAS 服务器在 Web 应用程序中安装 jasig cas 客户端执行的,如下所示:

Configuring the JA-SIG CAS Client for Java in the web.xml

当用户通过身份验证时,我能够在 EJB 中捕获主体对象。 CAS 主体属性来自 Active Directory 上的 LDAP。现在如何添加授权? 如何仅允许 AD 中定义的特定用户组访问某些网页?

目的只是让用户根据他们的LDAP角色访问不同的网页。 我尝试按照 Java EE 教程中的 Securing Web Applications 进行操作,而我的 web.xml 是

<filter>
    <filter-name>CAS Authentication Filter</filter-name>
    <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
    <init-param>
        <param-name>casServerLoginUrl</param-name>
        <param-value>https://casserver:8443/cas/login</param-value>
    </init-param>
    <init-param>
        <param-name>serverName</param-name>
        <param-value>http://localhost:8080</param-value>
    </init-param>
</filter>
<filter>
    <filter-name>CAS Validation Filter</filter-name>
    <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
    <init-param>
        <param-name>casServerUrlPrefix</param-name>
        <param-value>https://casserver:8443/cas</param-value>
    </init-param>
    <init-param>
        <param-name>serverName</param-name>
        <param-value>http://localhost:8080</param-value>
    </init-param>
</filter>
<filter>
    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
    <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
<!-- 
<filter>
    <filter-name>CAS Assertion Thread Local Filter</filter-name>
    <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
 -->
<filter-mapping>
    <filter-name>CAS Authentication Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>CAS Validation Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<security-constraint>
    <display-name>Pagina di user</display-name>
    <web-resource-collection>
        <web-resource-name>index1</web-resource-name>
        <description>ristretto a user</description>
        <url-pattern>/faces/index.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description>user only</description>
        <role-name>AMP-User</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <display-name>Pagina di profile</display-name>
    <web-resource-collection>
        <web-resource-name>index2</web-resource-name>
        <description>risretto a profile</description>
        <url-pattern>/faces/index_2.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description>profile only</description>
        <role-name>AMP-Profile</role-name>
    </auth-constraint>
</security-constraint>
<security-role>
    <description>utente generico</description>
    <role-name>AMP-User</role-name>
</security-role>
<security-role>
    <description>Utente di alto profilo</description>
    <role-name>AMP-Profile</role-name>
</security-role> 

然后我在 glassifh-web.xml 中将角色分配给我的 LDAP 组

    <glassfish-web-app error-url="">
  <security-role-mapping>
    <role-name>AMP-Profile</role-name>
    <group-name>AMP-Profile</group-name>
  </security-role-mapping>
  <security-role-mapping>
    <role-name>AMP-User</role-name>
    <group-name>AMP-User</group-name>
  </security-role-mapping>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
</glassfish-web-app>

问题是当我访问页面 index.xhtml 时,一个表单要求我进行身份验证,但身份验证应该由 CAS 服务器完成。无论如何,我无法进行身份验证。 如何使用 CAS 身份验证并将 LDAP 组映射到角色?

【问题讨论】:

  • 那么您到底想使用 glassfish 进行身份验证或授权吗?还是 CAS 服务器(通过那些过滤器元素)应该阻止未经身份验证的请求?您确实要求 glassfish 服务器通过为它们指定安全约束来限制对这些索引页面的访问。由于您没有提供身份验证方法,我猜它默认为基本身份验证。或许您可以移除这些限制,让 CAS 服务器完成它的工作。
  • CAS 应该阻止未经身份验证的请求。此外,CAS 或 Glassfish 应该阻止经过身份验证的请求但具有不允许的角色。那可能吗?我删除了约束,但没有执行任何授权,因此任何 LDAP 组都可以在身份验证后访问。
  • 那么在移除安全约束时,使用 CAS 服务器的身份验证已经可以工作了吗?
  • 是的,身份验证有效。问题在于授权...

标签: authentication active-directory glassfish authorization cas


【解决方案1】:

您可以使用安全注释(JSR 250、javax.security.annotations)来定义基于角色的访问控制:

@Stateless
@DeclareRoles({"admin", "users"})
public class HelloEJB implements Hello {
    @PermitAll
    public String hello(String msg) {
        return "Hello, " + msg;
    }

    @RolesAllowed("admin")
    public String bye(String msg) {
         return "Bye, " + msg;
    }
}

稍微改编自下面第一个链接的示例

这里我首先声明了角色,使用@DeclareRoles@PermitAll 向所有经过身份验证的用户授予访问权限,而 @RolesAllowed 仅授予对上述角色的访问权限。

您还需要在部署描述符和 glassfish 中设置角色。我发现这个article 很有帮助。 Glassfish Server Security Guide 会详细介绍。您可能还想查看此question。如果您遇到一些问题,请在此处回复。

【讨论】:

  • 谢谢,但我认为保护特定人群的网页访问对于我的应用程序来说已经足够了。我尝试遵循安全文档,但它似乎与 CAS 身份验证冲突。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多