Learning Spring Boot: (18) Session Sharing in Spring Boot

foreword

Earlier we integrated Redis into the project, and now we need to use it to do something practical. This time, in order to solve the problem of session sharing in the distributed system, the session is hosted in Redis.

text

import dependencies

In addition to the introduction in the previous article, dependenciesspring-boot-starter-data-redis are also required :spring-session

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

configure

In the system configuration file add:

spring:
  session:
    store-type: redis

And it can be found that store-typethere are several values ​​that can be set, all of which can be used as a medium for session sharing.
image

As you can see, Spring Session supports the use of Redis, Mongo, JDBC, and Hazelcast to store Sessions.

This is done.

test

image

It can be seen that the session has been taken over by shiro.

The idea of ​​spring-session implementation is : design a Filter, use HttpServletRequestWrapper, implement your own, getSession()方法and take over the work of creating and managing Session data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325632331&siteId=291194637