java 集成 cas系统 服务端和客户端配置

http://blog.csdn.net/yunye114105/article/details/7997041

参考:

http://blog.csdn.net/diyagea/article/details/50638737

(配置SSL协议)http://www.cnblogs.com/notDog/p/5264666.html

背景

有几个相对独立的java的web应用系统, 各自有自己的登陆验证功能,用户在使用不同的系统的时候,需要登陆不同的系统。现在需要提供一个统一的登陆/登出界面, 而不修改各个系统原来的登陆验证机制。于是采用单点登录系统CAS。

使用步骤

要使用单点登录,需要部署CAS系统, CAS服务端可以直接部署在tomcat下运行, 对于CAS服务端来说,所有要集成单点登录的web应用都是它的一个客户端, CAS有客户端jar包, 客户端web应用需要引入CAS客户端的jar包,这样CAS系统的服务端和客户端web应用程序端才能通信。

客户端web应用程序的通过配置web.xml,添加CAS需要的各种过滤器,来实现和CAS服务器通信, 用户信息验证工作在CAS服务端统一完成, 验证通过后,客户端web应用程序只需要补全自己的Session信息即可。

各个客户端web应用程序需要使用一个公用的用户表。

第一步 部署CAS系统服务端

1.从官网http://developer.jasig.org/cas/下载CAS Server, 将cas-server-webapp-3.4.12.war解压, 可以看到是一个标准的java的web应用, 可以直接部署到tomcat的webapps目录下的,我这里假设部署的路径是{tomcat_home}/webapps/cas。

2. CAS默认需要tomcat配置SSL协议,使用https协议通信的。 由于项目是企事业单位内部的系统,不需要这么高的安全级别, 可以简化操作,不走SSL协议。修改下配置文件\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml, 如下, 将默认的true改成false即可。

[html] view plain copy

  1. <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  2.         p:cookieSecure="false"  
  3.         p:cookieMaxAge="-1"  
  4.         p:cookieName="CASTGC"  
  5.         p:cookiePath="/cas" />  

3.配置登录的验证逻辑, 修改配置文件cas\WEB-INF\deployerConfigContext.xml。在authenticationHandlers中配置验证方式,我这里配置数据库查询语句来实现用户名和密码的验证。

[html] view plain copy

  1. <property name="authenticationHandlers">  
  2.             <list>  
  3.                 <!--  
  4.                     | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating  
  5.                     | a server side SSL certificate.  
  6.                     +-->  
  7.                 <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  8.                     p:httpClient-ref="httpClient" />  
  9.                 <!--  
  10.                     | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS   
  11.                     | into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials  
  12.                     | where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your  
  13.                     | local authentication strategy.  You might accomplish this by coding a new such handler and declaring  
  14.                     | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.  
  15.                     +-->  
  16.                  <bean class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler">
                    <property name="dataSource" ref="ds"/>

                    <property name="tableUsers" value="user"/>
                    <property name="fieldUser" value="username"/>
                    <property name="fieldPassword" value="password"/>
                    </bean>
  17.                 <!-- <bean  
  18.                     class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->  
  19.                 <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">  
  20.                     <property name="sql" value="select password from userTable where userName=?" />  
  21.                     <property name="passwordEncoder" ref="passwordEncoder"/>  
  22.                     <property name="dataSource" ref="dataSource" />  
  23.                 </bean>  -->

         

  

  1.             </list>  
  2.         </property>  

密码加密方法我这里使用MD5, 配置passwordEncoder的bean

[html] view plain copy

  1. <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">  
  2.         <constructor-arg value="MD5"/>  
  3.     </bean>  

在配置一个名称为dataSource的数据源

[html] view plain copy

  1. <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">  
  2.         <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>  
  3.         <property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=testDB;"></property>  
  4.         <property name="user" value="sa"></property>  
  5.         <property name="password" value="123456"></property>  
  6.         <property name="maximumConnectionCount" value="100"></property>  
  7.         <property name="minimumConnectionCount" value="1"></property>  
  8.     </bean>  

