CAS接受服务器端返回值中文乱码问题解决方法

CAS接受服务器端返回值中文乱码问题解决方法

这几天在做CAS,客户端使用的是shiro-cas,cas服务器版本4.0.0,shiro-cas版本1.2.6。发现当客户端返回的用户信息中有中文时,会造成乱码。网上搜索都说的时加过滤器,编码什么的,然而并没什么用。通过跟踪代码,发现

造成乱码的原因是shiro-cas中的CasRealm获取CAS服务器返回字符串的时候没有编码

造成的,故有如下解决方法:

重写

AbstractUrlBasedTicketValidator AbstractCasProtocolUrlBasedTicketValidator Cas20ServiceTicketValidator

它们的代码什么的,直接重从他们源代码中复制过来,重点在于将这三个重写的类都增加一个构造方法,在原构造方法的基础上增加一个encoding参数,然后实现自己的CasRealm的时候,增加一个私有属性ticketValidator,重写其中的onInit()、ensureTicketValidator()、createTicketValidator()这三个方法。他们的具体方法都可以直接从CasRealm中拷贝过来,不过createTicketValidator()方法最后返回的时候需要修改为return new MyCas20ServiceTicketValidator(urlPrefix,”utf-8”);

具体代码如下:

MyAbstractUrlBasedTicketValidator.java,注意47到51行代码

public abstract class MyAbstractUrlBasedTicketValidator implements TicketValidator {
   
    /**
     * Commons Logging instance.
     */
    protected final Log log = LogFactory.getLog(getClass());

    /**
     * Hostname verifier used when making an SSL request to the CAS server.
     */
    protected HostnameVerifier hostnameVerifier;

    /**
     * Prefix for the CAS server.   Should be everything up to the url endpoint, including the /.
     * <p>
     * i.e. https://cas.rutgers.edu/
     */
    private final String casServerUrlPrefix;

    /**
     * Whether the request include a renew or not.
     */
    private boolean renew;

    /**
     * A map containing custom parameters to pass to the validation url.
     */
    private Map<String, String> customParameters;

    private String encoding;

    /**
     * Constructs a new TicketValidator with the casServerUrlPrefix.
     *
     * @param casServerUrlPrefix the location of the CAS server.
     */
    protected MyAbstractUrlBasedTicketValidator(final String casServerUrlPrefix) {
        this.casServerUrlPrefix = casServerUrlPrefix;
        CommonUtils.assertNotNull(this.casServerUrlPrefix, "casServerUrlPrefix cannot be null.");
    }

    /**
     * Constructs a new TicketValidator with the casServerUrlPrefix.
     *
     * @param casServerUrlPrefix the location of the CAS server.
     * @param encoding           the response encoding
     */
    protected MyAbstractUrlBasedTicketValidator(final String casServerUrlPrefix, final String encoding) {
        this.casServerUrlPrefix = casServerUrlPrefix;
        this.encoding = encoding;
        CommonUtils.assertNotNull(this.casServerUrlPrefix, "casServerUrlPrefix cannot be null.");
    }


    /**
     * Template method for ticket validators that need to provide additional parameters to the validation url.
     *
     * @param urlParameters the map containing the parameters.
     */
    protected void populateUrlAttributeMap(final Map<String, String> urlParameters) {
        // nothing to do
    }

    /**
     * The endpoint of the validation URL.  Should be relative (i.e. not start with a "/").  I.e. validate or serviceValidate.
     *
     * @return the endpoint of the validation URL.
     */
    protected abstract String getUrlSuffix();

    /**
     * Disable XML Schema validation.  Note, setting this to true may not be reversable. Defaults to false. Setting it to false
     * after setting it to true may not have any affect.
     *
     * @param disabled whether to disable or not.
     */
    protected abstract void setDisableXmlSchemaValidation(boolean disabled);

    /**
     * Constructs the URL to send the validation request to.
     *
     * @param ticket     the ticket to be validated.
     * @param serviceUrl the service identifier.
     * @return the fully constructed URL.
     */
    protected final String constructValidationUrl(final String ticket, final String serviceUrl) {
        final Map<String, String> urlParameters = new HashMap<String, String>();

        log.debug("Placing URL parameters in map.");
        urlParameters.put("ticket", ticket);
        urlParameters.put("service", serviceUrl);

        if (this.renew) {
            urlParameters.put("renew", "true");
        }

        log.debug(&#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值