基于CAS实现单点登录(SSO):实例讲解

本文目录:
  • 一、概述

  • 二、演示环境

  • 三、JDK安装配置

  • 四、安全证书配置

  • 五、部署CAS-Server相关的Tomcat

  • 六、部署CAS-Client相关的Tomcat

  • 七、 测试验证SSO

 

 

一、概述

此文的目的就是为了帮助初步接触SSO和CAS的人员提供一个入门指南,一步一步演示如何实现基于CAS的单点登录。

CAS的官网:http://www.jasig.org/cas

二、演示环境

本文演示过程在同一个机器上的(也可以在三台实体机器或者三个的虚拟机上),环境如下:

  • windows732

  • JDK 1.6.0_18

  • Tomcat 6.0.29

  • CAS-server-3.4.11、CAS-client-3.2.1

根据演示需求,用修改hosts 文件的方法添加域名,在文件 C:\Windows\System32\drivers\etc\hosts 文件中添加三条

127.0.0.1    demo.tch.com
127.0.0.1    app1.
tch.com
127.0.0.1    app2.
tch.com

  • demo.tch.com  =>>对应部署cas servertomcat,这个虚拟域名还用于证书生成

  • app1.tch.com  =>>  对应部署app1tomcat

  • app2.tch.com   =>>对应部署app2tomcat

 

三、JDK安装配置

因为casjava框架,需要在JVM中运行,所以需要配置安装和配置JDK。可参考娟姐博客

Win7下配置"JAVA环境变量"

 

 

四、安全证书配置

CAS默认使用的是HTTPS协议,如果对安全要求不高,可使用HTTP协议。
      
修改deployerConfigContext.xml  增加参数p:requireSecure="false",是否需要安全验证,即HTTPSfalse为不采用。
       <beanclass="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"p:httpClient-ref="httpClient" 
p:requireSecure="false" />

 

       修改ticketGrantingTicketCookieGenerator.xml(cas/WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml) ticketGrantingTicketCookieGenerator p:cookieSecure 属性 修改为 false 
       <beanid="ticketGrantingTicketCookieGenerator"class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false" p:cookieMaxAge="-1"p:cookieName="CASTGC" p:cookiePath="/cas" /> 


使用HTTPS协议,步骤如下:

4.1. 生成证书:

keytool-genkey -alias ssodemo -keyalg RSA -keysize 1024 -keypass 123456 -validity 365-keystore F:\Study\Java\Projects\SSO\Demo\keys\ssodemo.keystore -storepass123456

 

基于CAS实现单点登录(SSO):实例讲解_第1张图片

ps:

  • 截图中需要输入的姓名和上面hosts文件中配置的一致;

  • keypass 和 storepass 两个密码要一致,否则下面tomcat 配置https 访问失败;

 

4.2.导出证书:

keytool-export -alias ssodemo -keystoreF:\Study\Java\Projects\SSO\Demo\keys\ssodemo.keystore -fileF:\Study\Java\Projects\SSO\Demo\keys\ssodemo.crt -storepass 123456


 

4.3.客户端导入证书:

keytool-import -keystore E:\Environment\java\Java1.6\jre\lib\security\cacerts -fileF:\Study\Java\Projects\SSO\Demo\keys\ssodemo.crt -alias ssodemo

基于CAS实现单点登录(SSO):实例讲解_第2张图片

ps:该命令中输入的密码和上面输入的不是同一个密码;如果是多台机器演示,需要在每一台客户端导入该证书

有关keytool工具的详细运用见:http://www.micmiu.com/lang/java/keytool-start-guide/

 

 

五、部署CAS-Server相关的Tomcat

5.1.配置HTTPS

解压apache-tomcat-6.0.29.tar.gz并重命名后的路径为F:\Study\Java\Projects\SSO\Demo\tomcat-cas,在文件 conf/server.xml文件找到:

<!--

<Connectorport="8443" protocol="HTTP/1.1" SSLEnabled="true"

  maxThreads="150" scheme="https"secure="true"

  clientAuth="false" sslProtocol="TLS" />

 

-->

 

修改成如下:

<Connectorport="8443"protocol="org.apache.coyote.http11.Http11Protocol"SSLEnabled="true"

  maxThreads="150" scheme="https"secure="true"

  keystoreFile="F:/Study/Java/Projects/SSO/Demo/keys/ssodemo.keystore"keystorePass="123456"

  clientAuth="false" sslProtocol="TLS"URIEncoding="UTF-8"/>

