Spring Boot 使用 MongoDB 实现共享 Session

Spring Boot MongoDB Session About 3,299 words

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-mongodb</artifactId>
</dependency>

开启MongoSession

使用EnableMongoHttpSession注解开启MongoDB共享session

@Configuration
@EnableMongoHttpSession
public class HttpSessionConfig {

}

默认保存在名称为sessions的集合中,过期时间为1800秒,可配置:

@EnableMongoHttpSession(maxInactiveIntervalInSeconds = 1200, collectionName = "mongo_session")

测试Session

@RestController
@SpringBootApplication
@EnableMongoHttpSession
//@EnableMongoHttpSession(maxInactiveIntervalInSeconds = 60, collectionName = "mongo_session")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @PostMapping("/login")
    public String login(HttpSession session) {
        session.setAttribute("testKey", "testValue");
        return "login success";
    }

    @PostMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "logout success";
    }

    @GetMapping("/attr")
    public String attr(HttpSession session) {
        Object testKey = session.getAttribute("testKey");
        return testKey == null ? "null" : ((String) testKey);
    }

}

序列化Session

默认使用JdkMongoSessionConverter:标准Java序列化将session属性映射以二进制形式持久化到MongoDB

@Configuration
@EnableMongoHttpSession
public class HttpSessionConfig {
    @Bean
    public JdkMongoSessionConverter jdkMongoSessionConverter() {
        return new JdkMongoSessionConverter(Duration.ofMinutes(30));
    }
}

保存至数据库后的记录如下:

{
    "_id" : "690aeece-0c27-4b56-82fb-42df46259cf7",
    "created" : ISODate("2020-05-13T06:18:26.212Z"),
    "accessed" : ISODate("2020-05-13T06:18:30.991Z"),
    "interval" : "PT30M",
    "principal" : null,
    "expireAt" : ISODate("2020-05-13T06:48:30.991Z"),
    "attr" : { "$binary" : "rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAABdAAHdGVzdEtleXQACXRlc3RWYWx1ZXg=", "$type" : "00" }
}

可使用JacksonMongoSessionConverter:使用Jackson序列化session属性到MongoDB

重要提醒:集成了Spring Security的项目,这个转换器将注册白名单组件保证Spring Security正常运行。

@Configuration
@EnableMongoHttpSession
public class HttpSessionConfig {
    @Bean
    JacksonMongoSessionConverter mongoSessionConverter() {
        return new JacksonMongoSessionConverter();
    }
}

保存至数据库后的记录如下:

{
    "_id" : "03533d5b-eb11-475d-82c0-c927f84fd64d",
    "@class" : "org.springframework.session.data.mongo.MongoSession",
    "intervalSeconds" : 1800,
    "originalSessionId" : "03533d5b-eb11-475d-82c0-c927f84fd64d",
    "createdMillis" : NumberLong(1589351032853),
    "accessedMillis" : NumberLong(1589351032853),
    "expireAt" : ISODate("2020-05-13T06:53:52.853Z"),
    "attrs" : {
        "@class" : "java.util.HashMap",
        "testKey" : "testValue"
    },
    "principal" : null
}

代码

https://github.com/fendoudebb/learning/tree/master/java/learn-spring-boot/mongo-session

参考

https://docs.spring.io/spring-session-data-mongodb/docs/2.1.1.RELEASE/reference/htmlsingle

https://docs.spring.io/spring-session-data-mongodb/docs/current

Views: 5,148 · Posted: 2020-05-14

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh