例如兩個網域:
aaa.yupi.com
SpringBoot整合SpringSession實現分散式登入
引入redis,能夠操作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>
引入spring-session 和redis 的整合,使得自動將session 儲存到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>
修改spring-session 儲存配置spring.session.store-type
預設是none,表示儲存在單一台伺服器store-type: redis,表示從redis 讀寫sessionredis: host: localhost port: 6379 session: timeout: 60 store-type: redis
效果:
/** * @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,"成功"); } }
以上是SpringBoot怎麼整合SpringSession實現分散式登錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!