diff --git a/src/main/java/com/yipin/liuwanr/config/CasFilterConfig.java b/src/main/java/com/yipin/liuwanr/config/CasFilterConfig.java index 73ec668..530e9b5 100644 --- a/src/main/java/com/yipin/liuwanr/config/CasFilterConfig.java +++ b/src/main/java/com/yipin/liuwanr/config/CasFilterConfig.java @@ -20,9 +20,10 @@ import org.springframework.core.Ordered; @Configuration public class CasFilterConfig { + //生效的cas配置 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 public ServletListenerRegistrationBean servletListenerRegistrationBean(){ @@ -40,7 +41,7 @@ public class CasFilterConfig { public FilterRegistrationBean singleSignOutFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new SingleSignOutFilter()); - registrationBean.addUrlPatterns("/*"); + registrationBean.addUrlPatterns("/userInfo/loginOut"); registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL ); registrationBean.setName("CAS Single Sign Out Filter"); registrationBean.setOrder(2); @@ -51,12 +52,11 @@ public class CasFilterConfig { * 单点登录认证 * @return registrationBean */ - @Bean public FilterRegistrationBean authenticationFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new AuthenticationFilter()); - registrationBean.addUrlPatterns("/*"); + registrationBean.addUrlPatterns("/userInfo/loginSchoolClient"); registrationBean.setName("CAS Filter"); registrationBean.addInitParameter("casServerLoginUrl",CAS_URL); registrationBean.addInitParameter("serverName", APP_URL ); @@ -73,7 +73,7 @@ public class CasFilterConfig { public FilterRegistrationBean cas20ProxyReceivingTicketValidationFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new Cas20ProxyReceivingTicketValidationFilter()); - registrationBean.addUrlPatterns("/*"); + registrationBean.addUrlPatterns("/userInfo/loginSchoolClient"); registrationBean.setName("CAS Validation Filter"); registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL ); registrationBean.addInitParameter("serverName", APP_URL ); @@ -90,7 +90,7 @@ public class CasFilterConfig { public FilterRegistrationBean httpServletRequestWrapperFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new HttpServletRequestWrapperFilter()); - registrationBean.addUrlPatterns("/*"); + registrationBean.addUrlPatterns("/userInfo/loginSchoolClient"); registrationBean.setName("CAS HttpServletRequest Wrapper Filter"); registrationBean.setOrder(5); return registrationBean; @@ -103,10 +103,11 @@ public class CasFilterConfig { @Bean public FilterRegistrationBean localUserInfoFilter(){ + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new LocalUserInfoFilter()); - registrationBean.addUrlPatterns("/*"); + registrationBean.addUrlPatterns("/userInfo/loginSchoolClient"); registrationBean.setName("localUserInfoFilter"); registrationBean.setOrder(6); return registrationBean; @@ -114,9 +115,11 @@ public class CasFilterConfig { @Bean public FilterRegistrationBean assertionThreadLocalFilter(){ + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new AssertionThreadLocalFilter()); + registrationBean.setOrder(4); return registrationBean; } diff --git a/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java index daac23f..dc5237a 100644 --- a/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java +++ b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java @@ -34,16 +34,16 @@ public class AuthInterceptor implements HandlerInterceptor { Object attribute = request.getSession().getAttribute("_const_cas_assertion_"); if (!StringUtils.isEmpty(attribute)){ AssertionImpl assertion = (AssertionImpl) attribute; - Field[] fields = assertion.getClass().getDeclaredFields(); +// Field[] fields = assertion.getClass().getDeclaredFields(); /*for (Field field : fields) { System.out.println(field); }*/ //获取用户信息中的公开属性 Map map = assertion.getPrincipal().getAttributes(); //获取用户信息中公开的Attributes部分 - map.get("user_name"); //获取登录用户姓名,默认可获取 - map.get("uid"); //获取登录用户id,需接入应用配置方可获取 - + Object userName = map.get("user_name");//获取登录用户姓名,默认可获取 + Object uid = map.get("uid");//获取登录用户id,需接入应用配置方可获取 + System.out.println(userName +"-"+uid); } String token = request.getHeader("token"); diff --git a/src/main/java/com/yipin/liuwanr/filter/CasFilter.java b/src/main/java/com/yipin/liuwanr/filter/CasFilter.java index 0b2bdb4..a165e16 100644 --- a/src/main/java/com/yipin/liuwanr/filter/CasFilter.java +++ b/src/main/java/com/yipin/liuwanr/filter/CasFilter.java @@ -1,3 +1,4 @@ +/* package com.yipin.liuwanr.filter; @@ -8,7 +9,7 @@ import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; @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 = "serverName", value = "http://222.197.183.3:8090") }) @@ -16,3 +17,4 @@ import javax.servlet.annotation.WebInitParam; public class CasFilter extends AuthenticationFilter { } +*/ diff --git a/src/main/java/com/yipin/liuwanr/filter/LocalUserInfoFilter.java b/src/main/java/com/yipin/liuwanr/filter/LocalUserInfoFilter.java index d5a53a8..dcc1085 100644 --- a/src/main/java/com/yipin/liuwanr/filter/LocalUserInfoFilter.java +++ b/src/main/java/com/yipin/liuwanr/filter/LocalUserInfoFilter.java @@ -1,12 +1,7 @@ package com.yipin.liuwanr.filter; -import com.alibaba.dubbo.common.utils.StringUtils; import com.yipin.liuwanr.util.CASUtil; -import org.jasig.cas.client.authentication.AttributePrincipal; -import org.jasig.cas.client.util.AbstractCasFilter; -import org.jasig.cas.client.validation.Assertion; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; @@ -18,11 +13,9 @@ import java.util.Map; * * @author weiller 2018年1月23日10:27:30 */ - +@Slf4j public class LocalUserInfoFilter implements Filter { - Logger logger = LoggerFactory.getLogger(LocalUserInfoFilter.class); - @Override public void init(FilterConfig filterConfig) throws ServletException { @@ -34,11 +27,14 @@ public class LocalUserInfoFilter implements Filter { Map loginName = CASUtil.getAccountNameFromCas(httpServletRequest); assert loginName != null; if(!loginName.isEmpty()){ - logger.info("访问者 :" +loginName.get("user_name")); - logger.info("访问者 :" +loginName.get("uid")); + log.info("访问者 :" +loginName.get("user_name")); + log.info("访问者 :" +loginName.get("uid")); httpServletRequest.getSession().setAttribute("loginName", loginName.get("user_name")); + //登录成功之后加入数据库 + } chain.doFilter(request, response); + } @Override diff --git a/src/main/java/com/yipin/liuwanr/util/CASUtil.java b/src/main/java/com/yipin/liuwanr/util/CASUtil.java index f1c3eb7..ecc86da 100644 --- a/src/main/java/com/yipin/liuwanr/util/CASUtil.java +++ b/src/main/java/com/yipin/liuwanr/util/CASUtil.java @@ -1,6 +1,5 @@ package com.yipin.liuwanr.util; -import org.jasig.cas.client.authentication.AttributePrincipal; import org.jasig.cas.client.util.AbstractCasFilter; import org.jasig.cas.client.validation.Assertion; @@ -25,8 +24,6 @@ public class CASUtil { Assertion assertion = (Assertion) request.getSession() .getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION); if(assertion!= null){ - AttributePrincipal principal = assertion.getPrincipal(); - return assertion.getAttributes(); }else return null;