SpringBoot-----Security安全機制的sessions配置策略
阿新 • • 發佈:2018-12-04
1、配置security.sessions策略
#安全配置
security:
sessions: stateless
basic:
enabled: true #啟用SpringSecurity的安全配置
user:
name: wendy #認證使用者名稱
password: wendy1 #認證密碼
role: #授權
- USER
2、security.sessions策略如下:
always:儲存session狀態(每次會話都儲存,可能會導致記憶體溢位【Always create an {@link HttpSession}】)
never:不會建立HttpSession,但是會使用已經存在的HttpSession[Spring Security will never create an {@link HttpSession}]
if_required:僅在需要HttpSession建立【Spring Security will only create an {@link HttpSession} if required】
stateless:不會儲存session狀態【 Spring Security will never create an {@link HttpSession} and it will never use it
* to obtain the {@link SecurityContext}】
注意:stateless策略推薦使用,也是預設配置
3、具體跟檢視原始碼SecurityProperties.java的配置項
/**
* Session creation policy (always, never, if_required, stateless).
*/
private SessionCreationPolicy sessions = SessionCreationPolicy.STATELESS;
/* * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.security.config.http; import javax.servlet.http.HttpSession; import org.springframework.security.core.context.SecurityContext; /** * Specifies the various session creation policies for Spring Security. * * @author Luke Taylor * @since 3.1 */ public enum SessionCreationPolicy { /** Always create an {@link HttpSession} */ ALWAYS, /** * Spring Security will never create an {@link HttpSession}, but will use the * {@link HttpSession} if it already exists */ NEVER, /** Spring Security will only create an {@link HttpSession} if required */ IF_REQUIRED, /** * Spring Security will never create an {@link HttpSession} and it will never use it * to obtain the {@link SecurityContext} */ STATELESS }