glassfish 3.1 web应用程序的CAS身份验证。授权怎么样?

时间:2012-03-27 06:48:46

标签: authentication active-directory glassfish authorization cas

我正在尝试设置基于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教程中关注安全Web应用程序,我的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组映射到角色?

1 个答案:

答案 0 :(得分:0)

您可以使用安全注释(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。您也可以查看此{{3}}。如果遇到一些问题,请回答SO。