CAS (14) —— CAS 更多用户信息

CAS (14) —— CAS 更多用户信息

摘要

将更多用户信息写入到service验证返回消息中


版本

tomcat版本: tomcat-8.0.29

jdk版本: jdk1.8.0_65

cas版本: 4.1.3

**cas4.1.3 (4.x还在开发过程中不是很稳定,迭代比较快,也会有些bug) **

cas-client-3.4.1

Ehcache版本: 2.10.1

内容

准备

参照下列文章配置好相关环境

配置

  • 重构attributeRepository

在deployerConfigContext.xml中移除


<!--

<util:map id="attrRepoBackingMap">




memberOf

faculty
staff
org


</util:map>
-->

新增attributeRepository.xml


















<util:map id="attrRepoBackingMap">




memberOf

faculty
staff
org


</util:map>

  • 修改Protocol 2.0的返回模板casServiceValidationSuccess.jsp

    <%@ page session="false" contentType="application/xml; charset=UTF-8" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
    cas:authenticationSuccess
    cas:user${fn:escapeXml(principal.id)}</cas:user>
    cas:protocal2.0</cas:protocal>
    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 test="${not empty pgtIou}">
    cas:proxyGrantingTicket${pgtIou}</cas:proxyGrantingTicket>
    </c:if>
    <c:if test="${fn:length(chainedAuthentications) > 0}">
    cas:proxies
    <c:forEach var="proxy" items="${chainedAuthentications}" varStatus="loopStatus" begin="0" end="${fn:length(chainedAuthentications)}" step="1">
    cas:proxy${fn:escapeXml(proxy.principal.id)}</cas:proxy>
    </c:forEach>
    </cas:proxies>
    </c:if>
    </cas:authenticationSuccess>
    </cas:serviceResponse>

客户端使用

<%
String name = null;
String department = null;
String job = null;
if (null != request.getUserPrincipal()) {
Map attributes = ((AttributePrincipal) request.getUserPrincipal()).getAttributes();
if( attributes == null ) {
out.println("No Attributes");
throw new ServletException("no attributes set by the CAS client");
}
name = (String) attributes .get("name");
department = (String) attributes .get("department");
job = (String) attributes .get("job");
} else {
out.println("No User Principal");
}
%>

请选择您要进入的模块


<%= (department == null ? "" : department) %> <%= (job == null ? "" : job) %> <%= (name == null ? request.getRemoteUser() : name) %>, 欢迎您!



<%--jsrender myTemplate--%>

问题

如果遇到返回中文名字为乱码,可以在CAS Validation Filter下添加encoding


CAS Validation Filter
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter

casServerUrlPrefix
https://nssotest.hoau.net/cas


serverName
https://authtest.hoau.net


redirectAfterValidation
true


useSession
true


acceptAnyProxy
true


encoding
UTF-8

测试

参考

参考来源:

CAS Protocol 3.0 Specification

CAS服务端自定义返回的用户信息

CAS返回中文乱码解决

结束

posted @ 2016-02-23 12:49  Richaaaard  阅读(1329)  评论(0)    收藏  举报