1.运行环境
jdk8
tomcat8.5
gradle-4.3.1
idea2.1
或者
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.4.1</version>
</dependency>
2.设置环境
2.1下载cas-4.2.7到自己想放的目录
2.2解压tomcat 到 D:\sso 复制3份
D:\sso\apache-tomcat-8-cas
D:\sso\apache-tomcat-8-c1
D:\sso\apache-tomcat-8-c2
2.3解压 gradle-4.3.1 到 D:\gradle-4.3.1
测试: 在cmd窗口中输入 gradle -v 输出版本信息。
2.5 证书配置
生成证书
keytool -genkey -alias cas -keyalg RSA -keysize 1024 -keypass a111111 -validity 365 -keystore d:\sso\cas.keystore -storepass a111111
名字和姓氏要填的和 上面你打算用的域名一样
导出证书
keytool -export -alias cas -keystore d:\sso\cas.keystore -file d:\sso\cas.crt
导入证书导入到jdk中
keytool -import -alias cas -file d:\sso\cas.crt -keystore D:\Java\jdk1.8.0_111\jre\lib\security\cacerts -storepass changeit
3 CAS服务端
3.1编译cas服务端
从CMD界面 进入cas web目录 输入gradle build
编译完成后 找到war包
3.2 修改服务端tomcat
把cas-server-webapp-4.2.7.war 修改名字,拷贝到 D:\sso\apache-tomcat-8-cas\webapps\cas.war
修改server.xml
找到<Connector port="8080" protocol="HTTP/1.1" -->
<!-- connectionTimeout="20000" -->
<!-- redirectPort="8443" />
修改端口号和证书配置
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="D:/sso/cas.keystore"
keystorePass="a111111" />
启动tomcat 访问
https://server.cas.com:8443/cas/login
账号:casuser
密码:Mellon
服务端可以访问了,要让客户端访问了
4 客户端
4.1 cas服务端修改
客户端一般都是http访问,不是https,服务端默认只支持https,所以先改服务端配置
修改此文件,增加http 访问支持
4.2 新建两个maven web 项目 client1 client2
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.4.1</version>
</dependency>
修改 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- ****************** 单点登录开始 ********************-->
<!-- 用于实现单点登出功能 可选 -->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<!-- 该过滤器用于实现单点登出功能,单点退出配置,一定要放在其他filter之前 可选 -->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>https://server.cas.com:8443/cas/</param-value>
</init-param>
</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>
<!--casServerLoginUrl:cas服务的登陆url -->
<param-name>casServerLoginUrl</param-name>
<param-value>https://server.cas.com:8443/cas/login</param-value>
</init-param>
<init-param>
<!--serverName:本项目的ip+port -->
<param-name>serverName</param-name>
<param-value>http://client1.cas.com:18080</param-value>
</init-param>
<init-param>
<param-name>useSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>redirectAfterValidation</param-name>
<param-value>true</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>https://server.cas.com:8443/cas/</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://client1.cas.com:18080</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<!-- 对项目中的哪些路径做登录拦截-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器对HttpServletRequest请求包装, 可通过HttpServletRequest的getRemoteUser()方法获得登录用户的登录名,可选 -->
<filter>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<filter-class>
org.jasig.cas.client.util.HttpServletRequestWrapperFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器使得可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。
比如AssertionHolder.getAssertion().getPrincipal().getName()。
这个类把Assertion信息放在ThreadLocal变量中,这样应用程序不在web层也能够获取到当前登录信息 -->
<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 Assertion Thread Local Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ****************** 单点登录结束 ********************-->
</web-app>
修改server.xml 端口号改一改 我用的
http://client1.cas.com:18080/client1
http://client2.cas.com:28080/client2
改完就能访问了
退出登录 : https://server.cas.com:8443/cas/logout
注遇到的难以解决的问题:
如果访问报错,上网搜了是证书问题 ,那很可能是 导入到jdk 里的证书没有生效,我是直接重启电脑,让他生效的。
参考博客
http://blog.csdn.net/u012554102/article/details/50274365
http://blog.csdn.net/zzq900503/article/details/54709801