以命令方式换到目录%TOMCAT_HOME%,在command命令行输入如下命令:
Keytool -genkey -alias tomcat_key -keyalg RSA -storepass changeit -keystore server.keystore -validity 3600
用户名输入域名,如localhost(开发或测试用)或hostname.domainname(用户拥有的域名),其它全部以Enter跳过,最后确认,此时会在%Tomcat_home%下生成server.keystore文件。
注:参数-validity指证书的有效期(天),缺省有效期很短,只有90天。
这步对于Tomcat的SSL配置不是必须,但对于CAS SSO是必须的,否则会出现如下错误:
Edu.yale.its.tp.cas.client.CASAuthenticationExcepti
导入过程分2步,第一步是导出证书,第二步是导入到证书信任库,命令如下:
Keytool -export -trustcacerts -alias tomcat_key -file server.cer -keystore server.keystore -storepass changeit
Keytool -import -trustcacerts -alias
如果有提示,输入Y就可以了。
其它有用keytool命令(列出信任证书库中所有已有证书,删除库中某个证书):
keytool -list -keystore %JAVA_HOME%/jre/lib/security/cacerts >t.txt
keytool -delete -trustcacerts -alias tomcat_key -keystore %JAVA_HOME%/jre/lib/security/cacerts -storepass changeit
在Tomcat的server.xml配置文件中加入:
<Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
部署CAS Server的步骤如下:
1. 到CAS网站下载CAS Server。
2. 解压压缩文件,在解压后的文件夹内找到/modules/ cas-server-webapp-3.5.2.war,将cas-server-webapp-3.5.2.war复制到%Tomcat_Home%/webapps下并改名为cas.war.
3. 启动Tomcat,测试https://localhost:8443/cas/login看是否访问正常。
4. 修改配置文件deployerConfigContext.xml。(可选)
CAS Server默认登陆验证方式是SimpleTestUsernamePasswo
(1) 在deployerConfigContext.xml添加数据源dataSource和密码加密器passWordEncoder配置。
<!-- 数据库连接信息 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.MySQL.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://192.168.0.210:3306/cas_demo</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>root123</value></property>
</bean>
<!-- 密码加密器 -->
<bean id="md5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
<constructor-arg index="0"><value>MD5</value></constructor-arg>
</bean>
(2) 注释掉SimpleTestUsernamePasswo
<!-- CAS Server从数据库中获取用户信息进行认证 -->
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource"></property>
<property name="sql" value="select password from sys_user_info where username=?"></property>
<property name="passwordEncoder" ref="MD5PasswordEncoder"></property>
</bean>
(3) 添加cas-server-support-jdbc-3.5.2.jar、mysql-connector-java-5.1.25.jar、 commons-pool-1.3.jar、commons-dbcp-1.2.2.jar等jar包到WEB-INF/lib。
(4) 启动tomcat,测试https://localhost:8443/cas/login看是否访问正常。本文使用的CAS Client版本为3.3.3,下载地址:http://downloads.jasig.org/cas-clients/ 。
部署CAS Client的步骤如下:
1. 如果是Maven项目的话添加cas client的依赖:<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.3.3</version>
</dependency>
否则,下载cas-client-core-3.3.3.jar,并将其放入项目的WEB-INF/lib。
2. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CAS CLIENT DEMO</display-name>
<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
<listener>
<listener-class>
org.jasig.cas.client.session.SingleSignOutHttpSessionListener
</listener-class>
</listener>
<!-- 该过滤器用于实现单点登出功能,可选配置。 -->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</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>
<param-name>casServerLoginUrl</param-name>
<param-value>https://localhost:8443/cas/login</param-value>
</init-param>
<!-- 这里的serverName是服务端的ip -->
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:8080</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://localhost:8443/cas</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:8080</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器负责实现HttpServletRequest请求的包裹, 比如允许开发者通过
HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->
<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>
<!-- 该过滤器负责把ticket验证后产生的Assertion放入ThreadLocal中,以便 不能访问web层的资源使用。该过滤器可以使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
<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>
<!-- ======================== 单点登录结束 ======================== -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. 打包部署CAS Client。新闻热点
疑难解答