数据源的配置根据自己的实际情况来配置, 需要的jar如果lib下面没有,自己复制进去, 不然数据源连不上报错。

4. 现在服务端就配置好了, 如果需要定制登录/登出页面的话(实际项目基本上都需要修改), 修改cas\WEB-INF\view\jsp\default\ui\下面的casLoginView.jsp和casLogoutView.jsp就可以了

第二步 客户端web应用程序集成CAS

1. 从官网 http://developer.jasig.org/cas-clients/ 下载CAS Client, 将客户端的jar,如cas-client-core-3.2.1.jar引入到web应用程序的classpath中

2 .配置web.xml文件, 主要是添加过滤器拦截通信, 下面的实例代码, 假设web应用程序的端口是8080

[html] view plain copy

  1. <!-- CAS 单点登录(SSO) 过滤器配置 (start) -->  
  2.       
  3.     <!-- 该过滤器用于实现单点登出功能。-->  
  4.     <filter>  
  5.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  6.         <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  
  7.     </filter>  
  8.     <filter-mapping>  
  9.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  10.         <url-pattern>/*</url-pattern>  
  11.     </filter-mapping>  
  12.     <!-- CAS: 用于单点退出 -->  
  13.     <listener>  
  14.         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  
  15.     </listener>  
  16.       
  17.     <!-- 该过滤器负责用户的认证工作,必须启用它 -->  
  18.     <filter>  
  19.         <filter-name>CASFilter</filter-name>  
  20.         <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
  21.         <init-param>  
  22.             <param-name>casServerLoginUrl</param-name>  
  23.             <!-- 下面的URL是Cas服务器的登录地址 -->  
  24.             <param-value>http://CAS服务端所在服务器IP:8080/cas/login</param-value>  
  25.         </init-param>  
  26.         <init-param>  
  27.             <param-name>serverName</param-name>  
  28.             <!-- 下面的URL是具体某一个应用的访问地址 -->  
  29.             <param-value>http://具体web应用程序所在服务器IP:8080</param-value>  
  30.         </init-param>  
  31.     </filter>  
  32.     <filter-mapping>  
  33.         <filter-name>CASFilter</filter-name>  
  34.         <url-pattern>/*</url-pattern>  
  35.     </filter-mapping>  
  36.        
  37.     <!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->  
  38.     <filter>  
  39.         <filter-name>CAS Validation Filter</filter-name>  
  40.         <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  
  41.         <init-param>  
  42.             <param-name>casServerUrlPrefix</param-name>  
  43.             <!-- 下面的URL是Cas服务器的认证地址 -->  
  44.             <param-value>http://CAS服务端所在服务器IP:8080/cas</param-value>  
  45.         </init-param>  
  46.         <init-param>  
  47.             <param-name>serverName</param-name>  
  48.             <!-- 下面的URL是具体某一个应用的访问地址 -->  
  49.             <param-value>http://具体web应用程序所在服务器IP:8080</param-value>  
  50.         </init-param>  
  51.         <init-param>  
  52.           <param-name>renew</param-name>  
  53.           <param-value>false</param-value>  
  54.         </init-param>  
  55.         <init-param>  
  56.           <param-name>gateway</param-name>  
  57.           <param-value>false</param-value>  
  58.         </init-param>  
  59.     </filter>  
  60.     <filter-mapping>  
  61.         <filter-name>CAS Validation Filter</filter-name>  
  62.         <url-pattern>/*</url-pattern>  
  63.     </filter-mapping>  
  64.        
  65.     <!--  
  66.     该过滤器负责实现HttpServletRequest请求的包裹,  
  67.     比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。  
  68.     -->  
  69.     <filter>  
  70.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  71.         <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  
  72.     </filter>  
  73.     <filter-mapping>  
  74.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  75.         <url-pattern>/*</url-pattern>  
  76.     </filter-mapping>  
  77.        
  78.     <!--  
  79.     该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。  
  80.     比如AssertionHolder.getAssertion().getPrincipal().getName()。  
  81.     -->  
  82.     <filter>  
  83.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  84.         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
  85.     </filter>  
  86.     <filter-mapping>  
  87.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  88.         <url-pattern>/*</url-pattern>  
  89.     </filter-mapping>  
  90.        
  91.     <!-- 自动根据单点登录的结果设置本系统的用户信息(具体某一个应用实现) -->  
  92.     <filter>  
  93.         <filter-name>CasForInvokeContextFilter</filter-name>  
  94.         <filter-class>com.cm.demo.filter.CasForInvokeContextFilter</filter-class>  
  95.         <init-param>  
  96.           <param-name>appId</param-name>  
  97.           <param-value>a5ea611bbff7474a81753697a1714fb0</param-value>  
  98.         </init-param>  
  99.     </filter>  
  100.     <filter-mapping>  
  101.         <filter-name>CasForInvokeContextFilter</filter-name>  
  102.         <url-pattern>/*</url-pattern>  
  103.     </filter-mapping>  
  104.     <!-- CAS 单点登录(SSO) 过滤器配置 (end) -->  

4. 注意上步配置文件中,过滤器CasForInvokeContextFilter的实现是需要在具体的应用中实现的,他的目的是, CAS服务端登录验证成功后,会将登录用户的用户名携带回来, 这时客户端web应用程序需要根据用户名从数据库用户表中查询到用户的Id等信息, 并填充到Session中, 这样,客户端应用程序原来的验证逻辑就不会出问题了, 因为我们一般都是通过验证session中是否含有当前登录的用户的ID来进行登录验证的。

下面是CasForInvokeContextFilter的一个简单实现。

[java] view plain copy

  1. /** 
  2.  * 该过滤器用户从CAS认证服务器中获取登录用户用户名,并填充必要的Session. 
  3.  * @author jiarong_cheng 
  4.  * @created 2012-7-12 
  5.  */  
  6. public class CasForInvokeContextFilter implements Filter {  
  7.   
  8.     @Override  
  9.     public void destroy() {  
  10.     }  
  11.   
  12.     @Override  
  13.     public void doFilter(ServletRequest request, ServletResponse response,  
  14.             FilterChain chain) throws IOException, ServletException {  
  15.         HttpSession session = ((HttpServletRequest) request).getSession();  
  16.         //如果session中没有用户信息,则填充用户信息  
  17.         if (session.getAttribute("j_userId") == null) {  
  18.             //从Cas服务器获取登录账户的用户名  
  19.             Assertion assertion = AssertionHolder.getAssertion();  
  20.             String userName = assertion.getPrincipal().getName();  
  21.   
  22.             try {  
  23.                 //根据单点登录的账户的用户名,从数据库用户表查找用户信息, 填充到session中  
  24.                 User user = UserDao.getUserByName(userName);  
  25.                 session.setAttribute("username", userName);  
  26.                 session.setAttribute("userId", user.getId());  
  27.             } catch (Exception e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.         chain.doFilter(request, response);  
  32.     }  
  33.   
  34.     @Override  
  35.     public void init(FilterConfig config) throws ServletException {  
  36.     }  
  37. }  


到此,就完成了, 当你访问具体应用的网址, 如http://具体应用IP: 8080/ ,就会跳转到CAS服务器的登陆页面: http://CAS服务器IP: 8080/  进行登录验证, 验证通过后, 又会跳转回应用的网址。

第三步 单点登出

这个比较简单, 只要在系统的登出事件中, 将URL访问地址指向CAS服务登出的servlet, 就可以了。

http://CAS服务器IP:8080/cas/logout

猜你喜欢

转载自blog.csdn.net/xiaogg3678/article/details/83105805