包含原理配置实现及简易demo CAS4.1单点登录实现( 四 )


②修改配置文件内容 , 并新增部分配置内容
修改%%//cas//t.xml
首先注释掉下述代码(注释默认用户名和密码)

添加下述代码 , MD5密码验证 , 数据库配置 , 认证类配置

③添加相关jar包
mysql--java-5.1.46.jar
cas---jdbc-4.1.0.jar
c3p0-0.9.5.5.jar
(最开始使用了cas---jdbc-4.0.0.jar , 发现输入正确的用户及密码时 , 无法实现正常页面跳转 , 且密码栏会被清空 , 切换为与本demo中的版本完全匹配的jar后此问题被修复)
重启的服务端1 , 重启客户端2、客户端3进行测试
输入admin/
效果如下图
可见 , 此时我们自己在数据库中添加的用户可以实现单点登录及单点登出 。
客户端采用cas--core 3.5.0版本依赖 , 实现cas单点登录登出效果
前述文中阐述了使用模板依赖cas--- 2.3.0-GA的客户端实现方式:
net.unicon.cascas-client-autoconfig-support2.3.0-GA
而实际上 , cas-的客户端接入方式不止一种 , 下面将简述另一种客户端接入方式:采用cas--core包的客户端接入 。
首先引入依赖:
org.jasig.cas.clientcas-client-core3.5.0
与前文集成 CAS略有不同 , 本集成不需要在启动类上加入启用 CAS的 @ 注解 。如果加了这个注解 ,  CAS 是不能实现根据配置文件开关的 , 每次想要关掉 , 必须修改代码 , 注释调注解才行 。
然后编写配置类:
import lombok.extern.slf4j.Slf4j;import org.jasig.cas.client.authentication.AuthenticationFilter;import org.jasig.cas.client.session.SingleSignOutFilter;import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;/*** Author Haozhonghao* Date 2022/1/10 10:34* Version 1.0* CAS集成核心配置类*/@Configuration@Slf4j@ConditionalOnProperty(value = "http://www.kingceram.com/post/cas.loginType", havingValue = "http://www.kingceram.com/post/cas")public class CasFilterConfig {/*** 需要走cas拦截的地址(/* 所有地址都拦截)*/@Value("${cas.urlPattern}")private String filterUrl;/*** 默认的cas地址 , 防止通过 配置信息获取不到*/@Value("${cas.server-url-prefix}")private String casServerUrl;/*** 应用访问地址(这个地址需要在cas服务端进行配置)*/@Value("${cas.authentication-url}")private String authenticationUrl;/*** 应用访问地址(这个地址需要在cas服务端进行配置)*/@Value("${cas.client-host-url}")private String appServerUrl;@Beanpublic ServletListenerRegistrationBean servletListenerRegistrationBean() {log.info(" \n cas 单点登录配置 \n appServerUrl = " + appServerUrl + "\n casServerUrl = " + casServerUrl);log.info(" servletListenerRegistrationBean ");ServletListenerRegistrationBean listenerRegistrationBean = new ServletListenerRegistrationBean();listenerRegistrationBean.setListener(new SingleSignOutHttpSessionListener());listenerRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);return listenerRegistrationBean;}/*** 单点登录退出*/@Beanpublic FilterRegistrationBean singleSignOutFilter() {log.info(" servletListenerRegistrationBean ");FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new SingleSignOutFilter());registrationBean.addUrlPatterns(filterUrl);registrationBean.addInitParameter("casServerUrlPrefix", casServerUrl);registrationBean.setName("CAS Single Sign Out Filter");registrationBean.setOrder(1);return registrationBean;}/*** 单点登录认证*/@Beanpublic FilterRegistrationBean AuthenticationFilter() {log.info(" AuthenticationFilter ");FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new AuthenticationFilter());registrationBean.addUrlPatterns(filterUrl);registrationBean.setName("CAS Filter");registrationBean.addInitParameter("casServerLoginUrl", casServerUrl);registrationBean.addInitParameter("serverName", appServerUrl);registrationBean.setOrder(1);return registrationBean;}/*** 单点登录校验*/@Beanpublic FilterRegistrationBean Cas30ProxyReceivingTicketValidationFilter() {log.info(" Cas30ProxyReceivingTicketValidationFilter ");FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new Cas30ProxyReceivingTicketValidationFilter());registrationBean.addUrlPatterns(filterUrl);registrationBean.setName("CAS Validation Filter");registrationBean.addInitParameter("casServerUrlPrefix", authenticationUrl);registrationBean.addInitParameter("serverName", appServerUrl);registrationBean.setOrder(1);return registrationBean;}/*** 单点登录请求包装*/@Beanpublic FilterRegistrationBean httpServletRequestWrapperFilter() {log.info(" httpServletRequestWrapperFilter ");FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new HttpServletRequestWrapperFilter());registrationBean.addUrlPatterns(filterUrl);registrationBean.setName("CAS HttpServletRequest Wrapper Filter");registrationBean.setOrder(1);return registrationBean;}}