CAS之5.2x版本之单点登录退出-yellowcong
2018-02-03 19:43
459 查看
单点不能只登录啊,还得退出啊,单点退出,需要完成两点重要工作:1、添加过滤器,过滤掉不需要登录的url,这个类需要实现
下面是完整配置
1、直接退出,就会到系统的默认页面,这个我们自己也是可以修改的。
2、加了参数service=http://xxx 的,推出后,就会直接跳转到指定的界面。
刚开始直接访问,通过数url太费劲了,我直接改了,直接弄这个,爽歪歪了。但是我可以看到
UrlPatternMatcherStrategy接口,然后需要在web.xml中配置。2、添加退出跳转的控制器。这个退出的方式有两种,一种是走默认的路径,第二种是走自定义的返回路基。3、修改服务端application.properties ,添加
cas.logout.followServiceRedirects=true,让客户端可以自己制定退出的路径。
项目代码
https://gitee.com/yellowcong/springboot_cas/tree/master/cas-client-loginout
项目结构
客户端
服务端
架构说明
节点 | 功能 | 项目名 |
---|---|---|
https://yellowcong.com:9000 | cas服务器 | cas-server-result |
http://yellowcong.com:8080/cas-client-maven/ | 子项目1 | cas-client-maven |
http://yellowcong.com:8080/cas-client-maven2/ | 子项目2 | cas-client-maven2 |
添加过滤器
创建过滤器类
我这个地方创建了一个叫SimpleUrlPatternMatcherStrategy,这个类需要实现
UrlPatternMatcherStrategy接口,添加 matches里面的方法,来判断是否过滤掉这条请求
package com.yellowcong.auth; import org.jasig.cas.client.authentication.UrlPatternMatcherStrategy; /** * 创建日期:2018年2月3日<br/> * 创建时间:下午5:36:48<br/> * 创建者 :yellowcong<br/> * 机能概要:过滤掉一些不需要授权,登录的界面 */ public class SimpleUrlPatternMatcherStrategy implements UrlPatternMatcherStrategy { /** * 创建日期:2018年2月3日<br/> * 创建时间:下午5:42:36<br/> * 创建用户:yellowcong<br/> * 机能概要: 判断是否匹配这个字符串 * @param url 用户请求的连接 * @return true : 我就不拦截你了 * false :必须得登录了 */ @Override public boolean matches(String url) { //http://yellowcong.com:8080/cas-client-maven/user/loginOut/success //当含有loginout的字段,就可以不用登录了 return url.contains("/loginOut/success"); } /** * 正则表达式的规则,这个地方可以是web传递过来的 */ @Override public void setPattern(String pattern) { } }
配置过滤器到web.xml
过滤器写好了,必须给配置上啊,不然写了干毛线啊<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>https://yellowcong.com:9000</param-value> </init-param> <!-- 这个服务的地址 --> <init-param> <param-name>serverName</param-name> <param-value>http://yellowcong.com:8080/cas-client-maven</param-value> </init-param> <!-- 不需要匹配的类 --> <init-param> <param-name>ignoreUrlPatternType</param-name> <param-value>com.yellowcong.auth.SimpleUrlPatternMatcherStrategy</param-value> </init-param> <!-- 不需要管的正则pattern --> <init-param> <param-name>ignorePattern</param-name> <param-value>.*</param-value> </init-param> </filter>
下面是完整配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>基于cas3.5.0 搭建的客户端</display-name>
<!-- 配置springmvc的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc 的配置信息 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 配置单点登出监听器 -->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<!-- 单点登录的过滤器 -->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
<!-- 配置单点登录的地址 -->
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>https://yellowcong.com:9000</param-value>
</init-param>
</filter>
<!-- 单点登录验证过滤器 -->
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter</filter-class>
<!-- 配置单点登录的地址 -->
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>https://yellowcong.com:9000</param-value>
</init-param>
<!-- 这个服务的地址 -->
<init-param>
<param-name>serverName</param-name>
<param-value>http://yellowcong.com:8080/cas-client-maven</param-value>
</init-param>
<init-param>
<param-name>useSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
<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>https://yellowcong.com:9000</param-value> </init-param> <!-- 这个服务的地址 --> <init-param> <param-name>serverName</param-name> <param-value>http://yellowcong.com:8080/cas-client-maven</param-value> </init-param> <!-- 不需要匹配的类 --> <init-param> <param-name>ignoreUrlPatternType</param-name> <param-value>com.yellowcong.auth.SimpleUrlPatternMatcherStrategy</param-value> </init-param> <!-- 不需要管的正则pattern --> <init-param> <param-name>ignorePattern</param-name> <param-value>.*</param-value> </init-param> </filter>
<!-- 登录验证器 -->
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 验证过滤器 -->
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 授权过滤器 -->
<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springmvc的过滤器 ,过滤器都配置到后面,这样好搞事情啊,你说呢-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置单点退出
退出的时候,有两种方式,一种是走默认的退出页面,另一种是,退出后,跳转到哪一个界面,当然,这个界面必须能允许未登录用户访问。package com.yellowcong.controller; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** *创建日期:2018年2月3日 <br/> *创建用户:yellowcong <br/> *机能说明:用户控制器 */ @RequestMapping("/user") @Controller public class UserController { /** * 创建日期:2018年2月3日<br/> * 创建时间:下午5:32:41<br/> * 创建用户:yellowcong<br/> * 机能概要:单点登出 * @param session * @return */ @RequestMapping("/loginOut") public String loginOut(HttpSession session){ session.invalidate(); //http://yellowcong.com:8080/cas-client-maven/user/loginOut/success //这个是直接退出,走的是默认退出方式 return "redirect:https://yellowcong.com:9000/logout"; } @RequestMapping("/loginOut2") public String loginOut2(HttpSession session){ session.invalidate(); //退出登录后,跳转到退成成功的页面,不走默认页面 return "redirect:https://yellowcong.com:9000/logout?service=http://yellowcong.com:8080/cas-client-maven/user/loginOut/success"; } /** * 创建日期:2018年2月3日<br/> * 创建时间:下午6:14:56<br/> * 创建用户:yellowcong<br/> * 机能概要:退出成功的界面 * @return */ @RequestMapping("/loginOut/success") public String loginOutPage(){ return "user/loginOut"; } }
首页代码
<%@ page import="org.jasig.cas.client.authentication.AttributePrincipal" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.Iterator" %> <%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>index</title> </head> <body> <h1>测试系统1</h1> <b>Authenticated User Id:</b> <a href="logout.jsp" title="Click here to log out"><%= request.getRemoteUser() %> </a> <br/> <a href="<%=request.getContextPath() %>/user/loginOut1" title="Click here to log out">退出系统(方式1)</a> <a href="<%=request.getContextPath() %>/user/loginOut2" title="Click here to log out">退出系统(方式2)</a> <% if (request.getUserPrincipal() != null) { AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); final Map attributes = principal.getAttributes(); if (attributes != null) { Iterator attributeNames = attributes.keySet().iterator(); out.println("<b>Attributes:</b>"); if (attributeNames.hasNext()) { out.println("<hr><table border='3pt' width='100%'>"); out.println("<th colspan='2'>Attributes</th>"); out.println("<tr><td><b>Key</b></td><td><b>Value</b></td></tr>"); for (; attributeNames.hasNext(); ) { out.println("<tr><td>"); String attributeName = (String) attributeNames.next(); out.println(attributeName); out.println("</td><td>"); final Object attributeValue = attributes.get(attributeName); if (attributeValue instanceof List) { final List values = (List) attributeValue; out.println("<strong>Multi-valued attribute: " + values.size() + "</strong>"); out.println("<ul>"); for (Object value : values) { out.println("<li>" + value + "</li>"); } out.println("</ul>"); } else { out.println(attributeValue); } out.println("</td></tr>"); } out.println("</table>"); } else { out.print("No attributes are supplied by the CAS server.</p>"); } } else { out.println("<pre>The attribute map is empty. Review your CAS filter configurations.</pre>"); } } else { out.println("<pre>The user principal is empty from the request object. Review the wrapper filter configuration.</pre>"); } %> </body> </html>
登录测试
两种退出方式
可以看到两种退出方式的结果1、直接退出,就会到系统的默认页面,这个我们自己也是可以修改的。
2、加了参数service=http://xxx 的,推出后,就会直接跳转到指定的界面。
子网站相互访问
当我们访问网站a,然后访问网站b,会发现,没有要用户再登录一遍。刚开始直接访问,通过数url太费劲了,我直接改了,直接弄这个,爽歪歪了。但是我可以看到
isFromNewLogin这两个站点参数是不一样的。
相关文章推荐
- CAS之5.2x版本自定义登录,多数据源登录-yellowcong
- CAS之5.2x版本自定义登录页面-yellowcong
- CAS之5.2x版本登录验证码-yellowcong
- CAS之5.2x版本单点登录服务安装-yellowcong
- CAS之5.2x版本之客户端集成(传统web项目)-yellowcong
- CAS之5.2x版本之服务器开发调试(eclipse调试cas)-yellowcong
- CAS之5.2x版本之服务管理-yellowcong
- CAS之5.2x版本之jdbc配置多返回值-yellowcong
- CAS之5.2x版本之通过yml的方式配置cas-yellowcong
- CAS之5.2x版本之中文返回值乱码问题-yellowcong
- CAS之5.2x版本自定义JDBC认证-yellowcong
- CAS 之3.x版本,单点登录服务搭建(Window)-yellowcong
- CAS之5.2x版本配置密码加密(MD5和SHA)-yellowcong
- CAS之5.2x版本自定义返回消息-yellowcong
- CAS之5.2x版本之客户端集成(Springboot)-yellowcong
- Cas之5.2.x版本之单点登录服务安装(SpringBoot启动)-yellowcong
- CAS之3.5版本,单点登录服务搭建(linux)-yellowcong
- CAS之 5.2x版本配置数据库认证-yellowcong
- CAS之5.2x版本自定义密码验证-yellowcong
- CAS之5.2x版本自定义错误信息-yellowcong