commit
8bb39a6431
11 changed files with 336 additions and 5 deletions
@ -0,0 +1,123 @@ |
|||||||
|
package com.yipin.liuwanr.config; |
||||||
|
|
||||||
|
import com.yipin.liuwanr.filter.LocalUserInfoFilter; |
||||||
|
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.AssertionThreadLocalFilter; |
||||||
|
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter; |
||||||
|
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter; |
||||||
|
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; |
||||||
|
|
||||||
|
/** |
||||||
|
* cas过滤器配置 |
||||||
|
*/ |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class CasFilterConfig { |
||||||
|
|
||||||
|
private static final String CAS_URL= "https://idas.uestc.edu.cn/authserver"; |
||||||
|
|
||||||
|
private static final String APP_URL= "http://222.197.183.3:8090"; |
||||||
|
|
||||||
|
@Bean |
||||||
|
public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> servletListenerRegistrationBean(){ |
||||||
|
ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>(); |
||||||
|
listenerRegistrationBean.setListener(new SingleSignOutHttpSessionListener()); |
||||||
|
listenerRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE); |
||||||
|
return listenerRegistrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 单点登录退出 |
||||||
|
* @return registrationBean |
||||||
|
*/ |
||||||
|
@Bean |
||||||
|
public FilterRegistrationBean<SingleSignOutFilter> singleSignOutFilter(){ |
||||||
|
FilterRegistrationBean<SingleSignOutFilter> registrationBean = new FilterRegistrationBean<>(); |
||||||
|
registrationBean.setFilter(new SingleSignOutFilter()); |
||||||
|
registrationBean.addUrlPatterns("/*"); |
||||||
|
registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL ); |
||||||
|
registrationBean.setName("CAS Single Sign Out Filter"); |
||||||
|
registrationBean.setOrder(2); |
||||||
|
return registrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 单点登录认证 |
||||||
|
* @return registrationBean |
||||||
|
*/ |
||||||
|
|
||||||
|
@Bean |
||||||
|
public FilterRegistrationBean<AuthenticationFilter> authenticationFilter(){ |
||||||
|
FilterRegistrationBean<AuthenticationFilter> registrationBean = new FilterRegistrationBean<>(); |
||||||
|
registrationBean.setFilter(new AuthenticationFilter()); |
||||||
|
registrationBean.addUrlPatterns("/*"); |
||||||
|
registrationBean.setName("CAS Filter"); |
||||||
|
registrationBean.addInitParameter("casServerLoginUrl",CAS_URL); |
||||||
|
registrationBean.addInitParameter("serverName", APP_URL ); |
||||||
|
registrationBean.setOrder(3); |
||||||
|
return registrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 单点登录校验 |
||||||
|
* @return registrationBean |
||||||
|
*/ |
||||||
|
|
||||||
|
@Bean |
||||||
|
public FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> cas20ProxyReceivingTicketValidationFilter(){ |
||||||
|
FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> registrationBean = new FilterRegistrationBean<>(); |
||||||
|
registrationBean.setFilter(new Cas20ProxyReceivingTicketValidationFilter()); |
||||||
|
registrationBean.addUrlPatterns("/*"); |
||||||
|
registrationBean.setName("CAS Validation Filter"); |
||||||
|
registrationBean.addInitParameter("casServerUrlPrefix", CAS_URL ); |
||||||
|
registrationBean.addInitParameter("serverName", APP_URL ); |
||||||
|
registrationBean.setOrder(4); |
||||||
|
return registrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 单点登录请求包装 |
||||||
|
* @return registrationBean |
||||||
|
*/ |
||||||
|
|
||||||
|
@Bean |
||||||
|
public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter(){ |
||||||
|
FilterRegistrationBean<HttpServletRequestWrapperFilter> registrationBean = new FilterRegistrationBean<>(); |
||||||
|
registrationBean.setFilter(new HttpServletRequestWrapperFilter()); |
||||||
|
registrationBean.addUrlPatterns("/*"); |
||||||
|
registrationBean.setName("CAS HttpServletRequest Wrapper Filter"); |
||||||
|
registrationBean.setOrder(5); |
||||||
|
return registrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 单点登录本地用户信息 |
||||||
|
* @return registrationBean |
||||||
|
*/ |
||||||
|
|
||||||
|
@Bean |
||||||
|
public FilterRegistrationBean<LocalUserInfoFilter> localUserInfoFilter(){ |
||||||
|
FilterRegistrationBean<LocalUserInfoFilter> registrationBean = new FilterRegistrationBean<>(); |
||||||
|
registrationBean.setFilter(new LocalUserInfoFilter()); |
||||||
|
|
||||||
|
registrationBean.addUrlPatterns("/*"); |
||||||
|
registrationBean.setName("localUserInfoFilter"); |
||||||
|
registrationBean.setOrder(6); |
||||||
|
return registrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
public FilterRegistrationBean<AssertionThreadLocalFilter> assertionThreadLocalFilter(){ |
||||||
|
FilterRegistrationBean<AssertionThreadLocalFilter> registrationBean |
||||||
|
= new FilterRegistrationBean<>(); |
||||||
|
registrationBean.setFilter(new AssertionThreadLocalFilter()); |
||||||
|
return registrationBean; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
package com.yipin.liuwanr.filter; |
||||||
|
|
||||||
|
|
||||||
|
import org.jasig.cas.client.authentication.AuthenticationFilter; |
||||||
|
import org.springframework.core.annotation.Order; |
||||||
|
|
||||||
|
import javax.servlet.annotation.WebFilter; |
||||||
|
import javax.servlet.annotation.WebInitParam; |
||||||
|
|
||||||
|
@Order(value = 0) |
||||||
|
@WebFilter(filterName = "CAS Authentication Filter", urlPatterns = "/*", initParams = { |
||||||
|
@WebInitParam(name = "casServerLoginUrl", value = "https://idas.uestc.edu.cn"), |
||||||
|
@WebInitParam(name = "serverName", value = "http://222.197.183.3:8090") |
||||||
|
}) |
||||||
|
//自定义一个继承CAS过滤器的过滤器,不用具体实现也可以生效
|
||||||
|
public class CasFilter extends AuthenticationFilter { |
||||||
|
|
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
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 javax.servlet.*; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* 本地用户信息过滤器 |
||||||
|
* |
||||||
|
* @author weiller 2018年1月23日10:27:30 |
||||||
|
*/ |
||||||
|
|
||||||
|
public class LocalUserInfoFilter implements Filter { |
||||||
|
|
||||||
|
Logger logger = LoggerFactory.getLogger(LocalUserInfoFilter.class); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void init(FilterConfig filterConfig) throws ServletException { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
||||||
|
HttpServletRequest httpServletRequest = (HttpServletRequest)request; |
||||||
|
Map<String,Object> loginName = CASUtil.getAccountNameFromCas(httpServletRequest); |
||||||
|
assert loginName != null; |
||||||
|
if(!loginName.isEmpty()){ |
||||||
|
logger.info("访问者 :" +loginName.get("user_name")); |
||||||
|
logger.info("访问者 :" +loginName.get("uid")); |
||||||
|
httpServletRequest.getSession().setAttribute("loginName", loginName.get("user_name")); |
||||||
|
} |
||||||
|
chain.doFilter(request, response); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void destroy() { |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,34 @@ |
|||||||
|
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; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* cas client常用工具类 |
||||||
|
* |
||||||
|
* @author weiller |
||||||
|
*/ |
||||||
|
|
||||||
|
public class CASUtil { |
||||||
|
|
||||||
|
/** |
||||||
|
* 从cas中获取用户名 |
||||||
|
* |
||||||
|
* @param request |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static Map<String, Object> getAccountNameFromCas(HttpServletRequest request) { |
||||||
|
Assertion assertion = (Assertion) request.getSession() |
||||||
|
.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION); |
||||||
|
if(assertion!= null){ |
||||||
|
AttributePrincipal principal = assertion.getPrincipal(); |
||||||
|
|
||||||
|
return assertion.getAttributes(); |
||||||
|
}else return null; |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,35 @@ |
|||||||
|
package com.yipin.liuwanr.util; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate; |
||||||
|
import org.springframework.data.redis.core.ValueOperations; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
|
||||||
|
@Component |
||||||
|
public class UserIdUtils { |
||||||
|
@Autowired |
||||||
|
StringRedisTemplate redisTemplate; |
||||||
|
@Autowired |
||||||
|
HttpServletRequest request; |
||||||
|
public Integer getUserIdByToken(){ |
||||||
|
String token = request.getHeader("token"); |
||||||
|
if (StringUtils.isEmpty(token)){ |
||||||
|
return 0; |
||||||
|
} |
||||||
|
ValueOperations<String, String> ops = redisTemplate.opsForValue(); |
||||||
|
Object loginStatus = ops.get(token); |
||||||
|
if (StringUtils.isEmpty(loginStatus)){ |
||||||
|
return 0; |
||||||
|
}else { |
||||||
|
String userId = request.getHeader("userId"); |
||||||
|
if (!StringUtils.isEmpty(userId)){ |
||||||
|
return Integer.parseInt(userId); |
||||||
|
}else { |
||||||
|
return Integer.parseInt(loginStatus.toString()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue