单点登录 - 自定义CAS客户端的过滤器AuthenticationFilter

CAS客户端的AuthenticationFilter是filter的实现,web容器可以根据它的 <url-pattern> 分发给相应的过滤器链进行处理。但是,这种粒度有时并不能满足我们需要排除拦截一些模块的需求,例如CAS的客户端AuthenticationFilter过滤器是提供登录认证拦截的,有时候某些URL不需要登录就可以被调用,下面我们将会自定义AuthenticationFilter,让它可能排除一些URL。

 

《单点登录 - 关于CAS客户端的拦截请求和忽略/排除不需要拦截的请求URL的问题》提供了另一种方式。

https://my.oschina.net/thinwonton/blog/1456732

 

自定义filter后,将提供excludePaths参数进行URL排除,该参数支持正则

<filter>  
    <filter-name>CASFilter</filter-name>  
    <filter-class>com.github.thinwonton.cas.filter.AuthenticationFilter</filter-class>  
    <init-param>  
        <param-name>casServerLoginUrl</param-name>  
        <param-value>http://localhost:8080/cas/login</param-value>  
    </init-param>  
    <init-param>  
        <param-name>serverName</param-name>  
        <param-value>http://localhost:8080</param-value>  
    </init-param>  
    <init-param>  
        <param-name>excludePaths</param-name>  
        <param-value>.*[/,\\]rest[/,\\].*</param-value>  
    </init-param>  
  </filter>  
  <filter-mapping>  
    <filter-name>CASFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>   

 

重点来了

public class AuthenticationFilter extends AbstractCasFilter{  
     /** 
     * The URL to the CAS Server login. 
     */  
    private String casServerLoginUrl;  
  
    /** 
     * Whether to send the renew request or not. 
     */  
    private boolean renew = false;  
  
    /** 
     * Whether to send the gateway request or not. 
     */  
    private boolean gateway = false;  
    /** 
     * 添加属性,这里用来存放不过滤地址正则表达式,可以根据自己需求定制---1 
     */  
    private String excludePaths;  
      
    private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl();  
  
    protected void initInternal(final FilterConfig filterConfig) throws ServletException {  
        if (!isIgnoreInitConfiguration()) {  
            super.initInternal(filterConfig);  
            setCasServerLoginUrl(getPropertyFromInitParams(filterConfig, "casServerLoginUrl", null));  
            log.trace("Loaded CasServerLoginUrl parameter: " + this.casServerLoginUrl);  
            setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false")));  
            log.trace("Loaded renew parameter: " + this.renew);  
            setGateway(parseBoolean(getPropertyFromInitParams(filterConfig, "gateway", "false")));  
            log.trace("Loaded gateway parameter: " + this.gateway);  
  
            final String gatewayStorageClass = getPropertyFromInitParams(filterConfig, "gatewayStorageClass", null);  
  
            if (gatewayStorageClass != null) {  
                try {  
                    this.gatewayStorage = (GatewayResolver) Class.forName(gatewayStorageClass).newInstance();  
                } catch (final Exception e) {  
                    log.error(e,e);  
                    throw new ServletException(e);  
                }  
            }  
            //自定义添加代码,用来读取web配置文件中excludes属性值 ---2  
            excludePaths = getPropertyFromInitParams(filterConfig, "excludePaths", null);//filterConfig.getInitParameter("excludePaths");  
            excludePaths = excludePaths.trim();  
        }  
    }  
  
    public void init() {  
        super.init();  
        CommonUtils.assertNotNull(this.casServerLoginUrl, "casServerLoginUrl cannot be null.");  
    }  
// url判断逻辑,这里大家可以根据自己需要来制订规则  
    private boolean isExclude(String uri){  
        boolean isInWhiteList = false;  
        if(excludePaths!=null&& uri!=null){  
            isInWhiteList = uri.matches(excludePaths);  
        }  
        return isInWhiteList;  
    }  
     
      
    public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {  
        final HttpServletRequest request = (HttpServletRequest) servletRequest;  
        final HttpServletResponse response = (HttpServletResponse) servletResponse;  
        final HttpSession session = request.getSession(false);  
        final Assertion assertion = session != null ? (Assertion) session.getAttribute(CONST_CAS_ASSERTION) : null;  
       // 该判断是自定义的对符合条件的url进行通过处理 ---3  
        if(isExclude(request.getRequestURI())){  
            filterChain.doFilter(request, response);  
            return;  
        }  
          
        if (assertion != null) {  
            filterChain.doFilter(request, response);  
            return;  
        }  
  
        final String serviceUrl = constructServiceUrl(request, response);  
        final String ticket = CommonUtils.safeGetParameter(request,getArtifactParameterName());  
        final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);  
  
        if (CommonUtils.isNotBlank(ticket) || wasGatewayed) {  
            filterChain.doFilter(request, response);  
            return;  
        }  
  
        final String modifiedServiceUrl;  
  
        log.debug("no ticket and no assertion found");  
        if (this.gateway) {  
            log.debug("setting gateway attribute in session");  
            modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl);  
        } else {  
            modifiedServiceUrl = serviceUrl;  
        }  
  
        if (log.isDebugEnabled()) {  
            log.debug("Constructed service url: " + modifiedServiceUrl);  
        }  
  
        final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);  
  
        if (log.isDebugEnabled()) {  
            log.debug("redirecting to \"" + urlToRedirectTo + "\"");  
        }  
  
        response.sendRedirect(urlToRedirectTo);  
    }  
  
    public final void setRenew(final boolean renew) {  
        this.renew = renew;  
    }  
  
    public final void setGateway(final boolean gateway) {  
        this.gateway = gateway;  
    }  
  
    public final void setCasServerLoginUrl(final String casServerLoginUrl) {  
        this.casServerLoginUrl = casServerLoginUrl;  
    }  
      
    public final void setGatewayStorage(final GatewayResolver gatewayStorage) {  
        this.gatewayStorage = gatewayStorage;  
    }  
      
}  

 

转载于:https://my.oschina.net/thinwonton/blog/1439112

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CAS(Central Authentication Service)是一种单点登录协议,用于实现在多个应用系统中进行统一的身份认证和授权。CAS客户端是指集成CAS协议的应用系统,用于与CAS服务器进行通信并实现单点登录功能。 以下是CAS客户端集成单点登录的代码示例: ```java // 引入CAS客户端依赖 <dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>3.6.0</version> </dependency> // 配置CAS客户端 <bean id="casClient" class="org.jasig.cas.client.authentication.CasClientAuthenticationFilter"> <property name="casServerLoginUrl" value="http://localhost:8081/cas/login" /> <property name="serverName" value="http://localhost:8080" /> </bean> // 配置CAS过滤器 <filter> <filter-name>CAS Authentication Filter</filter-name> <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> <init-param> <param-name>casServerLoginUrl</param-name> <param-value>http://localhost:8081/cas/login</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://localhost:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Authentication Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> // 配置CAS单点登出过滤器 <filter> <filter-name>CAS Logout Filter</filter-name> <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> </filter> <filter-mapping> <filter-name>CAS Logout Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> // 配置CAS请求单点登出过滤器 <filter> <filter-name>CAS Request Single Sign Out Filter</filter-name> <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> </filter> <filter-mapping> <filter-name>CAS Request Single Sign Out Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 以上代码是一个Java Web应用中集成CAS客户端的示例,通过配置CAS客户端和相关过滤器,实现与CAS服务器的通信和单点登录功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值