Commit d6a4edad authored by huyuchen's avatar huyuchen

cas修改

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