`
schy_hqh
  • 浏览: 543076 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

SSO-CAS单点登录(二)

 
阅读更多

Ticket 有效期
Proxy代理认证
合理设置TGC超时时间, 默认是2个小时
ST service ticket
PT proxy ticket
TGC(Ticket Granting Cookie)存储在客户端浏览器中,是在CAS服务器上获取对应ST(Service Ticket)的凭据

=====================================================================
注销后跳转到指定页面
打开cas\cas-server-3.4.10\cas-server-webapp\src\main\webapp\WEB-INF\cas-servlet.xml


为logoutController设置属性:p:followServiceRedirects="true"

<bean id="logoutController" class="org.jasig.cas.web.LogoutController"
        p:centralAuthenticationService-ref="centralAuthenticationService"
        p:logoutView="casLogoutView"
        p:warnCookieGenerator-ref="warnCookieGenerator"
        p:ticketGrantingTicketCookieGenerator-ref="ticketGrantingTicketCookieGenerator"
        p:followServiceRedirects="true"/>

 
然后,在LogoutServlet中,为注销的链接拼接跳转的路径
【这里可以将serviceAddress写为CAS登录页面的路径,或其它页面的路径】

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" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">
        <constructor-arg index="0" 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 class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
    <property name="attributeRepository" ref="attributeRepository"/>
</bean>

 
3.修改InMemoryServiceRegistryDaoImpl中的RegisteredServiceImpl属性,增加<property name="ignoreAttributes" value="true" />
如,

<bean class="org.jasig.cas.services.RegisteredServiceImpl">
    <property name="id" 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"));
       
        }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics