session管理
session超時處理
直接在配置文件配置
server:
servlet:
session:
timeout: 10
SpringBoot的默認的session過期時間最少爲1分鐘。session失效跳轉地址,在security配置中進行配置。
session併發控制(一個賬號只在一個平臺登錄)
在chrom登錄用戶,再在火狐瀏覽器登錄時,會頂掉在chrom登錄的賬號。
在security的權限配置中,調用HttpSecurity的 maximumSessions的方法進行最大session數設置。
通過expiredSessionStrategy方法進行失效處理。
maximumSessions(1)
.expiredSessionStrategy(new ImoocExpiredSessionStrategy())
public class ImoocExpiredSessionStrategy implements SessionInformationExpiredStrategy {
@Override
public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
event.getResponse().setContentType("application/json;charset=UTF-8");
event.getResponse().getWriter().write("併發登錄!");
}
}
當用戶已經在chrom登錄時,不希望在別的瀏覽器登錄。
可以調用.maxSessionsPreventsLogin(true)
方法。
集羣session管理
在互聯網應用中,將集羣應用的的session提取出來,放在一個第三方的數據庫庫中比如:REDIS\MONGO\jdbc
1、配置session管理工具
spring:
session:
store-type: REDIS