参数说明:

  • keystoreFile 就是4.1中创建证书的路径

  • keystorePass 就是4.1中创建证书的密码

 

5.2.验证HTTPS配置

其他按照默认配置不作修改,双击%TOMCAT_HOME%\bin\startup.bat启动tomcat-cas 验证https访问配置:

基于CAS实现单点登录(SSO):实例讲解_第3张图片

 

基于CAS实现单点登录(SSO):实例讲解_第4张图片

如果看到上述界面表示https访问配置成功。

 

5.3部署CAS-Server

CAS-Server下载地址:http://www.jasig.org/cas/download

本文以cas-server-3.4.11-release.zip为例,解压提取cas-server-3.4.11/modules/cas-server-webapp-3.4.11.war文件,把改文件copy到 G:\sso\tomcat-cas\webapps\目下,并重命名为:cas.war.

启动tomcat-cas,在浏览器地址栏输入:https://demo.tch.com:8443/cas/login ,回车

基于CAS实现单点登录(SSO):实例讲解_第5张图片

CAS-server的默认验证规则:只要用户名和密码相同就认证通过(仅仅用于测试,生成环境需要根据实际情况修改),输入admin/admin 点击登录,就可以看到登录成功的页面:

基于CAS实现单点登录(SSO):实例讲解_第6张图片

看到上述页面表示CAS-Server已经部署成功。

 

六、部署CAS-Client相关的Tomcat

6.1Cas-Client下载

CAS-Client下载地址:http://downloads.jasig.org/cas-clients/

以cas-client-3.2.1-release.zip为例,解压提取cas-client-3.2.1/modules/cas-client-core-3.2.1.jar

借以tomcat默认自带的webapps\examples 作为演示的简单web项目

 

6.2 安装配置tomcat-app1

解压apache-tomcat-6.0.29.tar.gz并重命名后的路径为F:\Study\Java\Projects\SSO\Demo\tomcat-app1,修改tomcat的启动端口(共计5处),在文件conf/server.xml文件找到如下内容:

<Serverport="8005" shutdown="SHUTDOWN">

<Connectorport="8080" protocol="HTTP/1.1"

  connectionTimeout="20000"

  redirectPort="8443" />

<Connectorport="8009" protocol="AJP/1.3"redirectPort="8443" />

 

修改成如下:

<Serverport="18005" shutdown="SHUTDOWN">

<Connectorport="18080" protocol="HTTP/1.1"

  connectionTimeout="20000"

  redirectPort="18443" />

<Connectorport="18009" protocol="AJP/1.3"redirectPort="18443" />

 

 

启动tomcat-app1,浏览器输入http://app1.tch.com:18080/examples/servlets/回车:

基于CAS实现单点登录(SSO):实例讲解_第7张图片

看到上述界面表示tomcat-app1的基本安装配置已经成功。

接下来复制client的lib包cas-client-core-3.2.1.jarcommons-logging-1.1.jar到 tomcat-app1\webapps\examples\WEB-INF\lib\目录下,tomcat-app1\webapps\examples\WEB-INF\web.xml 文件中增加如下内容:

