[英]Spring Security 4 with custom provider and handlers not caching the url before authentication
[英]Spring Security caching my authentication
我剛剛在我的 Spring Boot 項目類路徑中添加了 Spring Security。 我沒有做 Java 配置或 XML 配置。
問題是,當我向我的資源localhost:8080/users發送請求時,我的第一個請求通常會通過身份驗證(通過基本身份驗證) ,但后續請求不需要任何身份驗證標頭。 即使我重新啟動服務器,請求仍在進行身份驗證而無需輸入任何憑據。
我想關閉這個“緩存”。
我嘗試了很多客戶。 Postman, SOAP-UI, browsers..Already read this , but not works
您必須將會話創建策略設置為 STATELESS。 否則 Spring 安全將使用 cookie。
(您可以在 Postman 中發送按鈕下方的 cookie 菜單中刪除 cookie。)
示例配置:
@Configuration
@EnableWebSecurity(debug = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(
SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
...
}
我的開箱即用的 Spring Actuator 遇到了這個問題。
我不得不添加: .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
到我的ActuatorSecurityConfig
類。
package com.foo;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.context.ShutdownEndpoint;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
.denyAll()
.requestMatchers(EndpointRequest.toAnyEndpoint())
.hasRole("ACTUATOR_ADMIN")
.requestMatchers(PathRequest.toStaticResources().atCommonLocations())
.permitAll()
.antMatchers("/foo/**")
.permitAll()
.antMatchers("/**")
.authenticated()
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
對於那些在 spring xml 中配置安全性的幸運兒,這也激活了無狀態安全性:
<http create-session="stateless">...</http>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.