CAS-Client客户端研究--AssertionThreadLocalFilter

配置在客户端web.xml中:
 <!--   该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。   
         比如AssertionHolder.getAssertion().getPrincipal().getName()
   -->  
<filter>   
    <filter-name>CAS Assertion Thread Local Filter</filter-name>    
    <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> 
</filter> 

<filter-mapping>   
    <filter-name>CAS Assertion Thread Local Filter</filter-name>   
    <url-pattern>/*</url-pattern> 
</filter-mapping>   

    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {  
            //转换参数  
            final HttpServletRequest request = (HttpServletRequest) servletRequest;  
            final HttpSession session = request.getSession(false);  
            //取得Assertion对象,如果session为null则从request中取得,否则从session中取得。  
            final Assertion assertion = (Assertion) (session == null ? request.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION) : session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION));  
      
            try {  
                //绑定到ThreadLocal  
                AssertionHolder.setAssertion(assertion);  
                // 处理后面的过滤器,这样后面的Fileter和Servlet可以在线程中取得Assertion对象。  
                filterChain.doFilter(servletRequest, servletResponse);  
            } finally {  
                //处理完毕后清理线程  
                AssertionHolder.clear();  
            }  
        }  
public class AssertionHolder {

    /**
     * ThreadLocal to hold the Assertion for Threads to access.
     */
    private static final ThreadLocal<Assertion> threadLocal = new ThreadLocal<Assertion>();


    /**
     * Retrieve the assertion from the ThreadLocal.
     *
     * @return the Asssertion associated with this thread.
     */
    public static Assertion getAssertion() {
        return threadLocal.get();
    }

    /**
     * Add the Assertion to the ThreadLocal.
     *
     * @param assertion the assertion to add.
     */
    public static void setAssertion(final Assertion assertion) {
        threadLocal.set(assertion);
    }

    /**
     * Clear the ThreadLocal.
     */
    public static void clear() {
        threadLocal.set(null);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值