|
|
springsession是什么
实现分布式session管理
为什么要使用springsession
spring全家桶,不想自己实现分布式session管理可以使用
添加依赖
|
|
添加配置
|
|
测试
原理分析
SpringHttpSession
第一步查看@EnableRedisHttpSession
12345678@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)@Target({ java.lang.annotation.ElementType.TYPE })@Documented@Import(RedisHttpSessionConfiguration.class)@Configurationpublic @interface EnableRedisHttpSession {...}进二步查看RedisHttpSessionConfiguration
123456@Configuration@EnableSchedulingpublic class RedisHttpSessionConfiguration extends SpringHttpSessionConfigurationimplements EmbeddedValueResolverAware, ImportAware {...}第三步发现其集成自SpringHttpSessionConfiguration查看
|
|
- 继续查看SessionRepositoryFilter1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556public class SessionRepositoryFilter<S extends ExpiringSession>extends OncePerRequestFilter {...}继承自OncePerRequestFilterabstract class OncePerRequestFilter implements Filter {public final void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws ServletException, IOException {调用doFilterInternal由SessionRepositoryFilter实现@Overrideprotected void doFilterInternal(HttpServletRequest request,HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {request.setAttribute(SESSION_REPOSITORY_ATTR, this.sessionRepository);SessionRepositoryRequestWrapper wrappedRequest = new SessionRepositoryRequestWrapper(request, response, this.servletContext);SessionRepositoryResponseWrapper wrappedResponse = new SessionRepositoryResponseWrapper(wrappedRequest, response);HttpServletRequest strategyRequest = this.httpSessionStrategy.wrapRequest(wrappedRequest, wrappedResponse);HttpServletResponse strategyResponse = this.httpSessionStrategy.wrapResponse(wrappedRequest, wrappedResponse);try {filterChain.doFilter(strategyRequest, strategyResponse);}finally {wrappedRequest.commitSession();}}包装请求,响应对象根据策略处理包装请求对象最后wrappedRequest.commitSession();HttpSessionWrapper wrappedSession = getCurrentSession();if (wrappedSession == null) {if (isInvalidateClientSession()) {SessionRepositoryFilter.this.httpSessionStrategy.onInvalidateSession(this, this.response);}}else {S session = wrappedSession.getSession();SessionRepositoryFilter.this.sessionRepository.save(session);if (!isRequestedSessionIdValid()|| !session.getId().equals(getRequestedSessionId())) {SessionRepositoryFilter.this.httpSessionStrategy.onNewSession(session,this, this.response);}}这就是最终处理,就不做详细解释了
几张图
SessionRepository的实现类