当前位置:网站首页>session management
session management
2022-07-19 14:57:00 【Leisurely summer】
Spring Security It can be done with Spring Session Library with the use of , You only need to do some simple configuration to realize some functions , Such as ( Session expiration, 、 Only one account can be online at the same time 、 colony session etc. )
1、 session time out
To configure session Session timeout , The default is 30 minute , however Spring Boot The session timeout in is at least 60 second
#session Set up
# To configure session Timeout time
server.servlet.session.timeout=60
When session After a timeout , By default, jump to the login page ( Default mode )
Custom Settings session Address after timeout
Set up session Management and post failure jump address
http.sessionManagement() // Set up session management
.invalidSessionUrl("/toLoginPage") // session Invalid jump path , The default is the login page
2、 concurrency control
Concurrency control refers to the number of online users of the same account at the same time , If the number of simultaneous online accounts of the same account is set to 1 Express , This account can only have one valid login at a time , If the same account is logged in elsewhere , Then expire the last login session , That is, the later login will kick out the previous login
2.1、 Modify timeout
#session Set up
# To configure session Timeout time
server.servlet.session.timeout=600
2.2、 Set the maximum number of sessions
http.sessionManagement() // Set up session management
.invalidSessionUrl("/toLoginPage") // session The path to jump after failure
.maximumSessions(1)// session Maximum number of sessions 1 Represents that only one user can log in at a time
.maxSessionsPreventsLogin(true) // Maximum number of sessions reached , Stop logging in
.expiredUrl("/toLoginPage"); // session The path to jump after expiration 2.3、 Prevent users from logging in a second time
sessionManagement You can also configure maxSessionsPreventsLogin:boolean value , When reach maximumSessions Block login when setting the maximum number of sessions .
3、 colony session
In the actual scenario, a service will have at least two servers providing services , There will be a in front of the server nginx Load balancing , User access nginx,nginx Then decide which server to access . When a service goes down , Another server can also continue to provide services , Guarantee uninterrupted service . If we were to session Save in Web Containers ( such as tomcat) in , If a user is assigned to the server for the first time 1 You need to log in , When some access is suddenly assigned to server 2 , Because there is no user login session on server 1 on server 2 session Information , Server 2 will also let users log in again , If the user has logged in, it will feel abnormal .

The idea to solve this problem is that the session information logged in by the user can no longer be saved to Web Server , Instead, save to a separate library (redis、mongodb、jdbc etc. ) in , All servers access the same library , Get the user's information from the same library session Information , If the user logs in on the server , Save the session information to the library , The user's next request is assigned to server 2 , Server 2 checks from the Library session Does it already exist , If it exists, you don't have to log in anymore , You can access the service directly .

3.1、 Citation depends on
<!-- be based on redis Realization session share -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3.2、 Set up session Storage type
# Use redis share session
spring.session.store-type=redis
3.3、 test
- Use one of these services to log in http://localhost:8080/login
- Use another service to access any interface , Then you can access directly without logging in again
边栏推荐
猜你喜欢

1、DBMS基本概念

PCIe Cameralink signal generator (Cameralink image analog source)

Problème de la valeur maximale de la fenêtre coulissante

CF 807 E. mark and Professor Koro (weight segment tree)

Redis

抽象类与派生类

Data consistency between redis and MySQL

End repeated development and personalize the login system in twoorthree times

Comparaison de deux types de machines virtuelles

An unforgettable day in 2022 summer camp
随机推荐
常见的内置函数、可迭代对象、迭代器对象、异常捕获、异常捕获的用途、生成器对象、模块、绝对导入与相对导入、包的概念、模块
MySQL storage functions and triggers
3438. 数制转换
Practice of tDesign in vitest
定时任务,vim直接创建修改用户
1、DBMS基本概念
Domestic fpga/dsp/zynq Chip & board scheme
Read the paper: temporary graph networks for deep learning on dynamic graphs
Alibaba微服务组件Nacos注册中心
MySQL index (II)
滑動窗口最大值問題
Cilium & Hubble
Common built-in functions, iteratable objects, iterator objects, exception capture, purpose of exception capture, generator objects, modules, absolute and relative imports, package concepts, modules
天勤第九章课后习题代码
2021 national vocational college skills competition network construction and application events
FPGA(VGA协议实现)
How to quickly realize Zadig single sign on on authoring?
Display module in pyGame
见鬼,U盘空间怎么少了,原来是EFI分区搞的鬼,删除它
Labview32-bit and 64 bit compatibility