CAS介绍

  • 开源的企业级单点登录解决方案
  • CAS Server 是需要独立部署的 Web 应用
  • CAS Client 支持非常多的客户端(这里指单点登录系统中的各个 Web 应用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等
        

CAS服务部署

  • CAS服务端下载:http://downloads.jasig.org/cas/
  • 解压zip文件,改名为cas.war,然后复制到tomcat/webapp目录下
  • 浏览器访问:http://localhost:8080/cas


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

Tomcat配置HTTPS

  • 创建证书

       这里使用JDK生成的证书,正式环境需购买专业提供商的证书

       用JDK自带的keytool工具生成证书:

1 keytool -genkey -alias xiaokaceng -keyalg RSA -keystore d
2 :/cas/xiaokaceng
 

注意:127.0.0.1==sso.xiaokaceng.com 在C:\Windows\System32\drivers\etc\hosts配置,只能输入域名不能输入IP

  • 导出证书

1 keytool -export -file d:/cas/xiaokaceng.crt -alias xiaoka
2 ceng -keystore d:/cas/xiaokaceng

证书导出完成,可提供JDK使用

  • JVM导入证书

1 keytool -import -keystore D:\JavaDev\jdk1.7\jre\lib\secur
2 ity\cacerts -file d:\cas\xiaokaceng.crt -alias xiaokaceng

特别提示:

keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect

那么请输入密码:changeit

  • 应用到Web容器

       启用Tomcat的SSL,开启83和87的注释,配置keystoreFile和keystorePass

1 <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
2                maxThreads="150" scheme="https" secure="true"
3                clientAuth="false" sslProtocol="TLS" keystoreFile="D:/cas/xiaokaceng" keystorePass="123456"/>

注意:keystoreFile和keystorePass有大小写之分


CAS登录

  • 启动两个tomcat客户端,修改其端口(tomcat默认自带的 webapps\examples 作为演示的简单web项目)
  • 整合CAS-Client

      CAS-Client 下载地址:http://downloads.jasig.org/cas-clients/,解压并拷贝相应jar

  


修改web.xml

01 <!-- ======================== 单点登录开始 ======================== -->
02     <!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置-->
03     <listener>
04       <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
05     </listener>
06  
07     <!-- 该过滤器用于实现单点登出功能,可选配置。 -->
08     <filter>
09       <filter-name>CAS Single Sign Out Filter</filter-name>
10       <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
11     </filter>
12     <filter-mapping>
13       <filter-name>CAS Single Sign Out Filter</filter-name>
14       <url-pattern>/*</url-pattern>
15     </filter-mapping>
16  
17     <filter>
18       <filter-name>CAS Filter</filter-name>
19       <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
20       <init-param>
21         <param-name>casServerLoginUrl</param-name>
22         <param-value>https://sso.xiaokaceng.com:8443/cas/login</param-value>
23       </init-param>
24       <init-param>
25         <param-name>serverName</param-name>
26         <param-value>http://localhost:18080</param-value>
27       </init-param>
28     </filter>
29     <filter-mapping>
30       <filter-name>CAS Filter</filter-name>
31       <url-pattern>/*</url-pattern>
32     </filter-mapping>
33     <!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
34     <filter>
35       <filter-name>CAS Validation Filter</filter-name>
36       <filter-class>
37         org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
38       <init-param>
39         <param-name>casServerUrlPrefix</param-name>
40         <param-value>https://sso.xiaokaceng.com:8443/cas</param-value>
41       </init-param>
42       <init-param>
43         <param-name>serverName</param-name>
44         <param-value>http://localhost:18080</param-value>
45       </init-param>
46     </filter>
47     <filter-mapping>
48       <filter-name>CAS Validation Filter</filter-name>
49       <url-pattern>/*</url-pattern>
50     </filter-mapping>
51  
52     <!--
53       该过滤器负责实现HttpServletRequest请求的包裹,
54       比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。
55     -->
56     <filter>
57       <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
58       <filter-class>
59         org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
60     </filter>
61     <filter-mapping>
62       <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
63       <url-pattern>/*</url-pattern>
64     </filter-mapping>
65  
66     <!--
67     该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。
68     比如AssertionHolder.getAssertion().getPrincipal().getName()。
69     -->
70     <filter>
71       <filter-name>CAS Assertion Thread Local Filter</filter-name>
72       <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
73     </filter>
74     <filter-mapping>
75       <filter-name>CAS Assertion Thread Local Filter</filter-name>
76       <url-pattern>/*</url-pattern>
77     </filter-mapping>
78  
79     <!-- ======================== 单点登录结束 ======================== -->


测试验证,访问http://localhost:18080/examples/servlets/servlet/HelloWorldExample


CAS登出

退出链接为:https://sso.xiaokaceng.com:8443/cas/logout


CAS服务界面

  • 登录界面:casLoginView.jsp
  • 登录成功:casGenericSuccess.jsp
  • 登出界面:casLogoutView.jsp

本文转载:CSDN博客