关于cas客户端的基本配置这里就不多说了,不清楚的可以参考上一篇博文:配置简单cas客户端。这里是关于cas客户端实现动态配置认证需要开发说明。

往往业务系统中有些模块或功能是可以不需要登录就可以访问的,但是添加了cas客户端之后,通过cas客户端filter中的url-pattern来设置需要过滤的url,有时根本无法满足实际业务的需求,这里笔者就通过对cas客户端中源码的阅读,和对认证流程的理解,对cas客户端做了些改动,来实现动态配置cas客户端认证范围。

下面是cas认证的核心配置,其中AuthenticationFilter过滤器为cas客户端核心过滤,下面的url-pattern是配置需要过滤的url,如果我们能编写该过滤器,我们就可以实现动态配置cas客户端的过滤url了。

  1. <!-- cas统一认证  -->
  2. <filter>
  3. <filter-name>CASFilter</filter-name>
  4. <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
  5. <init-param>
  6. <param-name>casServerLoginUrl</param-name>
  7. <param-value>http://localhost:8080/casServer3/login</param-value>
  8. </init-param>
  9. <init-param>
  10. <param-name>serverName</param-name>
  11. <param-value>http://localhost:8080</param-value>
  12. </init-param>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>CASFilter</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. <filter>
  19. <filter-name>CAS Validation Filter</filter-name>
  20. <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
  21. <init-param>
  22. <param-name>casServerUrlPrefix</param-name>
  23. <param-value>http://localhost:8080/casServer3</param-value>
  24. </init-param>
  25. <init-param>
  26. <param-name>serverName</param-name>
  27. <param-value>http://localhost:8080</param-value>
  28. </init-param>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>CAS Validation Filter</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>

思路:将配置中指向的核心过滤器,指向自己定义的过滤器,将源码中核心过滤器AuthenticationFilter的代码复制拷贝到该自定义过滤器中,然后在该过滤器中添加自己的过滤规则。

步骤:

1.配置并启动cas服务端,具体配置可以参考博文:搭建简单的cas认证服务

2.新建一个web项目,然后添加cas客户端配置,具体配置可以参考博文:配置简单cas客户端

3.导入cas客户端核心jar的源码到该web项目中,源码在cas客户端下载zip包中就有,一般为cas-client-core文件夹

4.在项目的src中新建类AuthenticationFilter,继承org.jasig.cas.client.util.AbstractCasFilter,打开web.xml文件,找到找到cas核心过滤器的配置项CASFilter,Ctrl+左键,点击进入org.jasig.cas.client.authentication.AuthenticationFilter类中,复制类里面的全部代码到自定义的AuthenticationFilter类中。修改web.xml中cas核心过滤器配置项CASFilter中的配置,将filter-class指向刚才自定义的AuthenticationFilter类,同时在该过滤器中添加<init-param>配置。如下

  1. <filter>
  2. <filter-name>CASFilter</filter-name>
  3. <filter-class>com.supre.filter.AuthenticationFilter</filter-class>
  4. <init-param>
  5. <param-name>casServerLoginUrl</param-name>
  6. <param-value>http://localhost:8080/casServer3/login</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>serverName</param-name>
  10. <param-value>http://localhost:8080</param-value>
  11. </init-param>
  12. <init-param>
  13. <param-name>excludePaths</param-name>
  14. <param-value>.*[/,\\]rest[/,\\].*</param-value>
  15. </init-param>
  16. </filter>
  17. <filter-mapping>
  18. <filter-name>CASFilter</filter-name>
  19. <url-pattern>/*</url-pattern>
  20. </filter-mapping>

说明:

1其中param-name为参数名,这个在过滤器初始化中需要根据该名字来取param-value中的值

2其中param-value的值可以根据需要在filter中制定自己的规则,笔者这里是正则表达式

5.在自定义的AuthenticationFilter中添加自己的代码,来实现认证范围的控制,代码如下:

  1. package com.supre.filter;
  2. import java.io.IOException;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import org.jasig.cas.client.authentication.DefaultGatewayResolverImpl;
  12. import org.jasig.cas.client.authentication.GatewayResolver;
  13. import org.jasig.cas.client.util.AbstractCasFilter;
  14. import org.jasig.cas.client.util.CommonUtils;
  15. import org.jasig.cas.client.validation.Assertion;
  16. /**
  17. * 为了方便控制filter,自定义了统一认证过滤器AuthenticationFilter
  18. * @author Administrator
  19. *
  20. */
  21. public class AuthenticationFilter extends AbstractCasFilter{
  22. /**
  23. * The URL to the CAS Server login.
  24. */
  25. private String casServerLoginUrl;
  26. /**
  27. * Whether to send the renew request or not.
  28. */
  29. private boolean renew = false;
  30. /**
  31. * Whether to send the gateway request or not.
  32. */
  33. private boolean gateway = false;
  34. /**
  35. * 添加属性,这里用来存放不过滤地址正则表达式,可以根据自己需求定制---1
  36. */
  37. private String excludePaths;
  38. private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl();
  39. protected void initInternal(final FilterConfig filterConfig) throws ServletException {
  40. if (!isIgnoreInitConfiguration()) {
  41. super.initInternal(filterConfig);
  42. setCasServerLoginUrl(getPropertyFromInitParams(filterConfig, "casServerLoginUrl", null));
  43. log.trace("Loaded CasServerLoginUrl parameter: " + this.casServerLoginUrl);
  44. setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false")));
  45. log.trace("Loaded renew parameter: " + this.renew);
  46. setGateway(parseBoolean(getPropertyFromInitParams(filterConfig, "gateway", "false")));
  47. log.trace("Loaded gateway parameter: " + this.gateway);
  48. final String gatewayStorageClass = getPropertyFromInitParams(filterConfig, "gatewayStorageClass", null);
  49. if (gatewayStorageClass != null) {
  50. try {
  51. this.gatewayStorage = (GatewayResolver) Class.forName(gatewayStorageClass).newInstance();
  52. } catch (final Exception e) {
  53. log.error(e,e);
  54. throw new ServletException(e);
  55. }
  56. }
  57. //自定义添加代码,用来读取web配置文件中excludes属性值 ---2
  58. excludePaths = getPropertyFromInitParams(filterConfig, "excludePaths", null);//filterConfig.getInitParameter("excludePaths");
  59. excludePaths = excludePaths.trim();
  60. }
  61. }
  62. public void init() {
  63. super.init();
  64. CommonUtils.assertNotNull(this.casServerLoginUrl, "casServerLoginUrl cannot be null.");
  65. }
  66. // url判断逻辑,这里大家可以根据自己需要来制订规则
  67. private boolean isExclude(String uri){
  68. boolean isInWhiteList = false;
  69. if(excludePaths!=null&& uri!=null){
  70. isInWhiteList = uri.matches(excludePaths);
  71. }
  72. return isInWhiteList;
  73. }
  74. public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
  75. final HttpServletRequest request = (HttpServletRequest) servletRequest;
  76. final HttpServletResponse response = (HttpServletResponse) servletResponse;
  77. final HttpSession session = request.getSession(false);
  78. final Assertion assertion = session != null ? (Assertion) session.getAttribute(CONST_CAS_ASSERTION) : null;
  79. // 该判断是自定义的对符合条件的url进行通过处理 ---3
  80. if(isExclude(request.getRequestURI())){
  81. filterChain.doFilter(request, response);
  82. return;
  83. }
  84. if (assertion != null) {
  85. filterChain.doFilter(request, response);
  86. return;
  87. }
  88. final String serviceUrl = constructServiceUrl(request, response);
  89. final String ticket = CommonUtils.safeGetParameter(request,getArtifactParameterName());
  90. final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);
  91. if (CommonUtils.isNotBlank(ticket) || wasGatewayed) {
  92. filterChain.doFilter(request, response);
  93. return;
  94. }
  95. final String modifiedServiceUrl;
  96. log.debug("no ticket and no assertion found");
  97. if (this.gateway) {
  98. log.debug("setting gateway attribute in session");
  99. modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl);
  100. } else {
  101. modifiedServiceUrl = serviceUrl;
  102. }
  103. if (log.isDebugEnabled()) {
  104. log.debug("Constructed service url: " + modifiedServiceUrl);
  105. }
  106. final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);
  107. if (log.isDebugEnabled()) {
  108. log.debug("redirecting to \"" + urlToRedirectTo + "\"");
  109. }
  110. response.sendRedirect(urlToRedirectTo);
  111. }
  112. public final void setRenew(final boolean renew) {
  113. this.renew = renew;
  114. }
  115. public final void setGateway(final boolean gateway) {
  116. this.gateway = gateway;
  117. }
  118. public final void setCasServerLoginUrl(final String casServerLoginUrl) {
  119. this.casServerLoginUrl = casServerLoginUrl;
  120. }
  121. public final void setGatewayStorage(final GatewayResolver gatewayStorage) {
  122. this.gatewayStorage = gatewayStorage;
  123. }
  124. }

