Liferay Portal 集成CAS单点登陆(SSO)完成对项目的访问控制 (六)

来源:互联网 发布:java语音聊天代码 编辑:程序博客网 时间:2024/06/03 03:44

Liferay Portal 集成CAS

我们之前已经用Liferay Portal嵌入了自己的web项目。但当我们单独访问自己web项目的时候是可以访问进去的,这不是我们想要的结果。在实际项目开发中应该是登陆后的用户拥有相应权限才可以看到访问的页面内容。我们接下来就用Liferay Portal集成CAS来解决这个问题。


  1. 准备工作
    • JDK的安装是必须的这里就不多说了(我用的版本为1.6)。
    • CAS-Server下载(我们用的版本为cas-server-3.5.0):下载地址。
    • CAS-Client下载(我们用的版本为cas-client-3.2.1):下载地址。
    • CAS讲解:点击这里。
  2. 部署CAS-Server
    1. 解压下载的cas-server-3.5.0.zip
    2. 在解压后的CAS中找到modules目录下的cas-server-webapp-3.5.0.war文件复制到tomcat下,我就直接用Liferay的tomcat。

    3. 启动后访问http://localhost:8080/cas/login,如下图(输入一样的用户名和密码即可登录成功!在实际开发中我们需要根据用户表中的信息进行登陆判断,请看接下来的CAS-Server配置。):

  3. CAS-Server配置
    1. 取消CAS服务器HTTPS认证方式:
      1. 修改 cas\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml 配置文件,将 p:cookieSecure="true” 改为 p:cookieSecure="false" ,改完后如下:
        修改ticketGrantingTicketCookieGenerator.xml 配置。
        <bean id="ticketGrantingTicketCookieGenerator"          class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"        p:cookieSecure="false"        p:cookieMaxAge="-1"        p:cookieName="CASTGC"        p:cookiePath="/cas" />       
      2. 修改 cas\WEB-INF\spring-configuration\warnCookieGenerator.xml 配置文件,将 p:cookieSecure="true” 改为 p:cookieSecure="false" ,改完后如下:
        修改warnCookieGenerator.xml 配置。
        <bean id="warnCookieGenerator"          class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"        p:cookieSecure="false"        p:cookieMaxAge="-1"        p:cookieName="CASPRIVACY"        p:cookiePath="/cas" />      
      3. 修改 deployerConfigContext.xml: 找到"HttpBasedServiceCredentialsAuthenticationHandler" bean 添加:p:requireSecure="false", 改完后如下:
        修改deployerConfigContext.xml:配置。
        <bean class="org.jasig.cas.authentication.handler.support.                    HttpBasedServiceCredentialsAuthenticationHandler"         p:httpClient-ref="httpClient"          p:requireSecure="false"/>     


    2. 通过配置让cas读取Liferay数据库中用户表进行登陆验证。
      1. 在cas\WEB-INF目录下打开deployerConfigContext.xml文件。配置数据源,数据源连接Liferay数据库。
        <!-- 数据源配置 -->      <bean id="dataSource"            class="org.springframework.jdbc.datasource.DriverManagerDataSource">            <property name="driverClassName"  value="com.mysql.jdbc.Driver" />            <property name="url" value="jdbc:mysql://localhost:3306/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false" />            <property name="username" value="root" />            <property name="password" value="1" />       </bean>  


      2. 修改登陆验证方式,CAS登陆要连接Liferay中的用户进行登陆判断。代码如下:
        <property name="authenticationHandlers">              <list>                  <!--                      | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating                      | a server side SSL certificate.                      +-->                  <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"                      p:httpClient-ref="httpClient" />                  <!--                      | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS                       | into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials                      | where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your                      | local authentication strategy.  You might accomplish this by coding a new such handler and declaring                      | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.                      +-->              <!-- <bean                       class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->                                    <!-- 用JDBC连接进行用户登录验证 -->                  <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" >                        <property name="sql" value="select password_ from user_ where screenName=?"/>                        <property name="dataSource" ref="dataSource"/>                        <property name="passwordEncoder" ref="mypasswordEncoder"/>                   </bean>              </list>          </property>      </bean>        <!-- 定义加密解密方式 -->      <bean id="mypasswordEncoder"           class="org.jasig.cas.util.MyPasswordEncoder">               <constructor-arg value="SHA"/>       </bean>  
      3. org.jasig.cas.util.MyPasswordEncoder这个类是我们自己写后编译成class文件放到该目录下的。代码:
        package org.jasig.cas.util;      import javax.validation.constraints.NotNull;  import org.jasig.cas.authentication.handler.PasswordEncoder;  import com.liferay.portal.kernel.util.DigesterUtil;    public class MyPasswordEncoder implements PasswordEncoder {        @NotNull      private final String algorithm;             public MyPasswordEncoder(final String algorithm) {           this.algorithm = algorithm;       }             public String encode(final String password) {          if(null == password){              return null;          }                    String pass = DigesterUtil.digest(algorithm,password);          return pass;       }          }  
      4. 注:DigesterUtil这个类是portal-service.jar中的类,作用是加密密码。(portal-service.jar的位置在liferay-portal-6.2-ce-ga2\tomcat-7.0.42\lib\ext\portal-service.jar中,如果你的CAS放到Liferay Portal下的tomcat中你就不用单独在将portal-service.jar拷贝到CAS的lib中了。)
        MyPasswordEncoder.class放置位置:
      5. <constructor-arg value="SHA"/>这是加密方式,必须和Liferay中的加密方式一样才能对比成功。Liferay加密方法配置在portal-ext.properties文件中添加如下代码即可:
        #  # 用户密码加密方式  #  passwords.encryption.algorithm=SHA  
      6. 在cas-server-3.5.0\modules目录下找到cas-server-support-jdbc-3.5.0.jar放到cas工程lib中。

