CAS实现中文的属性

wangjianme 13年前

casServiceValidationSuccess.jsp页面默认编码格式为ISO-8859-1,且在表单提交到客户端页面时,也使用IS0进行编码,为了处理中文,可以在页面上使用URLEncoder对需要传递的中文时行UTF-8编码,然后从客户端取得数据时,再做URLDecoder解码:

casServiceValidationSuccess.jsp页面,真是一个奇怪的页面,由于cas使用手工解析(没有使用任何dom解析,硬编码识别标标签的开始和标签的结束)xml的方式解析xml文件,所有,在修改此文件时,一定要加以注意:

以下是源代码

<%@ page session="false" contentType="text/xml; charset=UTF-8" import="java.net.URLEncoder"%>

<%@ 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(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.id)}</cas:user>

       <c:if test="${not empty pgtIou}">

           <cas:proxyGrantingTicket>${pgtIou}</cas:proxyGrantingTicket>

       </c:if>

       <c:if test="${fn:length(assertion.chainedAuthentications) > 1}">

           <cas:proxies>

              <c:forEach var="proxy" items="${assertion.chainedAuthentications}"

                  varStatus="loopStatus" begin="0"

                  end="${fn:length(assertion.chainedAuthentications)-2}"step="1">

                  <cas:proxy>${fn:escapeXml(proxy.principal.id)}</cas:proxy>

              </c:forEach>

           </cas:proxies>

       </c:if>

        <cas:attributes>

           <c:forEach items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}"var="attr">

                  <c:set var="val" value="${attr.value}"/>

                  <cas:${attr.key}><%=URLEncoder.encode((String)pageContext.getAttribute("val"),"UTF-8")%></cas:${attr.key}>

           </c:forEach>

       </cas:attributes>

    </cas:authenticationSuccess>

</cas:serviceResponse>

经过编码以后的XML数据如下:

<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>

    <cas:authenticationSuccess>

       <cas:user>U003</cas:user>

        <cas:attributes>

                  <cas:pwd>1111</cas:pwd>

                  <cas:username>%E5%BC%A0%E4%B8%89</cas:username>

       </cas:attributes>

    </cas:authenticationSuccess>

</cas:serviceResponse>

 

可见,对中文进行了UTF-8编码。

 

在客户端使用URLDecoder进行解码:

以下:

    <%

       Assertion assertion = AssertionHolder.getAssertion();

       AttributePrincipal ap =  assertion.getPrincipal();

       String id = ap.getName();

       Map<String,Object> att = ap.getAttributes();

       out.print("<br/>"+id);

       out.print("<br/>"+att);

       String name = URLDecoder.decode(""+att.get("username"), "UTF-8");

       out.println("<br/>"+name);

    %>