CAS單點登陸,中文用戶名無法驗證解決方案

          登陸名如果用中文的話,會出現亂碼,導致無法驗證通過。解決中文問題無非就是編碼問題,其中需要轉換編碼的有以下幾個地方.

          第一,在Web.xml文件里加上Filter,對所有頁面進行一次編碼轉換.

           1<filter>
           2        <description>EncodingFilter</description>
           3        <filter-name>EncodingFilter</filter-name>
           4        <filter-class>com.xyb.EncodingFilter</filter-class>
           5        <init-param>
           6            <description>encoding_charset</description>
           7            <param-name>encoding</param-name>
           8            <param-value>gb2312</param-value>
           9        </init-param>
          10        <init-param>
          11            <param-name>ignore</param-name>
          12            <param-value>true</param-value>
          13        </init-param>
          14</filter>
          15<filter-mapping>
          16        <filter-name>EncodingFilter</filter-name>
          17        <url-pattern>/*</url-pattern>
          18</filter-mapping>

          下面是EncodingFilter類:

           1package com.xyb;
           2
           3import java.io.IOException;
           4
           5import javax.servlet.Filter;
           6import javax.servlet.FilterChain;
           7import javax.servlet.FilterConfig;
           8import javax.servlet.ServletException;
           9import javax.servlet.ServletRequest;
          10import javax.servlet.ServletResponse;
          11
          12/**
          13*
          14@author XiangYabin
          15*/

          16public class EncodingFilter implements Filter {
          17    
          18    protected String encoding = null;
          19    protected FilterConfig filterConfig = null;
          20    protected boolean ignore = true;
          21    
          22    public void destroy() {
          23        
          24        this.encoding = null;
          25        this.filterConfig = null;
          26        
          27    }

          28    
          29    public void doFilter(ServletRequest request, ServletResponse response,
          30            FilterChain chain)
          31            throws IOException, ServletException {
          32// Conditionally select and set the character encoding to be used
          33        if (ignore || (request.getCharacterEncoding() == null)) {
          34            String encoding = selectEncoding(request);
          35            if (encoding != null)
          36                request.setCharacterEncoding(encoding);
          37        }

          38        
          39// Pass control on to the next filter
          40        chain.doFilter(request, response);
          41        
          42    }

          43    
          44    public void init(FilterConfig filterConfig) throws ServletException {
          45        
          46        this.filterConfig = filterConfig;
          47        this.encoding = filterConfig.getInitParameter("encoding");
          48        String value = filterConfig.getInitParameter("ignore");
          49        if (value == null)
          50            this.ignore = true;
          51        else if (value.equalsIgnoreCase("true"))
          52            this.ignore = true;
          53        else if (value.equalsIgnoreCase("yes"))
          54            this.ignore = true;
          55        else
          56            this.ignore = false;
          57        
          58    }

          59    
          60    protected String selectEncoding(ServletRequest request) {
          61        
          62        return (this.encoding);
          63        
          64    }

          65    
          66}

          第二步,修改CAS登陸驗證頁面的編碼,如果你沒換過這個主題的話,默認的是修改WEB-INF\view\jsp\default\ui\casLoginView.jsp文件,在最上面加上<%@ page pageEncoding="gb2312"%>。這里要特別注意一下,這個頁面的編碼要和上面XML文件里寫的編碼一致。

          第三步,修改驗證成功后做跳轉的頁面。也就是WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp文件,在最上面加上<%@ page pageEncoding="UTF-8"%>。這里特別注意的是這個頁面的編碼要和你跳轉過去的那個頁面的編碼一致,換句話說,客戶端的驗證頁面的編碼要和這個地方的一致,不一致的話,要轉換一下。

          經過這三步之后,理論上講就已經可以驗證通過了。



          眼鏡蛇

          posted on 2009-07-12 13:02 眼鏡蛇 閱讀(2878) 評論(1)  編輯  收藏 所屬分類: JavaLiferaySSO

          評論

          # re: CAS單點登陸,中文用戶名無法驗證解決方案[未登錄] 2011-07-21 14:27 nieyong

          感覺很羅嗦的。
          CAS默認是UTF-8編碼,可以不添加Filter,原CAS頁面也可以保持不變。
          唯一需要變化的是
          WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp需要和跳轉過去的那個頁面的編碼一致。
          添加:pageEncoding="UTF-8" 或 pageEncoding="GBK" 根據實際情況而定。
            回復  更多評論   

          <2011年7月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 九龙坡区| 社会| 南平市| 淳安县| 芜湖市| 无为县| 廊坊市| 资中县| 宁蒗| 肥城市| 叶城县| 章丘市| 蒲江县| 金乡县| 六盘水市| 大城县| 长乐市| 清丰县| 晴隆县| 百色市| 辰溪县| 大荔县| 云安县| 宜州市| 泰兴市| 张家港市| 阳原县| 太白县| 苍梧县| 昌图县| 巴青县| 安丘市| 昌江| 沙洋县| 盐池县| 铅山县| 德格县| 左云县| 武平县| 虞城县| 新巴尔虎左旗|