master
yangjie 3 years ago
parent 10320169e1
commit 8e62166d6b
  1. 17
      src/main/java/com/yipin/liuwanr/config/CasFilterConfig.java
  2. 8
      src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java
  3. 4
      src/main/java/com/yipin/liuwanr/filter/CasFilter.java
  4. 18
      src/main/java/com/yipin/liuwanr/filter/LocalUserInfoFilter.java
  5. 3
      src/main/java/com/yipin/liuwanr/util/CASUtil.java

@ -20,9 +20,10 @@ import org.springframework.core.Ordered;
@Configuration @Configuration
public class CasFilterConfig { public class CasFilterConfig {
//生效的cas配置
private static final String CAS_URL= "https://idas.uestc.edu.cn/authserver"; private static final String CAS_URL= "https://idas.uestc.edu.cn/authserver";
private static final String APP_URL= "http://222.197.183.3:8090"; private static final String APP_URL= "https://finance.uestc.cn";
@Bean @Bean
public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> servletListenerRegistrationBean(){ public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> servletListenerRegistrationBean(){
@ -40,7 +41,7 @@ public class CasFilterConfig {
public FilterRegistrationBean<SingleSignOutFilter> singleSignOutFilter(){ public FilterRegistrationBean<SingleSignOutFilter> singleSignOutFilter(){
FilterRegistrationBean<SingleSignOutFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<SingleSignOutFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new SingleSignOutFilter()); registrationBean.setFilter(new SingleSignOutFilter());
registrationBean.addUrlPatterns("/*"); registrationBean.addUrlPatterns("/userInfo/loginOut");
registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL ); registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL );
registrationBean.setName("CAS Single Sign Out Filter"); registrationBean.setName("CAS Single Sign Out Filter");
registrationBean.setOrder(2); registrationBean.setOrder(2);
@ -51,12 +52,11 @@ public class CasFilterConfig {
* 单点登录认证 * 单点登录认证
* @return registrationBean * @return registrationBean
*/ */
@Bean @Bean
public FilterRegistrationBean<AuthenticationFilter> authenticationFilter(){ public FilterRegistrationBean<AuthenticationFilter> authenticationFilter(){
FilterRegistrationBean<AuthenticationFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<AuthenticationFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new AuthenticationFilter()); registrationBean.setFilter(new AuthenticationFilter());
registrationBean.addUrlPatterns("/*"); registrationBean.addUrlPatterns("/userInfo/loginSchoolClient");
registrationBean.setName("CAS Filter"); registrationBean.setName("CAS Filter");
registrationBean.addInitParameter("casServerLoginUrl",CAS_URL); registrationBean.addInitParameter("casServerLoginUrl",CAS_URL);
registrationBean.addInitParameter("serverName", APP_URL ); registrationBean.addInitParameter("serverName", APP_URL );
@ -73,7 +73,7 @@ public class CasFilterConfig {
public FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> cas20ProxyReceivingTicketValidationFilter(){ public FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> cas20ProxyReceivingTicketValidationFilter(){
FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new Cas20ProxyReceivingTicketValidationFilter()); registrationBean.setFilter(new Cas20ProxyReceivingTicketValidationFilter());
registrationBean.addUrlPatterns("/*"); registrationBean.addUrlPatterns("/userInfo/loginSchoolClient");
registrationBean.setName("CAS Validation Filter"); registrationBean.setName("CAS Validation Filter");
registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL ); registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL );
registrationBean.addInitParameter("serverName", APP_URL ); registrationBean.addInitParameter("serverName", APP_URL );
@ -90,7 +90,7 @@ public class CasFilterConfig {
public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter(){ public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter(){
FilterRegistrationBean<HttpServletRequestWrapperFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<HttpServletRequestWrapperFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new HttpServletRequestWrapperFilter()); registrationBean.setFilter(new HttpServletRequestWrapperFilter());
registrationBean.addUrlPatterns("/*"); registrationBean.addUrlPatterns("/userInfo/loginSchoolClient");
registrationBean.setName("CAS HttpServletRequest Wrapper Filter"); registrationBean.setName("CAS HttpServletRequest Wrapper Filter");
registrationBean.setOrder(5); registrationBean.setOrder(5);
return registrationBean; return registrationBean;
@ -103,10 +103,11 @@ public class CasFilterConfig {
@Bean @Bean
public FilterRegistrationBean<LocalUserInfoFilter> localUserInfoFilter(){ public FilterRegistrationBean<LocalUserInfoFilter> localUserInfoFilter(){
FilterRegistrationBean<LocalUserInfoFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<LocalUserInfoFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new LocalUserInfoFilter()); registrationBean.setFilter(new LocalUserInfoFilter());
registrationBean.addUrlPatterns("/*"); registrationBean.addUrlPatterns("/userInfo/loginSchoolClient");
registrationBean.setName("localUserInfoFilter"); registrationBean.setName("localUserInfoFilter");
registrationBean.setOrder(6); registrationBean.setOrder(6);
return registrationBean; return registrationBean;
@ -114,9 +115,11 @@ public class CasFilterConfig {
@Bean @Bean
public FilterRegistrationBean<AssertionThreadLocalFilter> assertionThreadLocalFilter(){ public FilterRegistrationBean<AssertionThreadLocalFilter> assertionThreadLocalFilter(){
FilterRegistrationBean<AssertionThreadLocalFilter> registrationBean FilterRegistrationBean<AssertionThreadLocalFilter> registrationBean
= new FilterRegistrationBean<>(); = new FilterRegistrationBean<>();
registrationBean.setFilter(new AssertionThreadLocalFilter()); registrationBean.setFilter(new AssertionThreadLocalFilter());
registrationBean.setOrder(4);
return registrationBean; return registrationBean;
} }

@ -34,16 +34,16 @@ public class AuthInterceptor implements HandlerInterceptor {
Object attribute = request.getSession().getAttribute("_const_cas_assertion_"); Object attribute = request.getSession().getAttribute("_const_cas_assertion_");
if (!StringUtils.isEmpty(attribute)){ if (!StringUtils.isEmpty(attribute)){
AssertionImpl assertion = (AssertionImpl) attribute; AssertionImpl assertion = (AssertionImpl) attribute;
Field[] fields = assertion.getClass().getDeclaredFields(); // Field[] fields = assertion.getClass().getDeclaredFields();
/*for (Field field : fields) { /*for (Field field : fields) {
System.out.println(field); System.out.println(field);
}*/ }*/
//获取用户信息中的公开属性 //获取用户信息中的公开属性
Map<String,Object> map = assertion.getPrincipal().getAttributes(); Map<String,Object> map = assertion.getPrincipal().getAttributes();
//获取用户信息中公开的Attributes部分 //获取用户信息中公开的Attributes部分
map.get("user_name"); //获取登录用户姓名,默认可获取 Object userName = map.get("user_name");//获取登录用户姓名,默认可获取
map.get("uid"); //获取登录用户id,需接入应用配置方可获取 Object uid = map.get("uid");//获取登录用户id,需接入应用配置方可获取
System.out.println(userName +"-"+uid);
} }
String token = request.getHeader("token"); String token = request.getHeader("token");

@ -1,3 +1,4 @@
/*
package com.yipin.liuwanr.filter; package com.yipin.liuwanr.filter;
@ -8,7 +9,7 @@ import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebInitParam;
@Order(value = 0) @Order(value = 0)
@WebFilter(filterName = "CAS Authentication Filter", urlPatterns = "/*", initParams = { @WebFilter(filterName = "CAS Authentication Filter", urlPatterns = "/userInfo/adminLogins", initParams = {
@WebInitParam(name = "casServerLoginUrl", value = "https://idas.uestc.edu.cn"), @WebInitParam(name = "casServerLoginUrl", value = "https://idas.uestc.edu.cn"),
@WebInitParam(name = "serverName", value = "http://222.197.183.3:8090") @WebInitParam(name = "serverName", value = "http://222.197.183.3:8090")
}) })
@ -16,3 +17,4 @@ import javax.servlet.annotation.WebInitParam;
public class CasFilter extends AuthenticationFilter { public class CasFilter extends AuthenticationFilter {
} }
*/

@ -1,12 +1,7 @@
package com.yipin.liuwanr.filter; package com.yipin.liuwanr.filter;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.yipin.liuwanr.util.CASUtil; import com.yipin.liuwanr.util.CASUtil;
import org.jasig.cas.client.authentication.AttributePrincipal; import lombok.extern.slf4j.Slf4j;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -18,11 +13,9 @@ import java.util.Map;
* *
* @author weiller 2018年1月23日10:27:30 * @author weiller 2018年1月23日10:27:30
*/ */
@Slf4j
public class LocalUserInfoFilter implements Filter { public class LocalUserInfoFilter implements Filter {
Logger logger = LoggerFactory.getLogger(LocalUserInfoFilter.class);
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
@ -34,11 +27,14 @@ public class LocalUserInfoFilter implements Filter {
Map<String,Object> loginName = CASUtil.getAccountNameFromCas(httpServletRequest); Map<String,Object> loginName = CASUtil.getAccountNameFromCas(httpServletRequest);
assert loginName != null; assert loginName != null;
if(!loginName.isEmpty()){ if(!loginName.isEmpty()){
logger.info("访问者 :" +loginName.get("user_name")); log.info("访问者 :" +loginName.get("user_name"));
logger.info("访问者 :" +loginName.get("uid")); log.info("访问者 :" +loginName.get("uid"));
httpServletRequest.getSession().setAttribute("loginName", loginName.get("user_name")); httpServletRequest.getSession().setAttribute("loginName", loginName.get("user_name"));
//登录成功之后加入数据库
} }
chain.doFilter(request, response); chain.doFilter(request, response);
} }
@Override @Override

@ -1,6 +1,5 @@
package com.yipin.liuwanr.util; package com.yipin.liuwanr.util;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AbstractCasFilter; import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion; import org.jasig.cas.client.validation.Assertion;
@ -25,8 +24,6 @@ public class CASUtil {
Assertion assertion = (Assertion) request.getSession() Assertion assertion = (Assertion) request.getSession()
.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION); .getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
if(assertion!= null){ if(assertion!= null){
AttributePrincipal principal = assertion.getPrincipal();
return assertion.getAttributes(); return assertion.getAttributes();
}else return null; }else return null;

Loading…
Cancel
Save