SSO-CAS单点登录(二)
<bean id="logoutController" name="code">public void doSingleSignOut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String serviceAddress = "http://localhost"+":"+request.getLocalPort()+request.getContextPath(); response.sendRedirect("https://sso.gc.com:8443/cas/logout?service="+serviceAddress);}
?
也可以直接在页面上注销,不用写servlet
<a href="https://sso.gc.com:8443/cas/logout?service=xxx">注销</a>
?
由于https默认端口为443,可以将Tomcat中的8443端口修改为443,则CAS服务地址可以省略端口:http://sso.gc.com/cas/login
=====================================================================
需要解决的问题:
1.CAS服务停了仍然能够访问?
用户已经认证成功后,对应项目会创建对应的Session来保持已登录的状态,所以在认证成功后,CAS服务停了,也能访问本项目
访问项目A,到CAS中心认证成功,正常访问项目A中的资源
此时,关闭CAS服务
再访问项目A中的资源,仍然能够正常访问(项目A已经创建了与浏览器关联的session)
再访问另一个项目B,由于项目B还没有创建Session,因此会到CAS服务中心进行验证,但CAS服务已经关闭了,所以项目B将不能访问
单点登出的原理:由CAS回调各个子系统,让子系统清除给的sessionId的会话,从而实现登出功能
这也解释了上面的问题:
CAS服务停止后,由于没有去回调子系统清除session,才导致已认证成功的系统仍然能够访问
2.Ticket有效期与Session有效期的区别,相互之间是什么关系?
首先,两者之间没有关系
3.CAS代理怎么用?
\=====================================================================
CAS返回更多信息到客户端
修改cas\cas-server-3.4.10\cas-server-webapp\src\main\webapp\WEB-INF\deployerConfigContext.xml
1.配置attributeRepository,替换掉默认使用的StubPersonAttributeDao
?
<!-- return more information to client--> <bean id="attributeRepository" ref="dataSource"/> <constructor-arg index="1" value="select * from t_admin_user where {0}"/> <property name="queryAttributeMapping"> <map> <!-- key为登录CAS的用户名name属性,value为数据库字段 --> <entry key="username" value="login_name"/> </map> </property> <property name="resultAttributeMapping"> <map> <!-- key为数据库字段名,value为客户端从Map获取数据使用的key --> <entry key="email" value="email"/> <entry key="login_name" value="login_name"/> <entry key="name" value="name"/> <entry key="password" value="password"/> </map> </property> </bean>
?上面将生成SQL:
select * from t_admin_user where login_name = #username#
2.注入attributeRepository到UsernamePasswordCredentialsToPrincipalResolver中
<bean ref="attributeRepository"/></bean>
?
3.修改InMemoryServiceRegistryDaoImpl中的RegisteredServiceImpl属性,增加<property name="ignoreAttributes" value="true" />
如,
<bean value="0" /> <property name="name" value="HTTP" /> <property name="description" value="Only Allows HTTP Urls" /> <property name="serviceId" value="http://**" /> <!-- 返回resultAttributeMapping --> <property name="ignoreAttributes" value="true" /> </bean>
?
4.修改cas\cas-server-3.4.10\cas-server-webapp\src\main\webapp\WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp
增加如下内容
<!-- response more infomation to client --><c:if test="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes) > 0}"> <cas:attributes> <c:forEach var="attr" items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}"> <cas:${fn:escapeXml(attr.key)}>${fn:escapeXml(attr.value)}</cas:${fn:escapeXml(attr.key)}> </c:forEach> </cas:attributes></c:if>
?
基于以上配置,客户端可以获取到更多关于登录用户的信息
默认情况下CAS只返回username,而实际项目中需要很多关于登录用户的信息
?
AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();if (principal != null) { Map<String, Object> attributes = principal.getAttributes(); System.out.println(attributes.size()); if (attributes.size() > 0) { System.out.println(attributes.get("email")); System.out.println(attributes.get("login_name")); System.out.println(attributes.get("name")); System.out.println(attributes.get("password")); }}
?