Home > Java > javaTutorial > body text

How SpringBoot integrates SpringSession to implement distributed login

王林
Release: 2023-05-10 20:34:04
forward
729 people have browsed it

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>
Copy after login

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>
Copy after login

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
Copy after login

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,"成功");
 
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template