Commit d6a4edad authored by huyuchen's avatar huyuchen

cas修改

parent f28c4121
package com.yifu.cloud.plus.v1.yifu.auth.config;
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.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.stereotype.Component;
import java.util.*;
@Configuration
@Component
public class CASAutoConfig {
/**
* SingleSignOutHttpSessionListener 添加监听器
* 用于单点退出,该过滤器用于实现单点登出功能,可选配置
*
* @author licancan
* @param
* @return {@link ServletListenerRegistrationBean<EventListener>}
*/
@Bean
public ServletListenerRegistrationBean<EventListener> singleSignOutListenerRegistration() {
ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<EventListener>();
registrationBean.setListener(new SingleSignOutHttpSessionListener());
registrationBean.setOrder(1);
return registrationBean;
}
/**
* SingleSignOutFilter 登出过滤器
* 该过滤器用于实现单点登出功能,可选配置
*
* @author licancan
* @param
* @return {@link FilterRegistrationBean}
*/
@Bean
public FilterRegistrationBean filterSingleRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SingleSignOutFilter());
// 设定匹配的路径
registration.addUrlPatterns("/*");
Map<String, String> initParameters = new HashMap();
initParameters.put("casServerUrlPrefix", CASproperties.CAS_SERVER_LOGIN_PATH);
registration.setInitParameters(initParameters);
// 设定加载的顺序
registration.setOrder(2);
return registration;
}
/**
* AuthenticationFilter 授权过滤器
*
* @author licancan
* @param
* @return {@link FilterRegistrationBean}
*/
@Bean
public FilterRegistrationBean filterAuthenticationRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
Map<String, String> initParameters = new HashMap();
registration.setFilter(new AuthenticationFilter());
//registration.addUrlPatterns("*.html");
initParameters.put("casServerLoginUrl", CASproperties.CAS_SERVER_PATH);
initParameters.put("serverName", CASproperties.CLIENT_SERVER_NAME);
// 不拦截的请求
initParameters.put("ignorePattern", "^.*[.](js|css|gif|png|zip)$");
// 表示过滤所有
initParameters.put("ignoreUrlPatternType", "com.yifu.cloud.plus.v1.yifu.auth.filter.SimpleUrlPatternMatcherStrategy");
registration.setInitParameters(initParameters);
// 设定加载的顺序
registration.setOrder(3);
return registration;
}
/**
* Cas30ProxyReceivingTicketValidationFilter 验证过滤器
* 该过滤器负责对Ticket的校验工作,必须启用它
*
* @author licancan
* @param
* @return {@link FilterRegistrationBean}
*/
@Bean
public FilterRegistrationBean filterValidationRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
// 设定匹配的路径
//registration.addUrlPatterns("*.html");
Map<String, String> initParameters = new HashMap();
initParameters.put("casServerUrlPrefix", CASproperties.CAS_SERVER_PATH);
initParameters.put("serverName", CASproperties.CLIENT_SERVER_NAME);
// 是否对serviceUrl进行编码,默认true:设置false可以在302对URL跳转时取消显示;jsessionid=xxx的字符串
// 观察CommonUtils.constructServiceUrl方法可以看到
//initParameters.put("encodeServiceUrl", "false");
registration.setInitParameters(initParameters);
// 设定加载的顺序
registration.setOrder(4);
return registration;
}
/**
* 该过滤器用于单点登录功能 ,对HttpServletRequest请求包装, 可通过HttpServletRequest的getRemoteUser()方法获得登录用户的登录名
* @return
*/
@Bean
public FilterRegistrationBean casHttpServletRequestWrapperFilter(){
FilterRegistrationBean authenticationFilter = new FilterRegistrationBean();
authenticationFilter.setFilter(new HttpServletRequestWrapperFilter());
authenticationFilter.setOrder(5);
List<String> urlPatterns = new ArrayList<>();
urlPatterns.add("/*");
authenticationFilter.setUrlPatterns(urlPatterns);
return authenticationFilter;
}
/**
* HttpServletRequestWrapperFilter wraper过滤器
* 该过滤器负责实现HttpServletRequest请求的包裹,
* 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。
*
* @author licancan
* @param
* @return {@link FilterRegistrationBean}
*/
@Bean
public FilterRegistrationBean filterWrapperRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new HttpServletRequestWrapperFilter());
// 设定匹配的路径
registration.addUrlPatterns("/*");
// 设定加载的顺序
registration.setOrder(6);
return registration;
}
}
//package com.yifu.cloud.plus.v1.yifu.auth.config;
//
//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.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.stereotype.Component;
//
//import java.util.*;
//
//@Configuration
//@Component
//public class CASAutoConfig {
//
// /**
// * SingleSignOutHttpSessionListener 添加监听器
// * 用于单点退出,该过滤器用于实现单点登出功能,可选配置
// *
// * @author licancan
// * @param
// * @return {@link ServletListenerRegistrationBean<EventListener>}
// */
// @Bean
// public ServletListenerRegistrationBean<EventListener> singleSignOutListenerRegistration() {
// ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<EventListener>();
// registrationBean.setListener(new SingleSignOutHttpSessionListener());
// registrationBean.setOrder(1);
// return registrationBean;
// }
//
// /**
// * SingleSignOutFilter 登出过滤器
// * 该过滤器用于实现单点登出功能,可选配置
// *
// * @author licancan
// * @param
// * @return {@link FilterRegistrationBean}
// */
// @Bean
// public FilterRegistrationBean filterSingleRegistration() {
// FilterRegistrationBean registration = new FilterRegistrationBean();
// registration.setFilter(new SingleSignOutFilter());
// // 设定匹配的路径
// registration.addUrlPatterns("/*");
// Map<String, String> initParameters = new HashMap();
// initParameters.put("casServerUrlPrefix", CASproperties.CAS_SERVER_LOGIN_PATH);
// registration.setInitParameters(initParameters);
// // 设定加载的顺序
// registration.setOrder(2);
// return registration;
// }
//
// /**
// * AuthenticationFilter 授权过滤器
// *
// * @author licancan
// * @param
// * @return {@link FilterRegistrationBean}
// */
// @Bean
// public FilterRegistrationBean filterAuthenticationRegistration() {
//
// FilterRegistrationBean registration = new FilterRegistrationBean();
// Map<String, String> initParameters = new HashMap();
//
// registration.setFilter(new AuthenticationFilter());
// //registration.addUrlPatterns("*.html");
// initParameters.put("casServerLoginUrl", CASproperties.CAS_SERVER_PATH);
// initParameters.put("serverName", CASproperties.CLIENT_SERVER_NAME);
//
// // 不拦截的请求
// initParameters.put("ignorePattern", "^.*[.](js|css|gif|png|zip)$");
//
// // 表示过滤所有
//// initParameters.put("ignoreUrlPatternType", "com.yifu.cloud.plus.v1.yifu.auth.filter.SimpleUrlPatternMatcherStrategy");
//
// registration.setInitParameters(initParameters);
// // 设定加载的顺序
// registration.setOrder(3);
// return registration;
// }
//
//
// /**
// * Cas30ProxyReceivingTicketValidationFilter 验证过滤器
// * 该过滤器负责对Ticket的校验工作,必须启用它
// *
// * @author licancan
// * @param
// * @return {@link FilterRegistrationBean}
// */
// @Bean
// public FilterRegistrationBean filterValidationRegistration() {
// FilterRegistrationBean registration = new FilterRegistrationBean();
// registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
// // 设定匹配的路径
// //registration.addUrlPatterns("*.html");
// Map<String, String> initParameters = new HashMap();
// initParameters.put("casServerUrlPrefix", CASproperties.CAS_SERVER_PATH);
// initParameters.put("serverName", CASproperties.CLIENT_SERVER_NAME);
//
// // 是否对serviceUrl进行编码,默认true:设置false可以在302对URL跳转时取消显示;jsessionid=xxx的字符串
// // 观察CommonUtils.constructServiceUrl方法可以看到
// //initParameters.put("encodeServiceUrl", "false");
// registration.setInitParameters(initParameters);
// // 设定加载的顺序
// registration.setOrder(4);
// return registration;
// }
//
// /**
// * 该过滤器用于单点登录功能 ,对HttpServletRequest请求包装, 可通过HttpServletRequest的getRemoteUser()方法获得登录用户的登录名
// * @return
// */
// @Bean
// public FilterRegistrationBean casHttpServletRequestWrapperFilter(){
// FilterRegistrationBean authenticationFilter = new FilterRegistrationBean();
// authenticationFilter.setFilter(new HttpServletRequestWrapperFilter());
// authenticationFilter.setOrder(5);
// List<String> urlPatterns = new ArrayList<>();
// urlPatterns.add("/*");
// authenticationFilter.setUrlPatterns(urlPatterns);
// return authenticationFilter;
// }
//
// /**
// * HttpServletRequestWrapperFilter wraper过滤器
// * 该过滤器负责实现HttpServletRequest请求的包裹,
// * 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。
// *
// * @author licancan
// * @param
// * @return {@link FilterRegistrationBean}
// */
// @Bean
// public FilterRegistrationBean filterWrapperRegistration() {
// FilterRegistrationBean registration = new FilterRegistrationBean();
// registration.setFilter(new HttpServletRequestWrapperFilter());
// // 设定匹配的路径
// registration.addUrlPatterns("/*");
// // 设定加载的顺序
// registration.setOrder(6);
// return registration;
// }
//}
package com.yifu.cloud.plus.v1.yifu.auth.filter;
import org.jasig.cas.client.authentication.UrlPatternMatcherStrategy;
import java.util.Arrays;
import java.util.List;
public class SimpleUrlPatternMatcherStrategy implements UrlPatternMatcherStrategy {
/**
* 判断是否匹配这个字符串
*
* @author licancan
* @param url 用户请求的连接
* @return {@link boolean} true:不拦截 false:必须得登录了
*/
@Override
public boolean matches(String url) {
List<String> list = Arrays.asList(
"/login",
"/restLogin"
);
String name = url.substring(url.lastIndexOf("/"));
// String url = "http://127.0.0.1:8082/login?service=http%3A%2F%2F127.0.0.1%3A8082%2Fb";
// 为了防止出现匹配中,忽略了后续参数的URL,出现比如:/login?service=http%3A%2F%2F127.0.0.1%3A8082%2Fb 现象
// 这种现象,会导致重定向过多异常,所以,不管什么url,只取出请求的最终 /login 即可
if (name.indexOf("?") != -1) {
name = name.substring(0, name.indexOf("?"));
}
boolean result = list.contains(name);
if (!result) {
System.out.println("拦截URL:" + url);
}
return result;
}
/**
* 正则表达式的规则,这个地方可以是web传递过来的
*
* @author licancan
* @param pattern
* @return void
*/
@Override
public void setPattern(String pattern) {
System.out.println(11111);
}
}
//package com.yifu.cloud.plus.v1.yifu.auth.filter;
//
//import org.jasig.cas.client.authentication.UrlPatternMatcherStrategy;
//
//import java.util.Arrays;
//import java.util.List;
//
//public class SimpleUrlPatternMatcherStrategy implements UrlPatternMatcherStrategy {
//
// /**
// * 判断是否匹配这个字符串
// *
// * @author licancan
// * @param url 用户请求的连接
// * @return {@link boolean} true:不拦截 false:必须得登录了
// */
// @Override
// public boolean matches(String url) {
// List<String> list = Arrays.asList(
// "/login",
// "/restLogin"
// );
// String name = url.substring(url.lastIndexOf("/"));
//
// // String url = "http://127.0.0.1:8082/login?service=http%3A%2F%2F127.0.0.1%3A8082%2Fb";
// // 为了防止出现匹配中,忽略了后续参数的URL,出现比如:/login?service=http%3A%2F%2F127.0.0.1%3A8082%2Fb 现象
// // 这种现象,会导致重定向过多异常,所以,不管什么url,只取出请求的最终 /login 即可
//
// if (name.indexOf("?") != -1) {
// name = name.substring(0, name.indexOf("?"));
// }
// boolean result = list.contains(name);
// if (!result) {
// System.out.println("拦截URL:" + url);
// }
// return result;
// }
//
// /**
// * 正则表达式的规则,这个地方可以是web传递过来的
// *
// * @author licancan
// * @param pattern
// * @return void
// */
// @Override
// public void setPattern(String pattern) {
// System.out.println(11111);
// }
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment