Maven依赖
<dependency> |
Java Configuration
WebInitializer.javapublic class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{HttpSessionConfiguration.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}
protected String[] getServletMappings() {
return new String[0];
}
}
SpringSessionInitializer.javapublic class SpringSessionInitializer extends AbstractHttpSessionApplicationInitializer {
SpringSessionInitializer() {
super(HttpSessionConfiguration.class);
}
}
HttpSessionConfiguration.java
"token") (redisNamespace =
public class HttpSessionConfiguration {
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory("pmusrds02.la.gomo.com", 6379);
}
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("JSESSIONID"); // <1>
serializer.setCookiePath("/"); // <2>
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$"); // <3>
return serializer;
}
public FilterRegistrationBean sessionFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SessionFilter());//添加过滤器
registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径
registration.setName("SessionFilter");//设置优先级
registration.setOrder(1);//设置优先级
return registration;
}
public class SessionFilter implements Filter {
public void init(FilterConfig filterConfig) {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
if(((HttpServletRequest) servletRequest).getSession().getAttribute("access_token") != null) {
Cookie cookie = new Cookie("access_token", ((HttpServletRequest) servletRequest).getSession().getAttribute("access_token").toString());
((HttpServletResponse) servletResponse).addCookie(cookie);
}
}
public void destroy() {
}
}
}