CAS单点登录源码解析之【客户端】
cas 3.5.3服务器搭建+spring boot集成+shiro模拟登录(不修改现有shiro认证架构)。因为我们属于供应商,所以有些客户那里会需要接对方的CAS,所以没有使用shiro和cas的直接集成模式,如果是这种情况,可以参考:https://blog.csdn.net/catoop/article/details/50534006。
Cas Client主要有四个核心过滤器:
l AuthenticationFilter
l TicketValidationFilter
l HttpServletRequestWrapperFilter
l AssertionThreadLocalFilter
他们的作用解释如下:
AuthenticationFilter
AuthenticationFilter用来拦截所有的请求,用以判断用户是否需要通过Cas Server进行认证,如果需要则将跳转到Cas Server的登录页面。如果不需要进行登录认证,则请求会继续往下执行。
AuthenticationFilter有两个用户必须指定的参数,一个是用来指定Cas Server登录地址的casServerLoginUrl,另一个是用来指定认证成功后需要跳转地址的serverName或service。service和serverName只需要指定一个就可以了。当两者都指定了,参数service将具有更高的优先级,即将以service指定的参数值为准。service和serverName的区别在于service指定的是一个确定的URL,认证成功后就会确切的跳转到service指定的URL;而serverName则是用来指定主机名,其格式为{protocol}:{hostName}:{port},如:https://localhost:8443,当指定的是serverName时,AuthenticationFilter将会把它附加上当前请求的URI,以及对应的查询参数来构造一个确定的URL,如指定serverName为“http://localhost”,而当前请求的URI为“/app”,查询参数为“a=b&b=c”,则对应认证成功后的跳转地址将为“http://localhost/app?a=b&b=c”。
除了上述必须指定的参数外,AuthenticationFilter还可以指定如下可选参数:
l renew:当指定renew为true时,在请Cas Server时将带上参数“renew=true”,默认为false。
l gateway:指定gateway为true时,在请求Cas Server时将带上参数“gateway=true”,默认为false。
l artifactParameterName:指定ticket对应的请求参数名称,默认为ticket。
l serviceParameterName:指定service对应的请求参数名称,默认为service。
TicketValidationFilter
在请求通过AuthenticationFilter的认证之后,如果请求中携带了参数ticket则将会由TicketValidationFilter来对携带的ticket进行校验。TicketValidationFilter只是对验证ticket的这一类Filter的统称,其并不对应Cas Client中的一个具体类型。Cas Client中有多种验证ticket的Filter,都继承自AbstractTicketValidationFilter,它们的验证逻辑都是一致的,都有AbstractTicketValidationFilter实现,所不同的是使用的TicketValidator不一样。默认是Cas10TicketValidationFilter。
可选参数包括:
l redirectAfterValidation :表示是否验证通过后重新跳转到该URL,但是不带参数ticket,默认为true。
l useSession :在验证ticket成功后会生成一个Assertion对象,如果useSession为true,则会将该对象存放到Session中。如果为false,则要求每次请求都需要携带ticket进行验证,显然useSession为false跟redirectAfterValidation为true是冲突的。默认为true。
l exceptionOnValidationFailure :表示ticket验证失败后是否需要抛出异常,默认为true。
l renew:当值为true时将发送“renew=true”到Cas Server,默认为false。
HttpServletRequestWrapperFilter
HttpServletRequestWrapperFilter用于将每一个请求对应的HttpServletRequest封装为其内部定义的CasHttpServletRequestWrapper,该封装类将利用之前保存在Session或request中的Assertion对象重写HttpServletRequest的getUserPrincipal()、getRemoteUser()和isUserInRole()方法。这样在我们的应用中就可以非常方便的从HttpServletRequest中获取到用户的相关信息。
AssertionThreadLocalFilter
AssertionThreadLocalFilter是为了方便用户在应用的其它地方获取Assertion对象,其会将当前的Assertion对象存放到当前的线程变量中,那么以后用户在程序的任何地方都可以从线程变量中获取当前Assertion,无需再从Session或request中进行解析。该线程变量是由AssertionHolder持有的,我们在获取当前的Assertion时也只需要通过AssertionHolder的getAssertion()方法获取即可,如:
Assertion assertion = AssertionHolder.getAssertion();
cas client/shiro过滤器顺序,集成cas的情况下,cas客户端总是第一个过滤器。
org.jasig.cas.client.validation.AbstractTicketValidationFilter#doFilter
org.jasig.cas.client.util.CommonUtils#safeGetParameter
org.jasig.cas.client.authentication.AuthenticationFilter#doFilter --在这里修改源码,拿到assertion、且为空后需要判断token是否存在且有效,如果存在且有效,说明已经登录过,则直接返回
org.jasig.cas.client.util.CommonUtils#constructRedirectUrl
javax.servlet.http.HttpServletResponse#sendRedirect --此时请求去了CAS
最后进入shiro过滤器UserFilter.isAccessAllowed
因为在userfilter里面标准的cas登录后,是可以通过UserPrincipal拿到当前的用户信息的,但是当我们是集群模式的时候因为直接在AuthenticationFilter#doFilter中拦截返回了,且没有明确设置,自然就拿不到了,servletrequest中没有提供设置UserPrincipal的入口,cas的org.jasig.cas.client.validation.Cas20ServiceTicketValidator#parseResponseFromServer是创建了,但是org.jasig.cas.client.validation.Cas20ServiceTicketValidator#customParseResponse的实现体是空的。如下:
protected final Assertion parseResponseFromServer(final String response) throws TicketValidationException { final String error = XmlUtils.getTextForElement(response, "authenticationFailure"); if (CommonUtils.isNotBlank(error)) { throw new TicketValidationException(error); } final String principal = XmlUtils.getTextForElement(response, "user"); final String proxyGrantingTicketIou = XmlUtils.getTextForElement(response, "proxyGrantingTicket"); final String proxyGrantingTicket = this.proxyGrantingTicketStorage != null ? this.proxyGrantingTicketStorage.retrieve(proxyGrantingTicketIou) : null; if (CommonUtils.isEmpty(principal)) { throw new TicketValidationException("No principal was found in the response from the CAS server."); } final Assertion assertion; final Map<String,Object> attributes = extractCustomAttributes(response); if (CommonUtils.isNotBlank(proxyGrantingTicket)) { final AttributePrincipal attributePrincipal = new AttributePrincipalImpl(principal, attributes, proxyGrantingTicket, this.proxyRetriever); assertion = new AssertionImpl(attributePrincipal); } else { assertion = new AssertionImpl(new AttributePrincipalImpl(principal, attributes)); } customParseResponse(response, assertion); return assertion; }
stackoverflow也没搜到。如下:
https://stackoverflow.com/questions/25885747/when-and-where-java-security-set-userprincipal
https://docs.oracle.com/javase/7/docs/jre/api/security/jaas/spec/com/sun/security/auth/UserPrincipal.html
此时如果设置UserPrincipal需要的话,需要自己通过HttpServletRequestWrapperFilter重写一遍,如下:
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { final AttributePrincipal principal = retrievePrincipalFromSessionOrRequest(servletRequest); filterChain.doFilter(new CasHttpServletRequestWrapper((HttpServletRequest) servletRequest, principal), servletResponse); } protected AttributePrincipal retrievePrincipalFromSessionOrRequest(final ServletRequest servletRequest) { final HttpServletRequest request = (HttpServletRequest) servletRequest; final HttpSession session = request.getSession(false); final Assertion assertion = (Assertion) (session == null ? request.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION) : session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION)); return assertion == null ? null : assertion.getPrincipal(); }
这样的话,shiro UserFilter里面就可以通过
AttributePrincipal principal = (AttributePrincipal)((HttpServletRequest) request).getUserPrincipal();
拿到登录用户的信息了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2018-10-19 oracle 18c的版本号规则
2018-10-19 mysql 虚拟列导入报错1906说明
2016-10-19 推荐两个很好用的javascript模板引擎
2016-10-19 API的非向后兼容性无论如何通常代表着一种比较差的设计
2016-10-19 MessageFormat格式化的一些问题