环境搭建:
apache-tomcat-7.0.55
jdk1.6.0_43
CAS Server版本:cas-server-3.4.10-release
CAS Client版本:cas-client-3.2.1
如果想在自己的本地环境搭建成功,就下载我上面的各软件版本,这个在我计算机上搭建成功了,我一开始版本没配套,出现了各种问题。搭建不成功。
步骤:
1.修改host文件
在 C:\Windows\System32\drivers\etc\hosts 增加下面的域名对应关系:
127.0.0.1 sso.kxl.com
127.0.0.1 app1.kxl.com
127.0.0.1 app2.kxl.com
sso.kxl.com的域名部署cas server的tomcat,和证书的生成,其他的两个分别部署两个webapp。
2.创建证书:
证书相当于单点登录(SSO)认证系统中的钥匙,保证客户端与服务器的交互的安全;在这里简单用jdk自带的keytool工具生成证书。
keytool -genkey -alias ssotest -keyalg RSA -keypass kxlpwd -keystore d:\keys\ssotest.keystore -storepass kxlpwd
keytool -export -alias ssotest -keystore D:\keys\ssotest.keystore -file d:\keys\ssotest.crt -storepass kxlpwd
keytool -import -keystore D:\java\jdk1.6.0_43\jre\lib\security\cacerts -file d:\keys\ssotest.crt -alias ssotest
注意:命令行中提示输入的新密码是:changeit
3.解压下载的tomcat,复制三份,用于模拟三台tomcat服务器上分别部署了cas-server,webapp1.webapp2的应用。
4.部署cas-server到tomcat_cas
将下载的cas解压缩后提取cas-server-3.4.11/modules/cas-server-webapp-3.4.11.war文件,复制到 D:\soft\apache-tomcat-7.0.55\tomcat_cas\webapps目下,并重命名为cas.war,tomcat启动的时候会解压缩这个war包部署到tomcat服务器
修改/tomcat-cas/conf/server.xml为:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="d:/keys/ssotest.keystore" keystorePass="kxlpwd"
clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8" />
我们启动tomcat-cas服务器的tomcat,在目录…\tomcat_cas\bin\startup.bat ,双击启动启动tomcat-cas, 验证https访问配置:
点击高级配置,然后点击箭头进入(我用的是谷歌浏览器,其他的浏览器可能不太一样):
在浏览器地址栏输入:https://sso.kxl.com:8443/cas/login ,回车
输入用户名密码admin/admin,登入进去:
5.部署webapp应用
就用tomcat默认自带的 …\webapps\examples 作为演示的简单web项目
1)修改tomcat-app1的启动端口,在文件 /conf/server.xml文件找到如下内容:
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改成如下:
<Server port="18005" shutdown="SHUTDOWN">
<Connector port="18080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="18443" />
<Connector port="18009" protocol="AJP/1.3" redirectPort="18443" />
2)启动tomcat-app1,浏览器输入 http://app1.kxl.com:18080/examples/servlets/ :
3)接下来复制 cas-client-3.2.1的lib包cas-client-core-3.2.1.jar到 tomcat-app1\webapps\examples\WEB-INF\lib\目录下, 在tomcat-app1\webapps\examples\WEB-INF\web.xml 文件中增加如下内容:
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListenerlistener-class>
listener>
<filter>
<filter-name>CAS Single Sign Out Filterfilter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>CAS Filterfilter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilterfilter-class>
<init-param>
<param-name>casServerLoginUrlparam-name>
<param-value>https://sso.kxl.com:8443/cas/loginparam-value>
init-param>
<init-param>
<param-name>serverNameparam-name>
<param-value>http://app1.kxl.com:18080param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CAS Filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>CAS Validation Filterfilter-name>
<filter-class>
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilterfilter-class>
<init-param>
<param-name>casServerUrlPrefixparam-name>
<param-value>https://sso.kxl.com:8443/casparam-value>
init-param>
<init-param>
<param-name>serverNameparam-name>
<param-value>http://app1.kxl.com:18080param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CAS Validation Filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>CAS HttpServletRequest Wrapper Filterfilter-name>
<filter-class>
org.jasig.cas.client.util.HttpServletRequestWrapperFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>CAS Assertion Thread Local Filterfilter-name>
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CAS Assertion Thread Local Filterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
6 、按上面的相同的方式部署webapp2,
conf/server中端口前面改成2**
webapps/examples/WEB-INF/下的web.xml中增加上面的filter,修改相关的url.
7、测试:
分别访问app1,app2
打开http://app1.kxl.com:18080/examples/servlets/servlet/HelloWorldExample
打开http://app1.kxl.com:18080/examples/servlets/servlet/HelloWorldExample
这个时候两个app都是未认证过的。都会跳转到cas server进行验证