4.CAS-Client配置:
    1. 下载并解压cas-client-3.2.1-release.zip
    2. 将cas-client-3.2.1\modules下的cas-client-core-3.2.1.jar拷贝到我们的web工程的lib中。
    3. 配置web.xml代码如下:
      <!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置          -->  <listener>        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  </listener>        <!-- 该过滤器用于实现单点登出功能,可选配置。 -->  <filter>                       <filter-name>CAS Single Sign Out Filter</filter-name>      <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  </filter>           <filter-mapping>                      <filter-name>CAS Single Sign Out Filter</filter-name>                      <url-pattern>/*</url-pattern>           </filter-mapping>               <!-- 该过滤器负责用户的认证工作,必须启用它          -->    <filter>                         <filter-name>CASFilter</filter-name>        <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>                        <init-param>                                    <param-name>casServerLoginUrl</param-name>                                    <param-value>http://localhost:8080/cas/login</param-value>                         <!--            这里的server是服务端的IP                  -->       </init-param>                        <init-param>                                    <param-name>serverName</param-name>                                    <param-value>http://localhost:8060</param-value>                        </init-param>             </filter>         <filter-mapping>                        <filter-name>CASFilter</filter-name>                        <url-pattern>/*</url-pattern>             </filter-mapping>     <!--     该过滤器负责对Ticket的校验工作,必须启用它   --><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>http://localhost:8080/cas</param-value>      </init-param>      <init-param>          <param-name>serverName</param-name>          <param-value>http://localhost:8060</param-value>      </init-param>  </filter>  <filter-mapping>      <filter-name>CAS Validation Filter</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>  

5.Liferay Portal中进行设置:
    1. CAS设置:在Liferay Portal中进入【控制面板】-【配置】-【Portal设置】-【认证】-【CAS】如图:

      测试CAS配置

    2. 设置访问权限(只有登陆过的人才可看到页面)。
      进入【站点页面】:先删除Welcome模块,只让它显示我们创建的测试模块。
      在分别设置【测试模块1】和【测试模块2】的【权限】,只有拥有相应权限的人才可看到页面。如图:



6.演示:




演示完毕!
0 0
原创粉丝点击