[html] view plaincopy在CODE上查看代码片

  1. <span style="font-size:18px;"><!-- ========================单点登录开始 ========================-->  

  2.                 <!--用于单点退出,该过滤器用于实现单点登出功能,可选配置-->  

  3.                 <listener>  

  4.                         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  

  5.                 </listener>  

  6. <!--该过滤器用于实现单点登出功能,可选配置。-->  

  7.                 <filter>  

  8.                         <filter-name>CASSingle Sign OutFilter</filter-name>  

  9.                         <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  

  10.                 </filter>  

  11.                 <filter-mapping>  

  12.                         <filter-name>CASSingle Sign Out Filter</filter-name>  

  13.                         <url-pattern>/*</url-pattern>  

  14.                 </filter-mapping>  

  15. <filter>  

  16.                         <filter-name>CASFilter</filter-name>  

  17.                         <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  

  18.                         <init-param>  

  19.                                 <param-name>casServerLoginUrl</param-name>  

  20.                                 <param-value>https://demo.micmiu.com:8443/cas/login</param-value>  

  21.                         </init-param>  

  22.                         <init-param>  

  23.                                 <param-name>serverName</param-name>  

  24.                                 <param-value>http://app1.micmiu.com:18080</param-value>  

  25.                         </init-param>  

  26.                 </filter>  

  27.                 <filter-mapping>  

  28.                         <filter-name>CASFilter</filter-name>  

  29.                         <url-pattern>/*</url-pattern>  

  30.                 </filter-mapping>  

  31.                 <!--该过滤器负责对Ticket的校验工作,必须启用它-->  

  32.                 <filter>  

  33.                         <filter-name>CASValidationFilter</filter-name>  

  34.                         <filter-class>  

  35.                                 org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  

  36.                         <init-param>  

  37.                                 <param-name>casServerUrlPrefix</param-name>  

  38.                                 <param-value>https://demo.micmiu.com:8443/cas</param-value>  

  39.                         </init-param>  

  40.                         <init-param>  

  41.                                 <param-name>serverName</param-name>  

  42.                                 <param-value>http://app1.micmiu.com:18080</param-value>  

  43.                         </init-param>  

  44.                 </filter>  

  45.                 <filter-mapping>  

  46.                         <filter-name>CASValidationFilter</filter-name>  

  47.                         <url-pattern>/*</url-pattern>  

  48.                 </filter-mapping>  

  49. <!--  

  50.                         该过滤器负责实现HttpServletRequest请求的包裹,  

  51.                         比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。  

  52.                 -->  

  53.                 <filter>  

  54.                         <filter-name>CASHttpServletRequest WrapperFilter</filter-name>  

  55.                         <filter-class>  

  56.                                 org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  

  57.                 </filter>  

  58.                 <filter-mapping>  

  59.                         <filter-name>CASHttpServletRequest WrapperFilter</filter-name>  

  60.                         <url-pattern>/*</url-pattern>  

  61.                 </filter-mapping>  

  62. <!--  

  63.                 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。  

  64.                 比如AssertionHolder.getAssertion().getPrincipal().getName()。  

  65.                 -->  

  66.                 <filter>  

  67.                         <filter-name>CASAssertion Thread LocalFilter</filter-name>  

  68.                         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  

  69.                 </filter>  

  70.                 <filter-mapping>  

  71.                         <filter-name>CASAssertion Thread LocalFilter</filter-name>  

  72.                         <url-pattern>/*</url-pattern>  

  73.                 </filter-mapping>  

  74. <!-- ========================单点登录结束 ======================== --></span>  

有关cas-client的web.xml修改的详细说明见官网介绍:

https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml

 

6.3 安装配置 tomcat-app2

解压apache-tomcat-6.0.29.tar.gz并重命名后的路径为F:\Study\Java\Projects\SSO\Demo\tomcat-app2,修改tomcat的启动端口,在文件conf/server.xml文件找到如下内容:

<Serverport="8005" shutdown="SHUTDOWN">

<Connectorport="8080" protocol="HTTP/1.1"

  connectionTimeout="20000"

  redirectPort="8443" />

<Connectorport="8009" protocol="AJP/1.3"redirectPort="8443" />

 

修改成如下:

<Server port="28005" shutdown="SHUTDOWN">

<Connector port="28080" protocol="HTTP/1.1"

  connectionTimeout="20000"

  redirectPort="28443"/>

<Connector port="28009" protocol="AJP/1.3" redirectPort="28443" />

启动tomcat-app2,浏览器输入http://app2.tch.com:28080/examples/servlets/回车,按照上述6.2中的方法验证是否成功。

同6.2中的复制client的lib包cas-client-core-3.2.1.jarcommons-logging-1.1.jar到tomcat-app2\webapps\examples\WEB-INF\lib\目录下,tomcat-app2\webapps\examples\WEB-INF\web.xml 文件中增加如下内容:

[html] view plaincopy在CODE上查看代码片

  1. <span style="font-size:18px;"><!-- ========================单点登录开始 ========================-->  

  2.                 <!--用于单点退出,该过滤器用于实现单点登出功能,可选配置-->  

  3.                 <listener>  

  4.                         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  

  5.                 </listener>  

  6. <!--该过滤器用于实现单点登出功能,可选配置。-->  

  7.                 <filter>  

  8.                         <filter-name>CASSingle Sign OutFilter</filter-name>  

  9.                         <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  

  10.                 </filter>  

  11.                 <filter-mapping>  

  12.                         <filter-name>CASSingle Sign Out Filter</filter-name>  

  13.                         <url-pattern>/*</url-pattern>  

  14.                 </filter-mapping>  

  15. <filter>  

  16.                         <filter-name>CASFilter</filter-name>  

  17.                         <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  

  18.                         <init-param>  

  19.                                 <param-name>casServerLoginUrl</param-name>  

  20.                                 <param-value>https://demo.micmiu.com:8443/cas/login</param-value>  

  21.                         </init-param>  

  22.                         <init-param>  

  23.                                 <param-name>serverName</param-name>  

  24.                                 <param-value>http://app2.micmiu.com:28080</param-value>  

  25.                         </init-param>  

  26.                 </filter>  

  27.                 <filter-mapping>  

  28.                         <filter-name>CASFilter</filter-name>  

  29.                         <url-pattern>/*</url-pattern>  

  30.                 </filter-mapping>  

  31.                 <!--该过滤器负责对Ticket的校验工作,必须启用它-->  

  32.                 <filter>  

  33.                         <filter-name>CASValidationFilter</filter-name>  

  34.                         <filter-class>  

  35.                                 org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  

  36.                         <init-param>  

  37.                                 <param-name>casServerUrlPrefix</param-name>  

  38.                                 <param-value>https://demo.micmiu.com:8443/cas</param-value>  

  39.                         </init-param>  

  40.                         <init-param>  

  41.                                 <param-name>serverName</param-name>  

  42.                                 <param-value>http://app2.micmiu.com:28080</param-value>  

  43.                         </init-param>  

  44.                 </filter>  

  45.                 <filter-mapping>  

  46.                         <filter-name>CASValidationFilter</filter-name>  

  47.                         <url-pattern>/*</url-pattern>  

  48.                 </filter-mapping>  

  49. <!--  

  50.                         该过滤器负责实现HttpServletRequest请求的包裹,  

  51.                         比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。  

  52.                 -->  

  53.                 <filter>  

  54.                         <filter-name>CASHttpServletRequest WrapperFilter</filter-name>  

  55.                         <filter-class>  

  56.                                 org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  

  57.                 </filter>  

  58.                 <filter-mapping>  

  59.                         <filter-name>CASHttpServletRequest WrapperFilter</filter-name>  

  60.                         <url-pattern>/*</url-pattern>  

  61.                 </filter-mapping>  

  62. <!--  

  63.                         该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。  

  64.                         比如AssertionHolder.getAssertion().getPrincipal().getName()。  

  65.                 -->  

  66.                 <filter>  

  67.                         <filter-name>CASAssertion Thread LocalFilter</filter-name>  

  68.                         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  

  69.                 </filter>  

  70.                 <filter-mapping>  

  71.                         <filter-name>CASAssertion Thread LocalFilter</filter-name>  

  72.                         <url-pattern>/*</url-pattern>  

  73.                 </filter-mapping>  

  74. <!-- ========================单点登录结束 ======================== --></span>  

 

七、 测试验证SSO

启动之前配置好的三个tomcat分别为:tomcat-cas、tomcat-app1、tomcat-app2.

7.1 基本的测试

预期流程:打开app1 url —->跳转cas server验证 —->显示app1的应用 —->打开app2 url —->显示app2应用 —->注销cas server —->打开app1/app2url —->重新跳转到cas server验证.

打开浏览器地址栏中输入:http://app1.tch.com:18080/examples/servlets/servlet/HelloWorldExample

基于CAS实现单点登录(SSO):实例讲解_第8张图片

验证通过后显示如下:

基于CAS实现单点登录(SSO):实例讲解_第9张图片

 

此时访问app2就不再需要验证:

基于CAS实现单点登录(SSO):实例讲解_第10张图片

 

地址栏中输入:https://demo.tch.com:8443/cas/logout,回车显示

基于CAS实现单点登录(SSO):实例讲解_第11张图片

上述表示认证注销成功,此时如果再访问 :http://app1.micmiu.com:18080/examples/servlets/servlet/HelloWorldExample或http://app2.micmiu.com:28080/examples/servlets/servlet/HelloWorldExample需要重新进行认证。

 

这里是源码下载http://download.csdn.net/detail/tcl_6666/6961265,欢迎互相交流,共同进步。


你可能感兴趣的:(基于CAS实现单点登录(SSO):实例讲解)