说明:上面的例子笔者是想在web中配置不需要认证的url,通过正则表达式来判断,这里相关的规则可以根据自己需要来编写。

6.到这里就基本完成了,根据自己定义的规则来做测试,大家可以在项目中创建多个jsp或html文件,放在不同目录下(部分设计为通过,部分设计为不通过),然后在浏览器中直接访问这些文件,看是否被拦截而跳到认证见面,通过根据自己定义的规则判断修改是否成功。

自定义cas客户端核心过滤器AuthenticationFilter的更多相关文章

  1. CAS学习笔记三:SpringBoot自动配置与手动配置过滤器方式集成CAS客户端

    本文目标 基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式集成CAS客户端. 需要提前搭建 CAS 服务端,参考 https://www.cnblogs.com/hell ...

  2. 带连接池的netty客户端核心功能实现剖解

    带连接池的netty客户端核心功能实现剖析 带连接池的netty的客户端核心功能实现剖析 本文为原创,转载请注明出处 源码地址: https://github.com/zhangxianwu/ligh ...

  3. 如何利用tomcat和cas实现单点登录(2):配置cas数据库验证和cas客户端配置

    接(1),上一篇主要讲述了tomcat和cas server端的部署. 接下来主要还有两个步骤. 注意:为了开启两个tomcat,要把直接配置的tomcat的环境变量取消!!!!!!!!!! 客户端配 ...

  4. cas sso单点登录系列2:cas客户端和cas服务端交互原理动画图解,cas协议终极分析

    转:http://blog.csdn.net/ae6623/article/details/8848107 1)PPT流程图:ppt下载:http://pan.baidu.com/s/1o7KIlom ...

  5. Spring Cloud源码分析(四)Zuul:核心过滤器

    通过之前发布的<Spring Cloud构建微服务架构(五)服务网关>一文,相信大家对于Spring Cloud Zuul已经有了一个基础的认识.通过前文的介绍,我们对于Zuul的第一印象 ...

  6. 【Tech】单点登录系统CAS客户端demo

    服务器端配置请参考: http://www.cnblogs.com/sunshineatnoon/p/4064632.html 工具:myeclipse或者javaee-eclipse 1.启动jav ...

  7. SSO单点登录系列1:cas客户端源码分析cas-client-java-2.1.1.jar

    落雨 cas 单点登录 希望能给以后来研究cas的兄弟留下一点思路,也算是研究了两天的成果,外国人的代码写的很晦涩,翻译下来也没有时间继续跟进,所以有错误的还请大家跟帖和我讨论,qq 39426378 ...

  8. SSO单点登录系列2:cas客户端和cas服务端交互原理动画图解,cas协议终极分析

    落雨 cas 单点登录 一.用户第一次访问web1应用. ps:上图少画了一条线,那一条线,应该再返回来一条,然后再到server端,画少了一步...谢谢提醒.而且,重定向肯定是从浏览器过去的.我写的 ...

  9. 品优购商城项目(六)CAS客户端与SpringSecurity集成

    cas单点登录旨在解决传统登录模式session在分布式项目中共享登录信息的问题. 本文cas服务器使用 4.0版本,仅供学习参考.把 cas.war 直接部署在tomcat即可,这里有个固定的用户名 ...

随机推荐

  1. 【POJ】1486:Sorting Slides【二分图关键边判定】

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5390   Accepted: 2095 De ...

  2. UVALive 6661 Equal Sum Sets

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  3. Linux性能监控分析命令(三)—iostat命令介绍

    性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof 命令介绍: i ...

  4. HDU 4718 The LCIS on the Tree (动态树LCT)

    The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  5. JTAG Level Translation

    http://www.freelabs.com/~whitis/electronics/jtag/ One of the big issues in making a JTAG pod is leve ...

  6. JSPGen4 自学路线图

  7. 报错: LINQ to Entities 不识别方法“Int32 Parse(System.String)

    断点调试发现报错的语句为: public ActionResult SomeMethod(string someId) { var temp = SomeService.LoadEntities(a ...

  8. java通过System.getProperty获取系统属性

    getProperties public static Properties getProperties() 确定当前的系统属性. 首先,如果有安全管理器,则不带参数直接调用其 checkProper ...

  9. [Linux] Linux 守护进程的启动方法

    reference : http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html "守护进程"(daemon)就是一直在后台 ...

  10. [Linux] 关于Unix哲学

    reference : http://www.ruanyifeng.com/blog/2009/06/unix_philosophy.html 先讲两个很老的小故事. 第一个故事. 有一家日本最大的化 ...