Home  >  Article  >  Java  >  How SpringBoot integrates SpringSession to implement distributed login

How SpringBoot integrates SpringSession to implement distributed login

王林
王林forward
2023-05-10 20:34:04732browse

Session Sharing

For example, two domain names:

  • aaa.yupi.com

  • bbb.yupi.com

  • If you want to share cookies, you can plant a higher-level public domain name, such as yupi.com

Why server After A logs in, the request is sent to server B. Does the user not know him?

The user logs in at A, so the session (user login information) exists on A

As a result, when requesting B, B does not have user information, so it does not recognize it.

How SpringBoot integrates SpringSession to implement distributed login

Solution

Shared storage instead of putting data in the memory of a single server

How SpringBoot integrates SpringSession to implement distributed login

SpringBoot integrates SpringSession to implement distributed login

Introduces redis to operate redis:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.6.4</version>
</dependency>

Introduces the integration of spring-session and redis to enable automatic Store the session in redis:

<!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>2.6.3</version>
</dependency>

Modify the spring-session storage configuration spring.session.store-type

The default is none, which means it is stored in a single Server

store-type: redis, indicating reading and writing sessions from redis

  redis:
    host: localhost
    port: 6379
  session:
    timeout: 60
    store-type: redis

Effect:

/**
 * @author 刘宇浩
 */
@RestController
@RequestMapping("/session")
public class SessionController {

    public static final String key = "USERLOGINSTATE";

    @GetMapping("/set")
    public Result setSession(HttpServletRequest request) {
        User user = new User();
        user.setClassName("21软件3");
        user.setName("lyl");
        request.getSession().setAttribute(key, user);
        return ResultGenerator.genSuccessResult(200, "成功");
    }
    @GetMapping("/get")
    public Result getSession(HttpServletRequest request){
        User userloginstate = (User)request.getSession().getAttribute(key);
        System.out.println(userloginstate.getName());
        System.out.println(userloginstate.getClassName());
        return ResultGenerator.genSuccessResult(200,"成功");
 
    }
}

The above is the detailed content of How SpringBoot integrates SpringSession to implement distributed login. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete