---
title: "spring session 原理"
author: "eric_weitm"
date: "2021-01-26T15:22:33Z"
tags: []
categories: ["互联网"]
slug: "spring-session"
draft: false
lastmod: "2021-01-26T15:24:09Z"
id: 2518924
url: "https://eric-weitm.iteye.com/blog/2518924"
---

spring-session-core

1、抽象注解、session生命周期的事件、过期、rememberMe

2、借助WebApplicationInitializer 接入app初始化的逻辑

3、抽象Session、SessionRepository、MapSession

4、web socket的支持

5、DefaultCookieSerializer 通过cookie来传输sessionid

6、HttpSessionAdapter 修改了标准servlet的HttpSession

spring-session-data-redis：redis做后端存储

核心类是RedisOperationsSessionRepository

 

使用header传递session：

 @Bean

    public HeaderHttpSessionIdResolver headerHttpSessionIdResolver() {

        return new HeaderHttpSessionIdResolver("SESSION");//设置session名称

    }

 

 

原理：

1、WebApplicationInitializer.onStartup(ServletContext servletContext) 

注入SessionRepositoryFilter

2、SessionRepositoryFilter重写SessionRepositoryRequestWrapper和

SessionRepositoryResponseWrapper，

使得标准的getSession()变成hack过的HttpSessionAdapter

3、HttpSessionAdapter内部重写了HttpSession相关的所有方法

概括：

启动--filter--getSession--request.getSession().getAttribute