使用ACEGI搭建权限系统:第三部分
三使用CAS实现单点登录
1.CAS SEVER3环境搭建
拷贝cas服务器到tomcat的部署目录下,可直接使用。
CAS默认的认证规则是用户名=密码即可登录,使用dbms认证需加入cas-server-support-jdbc.jar,ojdbc14
同时修改deployerConfigContext.xml中的authenticationHandlers配置:
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="select password from t_admin_user where login_name=?" />
<property name="passwordEncoder" ref="MD5PasswordEncoder"/>
</bean>
<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:///wsriademo</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>root</value></property>
</bean>
<bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
<constructor-arg index="0">
<value>MD5</value>
</constructor-arg>
</bean>
修改登录页面:
修改cas\WEB-INF\view\casLoginView.jsp页面,修改为我们想要的登录页面
2.配置证书,实现ssl登录环境
web服务器需要实现SSL,资源请求通过web server上的cas client使用proxyValidate和cas之间完成认证,这些都是需要配置x.509证书
//创建根证书,并采用自签名签署它
//创建私钥
openssl genrsa -out d:/apps/root/root-key.pem 1024
//创建证书请求
openssl req -new -out d:/apps/root/root-req.csr -key d:/apps/root/root-key.pem
//自签署根证书
openssl x509 -req -in d:/apps/root/root-req.csr -out d:/apps/root/root-cert.pem -signkey d:/apps/root/root-key.pem -days 3650
//将根证书导出成浏览器支持的.p12(PKCS12)格式
openssl pkcs12 -export -clcerts -in d:/apps/root/root-cert.pem -inkey d:/apps/root/root-key.pem -out d:/apps/root/root.p12
//打印证书
keytool -printcert -file d:/apps/root/root-cert.pem
//创建服务器证书,并采用根证书签署它
//创建私钥
openssl genrsa -out d:/apps/server/server-key.pem 1024
//创建证书请求
openssl req -new -out d:/apps/server/server-req.csr -key d:/apps/server/server-key.pem
//签署服务器证书
openssl x509 -req -in d:/apps/server/server-req.csr -out d:/apps/server/server-cert.pem -signkey d:/apps/server/server-key.pem -CA d:/apps/root/root-cert.pem -CAkey d:/apps/root/root-key.pem -CAcreateserial -days 3650
//将客户证书导出成浏览器支持的.p12(PKCS12)格式
openssl pkcs12 -export -clcerts -in d:/apps/server/server-cert.pem -inkey d:/apps/server/server-key.pem -out d:/apps/server/server.p12
//打印证书
keytool -printcert -file d:/apps/server/server-cert.pem
//创建客户证书,并采用根证书签署它
//创建私钥
openssl genrsa -out d:/apps/client/client-key.pem 1024
//创建证书请求
openssl req -new -out d:/apps/client/client-req.csr -key d:/apps/client/client-key.pem
//签署客户证书
openssl x509 -req -in d:/apps/client/client-req.csr -out d:/apps/client/client-cert.pem -signkey d:/apps/client/client-key.pem -CA d:/apps/root/root-cert.pem -CAkey d:/apps/root/root-key.pem -CAcreateserial -days 3650
//将客户证书导出成浏览器支持的.p12(PKCS12)格式
openssl pkcs12 -export -clcerts -in d:/apps/client/client-cert.pem -inkey d:/apps/client/client-key.pem -out d:/apps/client/client.p12
//打印证书
keytool -printcert -file d:/apps/client/client-cert.pem
....................................
//根据根证书生成相应的jks文件
keytool -import -v -trustcacerts -storepass password -alias root -file d:/apps/root/root-cert.pem -keystore d:/apps/root/root.jks
把root.jks,root.p12,server.p12导入到tomcat的conf目录中
配置tomcat ssl,修改conf/server.xml,tomcat6中多了SSLEnabled="true"
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxHttpHeaderSize="8192" scheme="https" secure="true"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" sslProtocol="TLS" clientAuth="false"
keystoreFile="D:/apache-tomcat-6.0.29/conf/server.p12"
keystoreType="PKCS12" keystorePass="password"
truststoreFile="D:/apache-tomcat-6.0.29/conf/root.jks"
truststoreType="JKS" truststorePass="password" />
IE中导入root.p12和client.p12,root.p12导入到受信任的根证书颁发机构
修改JDK,使JDK可以找到证书
设置tomcat启动参数
-Djavax.net.ssl.trustStore=D:/apache-tomcat-6.0.29/conf/root.jks -Djavax.net.ssl.trustStorePassword=password
方法2:
keytool -import -v -trustcacerts -storepass changeit -alias root -file d:/apps/root/root-cert.pem -keystore cacerts
keytool -v -list -alias root -keystore cacerts
方法3:
<property name="ticketValidator">
<bean
class="org.acegisecurity.providers.cas.ticketvalidator.CasProxyTicketValidator">
<!-- 导入认证证书,也可使用jdk工具导入 -->
<property name="trustStore" value="C:/Tomcat 5.5/conf/root.jks" />
</property>
3.使用acegi集成CAS
这个看上面的xml就可以了。里面有解释
casAuthenticationProvider
CasProxyTicketValidator,casValidatecasValidate,trustStore,serviceProperties,proxyCallbackUrl(代理时用)和表单认证有不同
注意
serviceProperties中service属性(CAS server登陆完毕后由CAS server重定向回来的页面),多系统的环境要写好回调地址
4.单点退出的处理
配置logoutFilter,退出后调用https://localhost:8443/cas/logout接口,注销服务器端ST
<bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter">
<!-- 注销后导向的页面 -->
<constructor-arg value="https://localhost:8443/cas/logout" />
<constructor-arg>
<list>
<bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler" />
</list>
</constructor-arg>
<!-- 注销的url -->
<property name="filterProcessesUrl" value="/j_logout.do" />
</bean>
界面中紧接刷新就可以讲浏览器中PT也清除了
function logout(){
parent.window.location.reload(true);
}