`
wangyangqq2008
  • 浏览: 34101 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

CAS实现过滤掉某些URL不走单点登录

    博客分类:
  • CAS
阅读更多
Cas filter client端没有自带过滤掉某些url不进行单点登录的init-param ,需要实现自定义的Filter 取代org.jasig.cas.client.authentication.AuthenticationFilter和org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
1.实现CASAuthenticationFilter extends AbstractCasFilter
因为cas自带的filter中org.jasig.cas.client.authentication.AuthenticationFilter 的doFilter方法是final的,无法重写,所以需要直接继承AbstractCasFilter
实现代码如下
[public class CASAuthenticationFilter extends AbstractCasFilter {
    private final String ExcludeFile = "ExcludeFile";  //excludeFile 列表
    private String casServerLoginUrl;
    private boolean renew;
    private boolean gateway;
    private GatewayResolver gatewayStorage;
    private String strExcludeFile;
    private String[] arrExcludeFile = null;
    public CASAuthenticationFilter(){
        this.renew = false;

        this.gateway = false;

        this.gatewayStorage = new DefaultGatewayResolverImpl(); 
        setStrExcludeFile("");
    }
    
    protected void initInternal(FilterConfig filterConfig) throws ServletException {
        if (!(isIgnoreInitConfiguration())) {
            super.initInternal(filterConfig);
            setCasServerLoginUrl(getPropertyFromInitParams(filterConfig, "casServerLoginUrl", null));
            this.log.trace("Loaded CasServerLoginUrl parameter: " + this.casServerLoginUrl);
            setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false")));
            this.log.trace("Loaded renew parameter: " + this.renew);
            setGateway(parseBoolean(getPropertyFromInitParams(filterConfig, "gateway", "false")));
            this.log.trace("Loaded gateway parameter: " + this.gateway);
            
            setStrExcludeFile(getPropertyFromInitParams(filterConfig, ExcludeFile, ""));
            this.log.trace("Loaded ExcludeFile parameter: " + this.strExcludeFile);
            
            String gatewayStorageClass = getPropertyFromInitParams(filterConfig, "gatewayStorageClass", null);

            if (gatewayStorageClass == null) return;
            try {
              this.gatewayStorage = ((GatewayResolver)Class.forName(gatewayStorageClass).newInstance());
            } catch (Exception e) {
              this.log.error(e, e);
              throw new ServletException(e);
            }
          }
    }
    public void init()
    {
      super.init();
      CommonUtils.assertNotNull(this.casServerLoginUrl, "casServerLoginUrl cannot be null.");
      if (strExcludeFile != null && strExcludeFile.trim().length() > 0) {
          arrExcludeFile = strExcludeFile.split(",");
      }
    }

    public final void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      String modifiedServiceUrl;
      HttpServletRequest request = (HttpServletRequest)servletRequest;
      HttpServletResponse response = (HttpServletResponse)servletResponse;
      HttpSession session = request.getSession(false);
      Assertion assertion = (session != null) ? (Assertion)session.getAttribute("_const_cas_assertion_") : null;
      
      if (assertion != null) {
        filterChain.doFilter(request, response);
        return;
      }
      
      [color=red]//excludeFile 跳出filter
      String requestStr = request.getRequestURL().toString();
      this.log.debug("requestStr-->"+requestStr);
      PathMatcher matcher = new AntPathMatcher();
      if(arrExcludeFile != null){
          for(String excludePath : arrExcludeFile){
              boolean flag = matcher.match(excludePath, requestStr);
              if(!flag){
                  flag = requestStr.indexOf(excludePath) > 0;
              }
              if(flag){
                  this.log.debug("excludePath " + excludePath + " pass sso authentication");
                  filterChain.doFilter(request, response);
                  return;
              }
          }
      }[/color]
      

      String serviceUrl = constructServiceUrl(request, response);
      String ticket = CommonUtils.safeGetParameter(request, getArtifactParameterName());
      boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);

      if ((CommonUtils.isNotBlank(ticket)) || (wasGatewayed)) {
        filterChain.doFilter(request, response);
        return;
      }

      this.log.debug("no ticket and no assertion found");
      if (this.gateway) {
        this.log.debug("setting gateway attribute in session");
        modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl);
      } else {
        modifiedServiceUrl = serviceUrl;
      }

      if (this.log.isDebugEnabled()) {
        this.log.debug("Constructed service url: " + modifiedServiceUrl);
      }

      String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);

      if (this.log.isDebugEnabled()) {
        this.log.debug("redirecting to \"" + urlToRedirectTo + "\"");
      }

      response.sendRedirect(urlToRedirectTo);
    }

    public final void setRenew(boolean renew) {
      this.renew = renew;
    }

    public final void setGateway(boolean gateway) {
      this.gateway = gateway;
    }

    public final void setCasServerLoginUrl(String casServerLoginUrl) {
      this.casServerLoginUrl = casServerLoginUrl;
    }

    public final void setGatewayStorage(GatewayResolver gatewayStorage) {
      this.gatewayStorage = gatewayStorage;
    }

    public void setStrExcludeFile(String strExcludeFile) {
        this.strExcludeFile = strExcludeFile;
    }
}]


2.实现CasTicketValidationFilter extends AbstractTicketValidationFilter
同样实现filter,过滤掉url
//exclude file 
      this.log.debug("requestStr-->"+requestUri);
      PathMatcher matcher = new AntPathMatcher();
      if(arrExcludeFile != null){
          for(String excludePath : arrExcludeFile){
              boolean flag = matcher.match(excludePath, requestUri);
              if(!flag){
                  flag = requestUri.indexOf(excludePath) > 0;
              }
              if(flag){
                  this.log.debug("excludePath " + excludePath + " pass sso authentication in validationFilter");
                  filterChain.doFilter(request, response);
                  return false;
              }
          }
      }


3.配置web.xml
<!-- cas begin -->
	<filter> 
		<filter-name>CAS Authentication Filter</filter-name> 
		<filter-class>CASAuthenticationFilter</filter-class> 
	
		<init-param> 
			<param-name>casServerLoginUrl</param-name> 
			<param-value>http://localhost:8088/cas-web/login</param-value> 
		</init-param> 
		<init-param> 
			<param-name>renew</param-name> 
			<param-value>false</param-value> 
		</init-param> 
		<init-param> 
			<param-name>gateway</param-name> 
			<param-value>false</param-value> 
		</init-param> 
		<init-param> 
			<param-name>serverName</param-name> 
			<param-value>http://localhost:8090</param-value> 
		</init-param> 
		<init-param>
			<param-name>ExcludeFile</param-name>
			<param-value>ShowOrderDetail.jsp,OrderDtl.jsp</param-value>
		</init-param>
	</filter> 
 
	<filter> 
		<filter-name>CAS Validation Filter</filter-name> 
		<filter-class>CasTicketValidationFilter</filter-class> 
		<init-param> 
			<param-name>casServerUrlPrefix</param-name> 
			<param-value>http://localhost:8088/cas-web</param-value> 
		</init-param> 
		<init-param>
			<param-name>serverName</param-name> 
			<param-value>http://localhost:8090</param-value> 
		</init-param> 
		<init-param> 
			<param-name>useSession</param-name> 
			<param-value>true</param-value> 
		</init-param> 
		<init-param> 
			<param-name>redirectAfterValidation</param-name> 
			<param-value>true</param-value> 
		</init-param> 
		<init-param>
			<param-name>ExcludeFile</param-name>
			<param-value>ShowOrderDetail.jsp,OrderDtl.jsp</param-value>
		</init-param>
	</filter> 
分享到:
评论
2 楼 soft_xiang 2019-04-24  
不需要重写代码,可以直接配置的
1 楼 zqb666kkk 2016-01-05  
2.实现CasTicketValidationFilter extends AbstractTicketValidationFilter 


这个代码怎么没有写全?

麻烦 博主写全下 好吗


发图不发种 菊花万人捅
有点公德心

相关推荐

    单点登录sso-shiro-cas-maven

    spring下使用shiro+cas配置单点登录,多个系统之间的访问,每次只需要登录一次 ## 系统模块说明 1. cas: 单点登录模块,这里直接拿的是cas的项目改了点样式而已 2. doc: 文档目录,里面有数据库生成语句,采用的...

    Spring Security 3 与 CAS单点登录配置.doc

    Spring Security 3 与 CAS 单点登录配置 Spring Security 3 是一个基于 Java 的安全框架,提供了灵活的身份验证、授权和访问控制功能。CAS(Central Authentication Service)是一种流行的单点登录协议,允许用户...

    CAS客户端JAR包版本3.3.3

    -- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 --&gt; org.jasig.cas.client.session.SingleSignOutHttpSessionListener &lt;!-- 该过滤器用于实现单点登出功能,可选配置。 --&gt; ...

    spring security 参考手册中文版

    5.1.2 AbstractSecurityWebApplicationInitializer不存在Spring 31 5.1.3使用Spring MVC的AbstractSecurityWebApplicationInitializer 32 5.2 HttpSecurity 32 5.3 Java配置和表单登录 34 5.4授权请求 35 5.5处理...

    java开源包3

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包4

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    spring in action英文版

     11.2.4 基于Acegi和Yale CAS实现单次登录  11.3 控制访问  11.3.1 访问决策投票  11.3.2 决定如何投票  11.3.3 处理投票弃权  11.4 保护Web应用程序  11.4.1 代理Acegi的过滤器  11.4.2 ...

    java开源包1

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包11

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包2

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包6

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包5

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包10

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包8

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包7

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包9

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包101

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    Java资源包01

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    JAVA上百实例源码以及开源项目

     Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...

    JAVA上百实例源码以及开源项目源代码

    EJB中JNDI的使用源码例子 1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件...

Global site tag (gtag.js) - Google Analytics