[英]Spring Boot Security with Basic Auth and OAuth Order Issue
我正在嘗試實現一個簡單的彈簧啟動項目。 我得到了幾個REST-Endpoints,我必須以不同的方式保護它們。 一個必須由Basic Auth保護,另一個使用OAuth保護,另一個使用自定義安全實現。
REST的端點:
從教程中,我知道我要設置安全適配器的順序。 我的第一個目的是以十步為單位設置順序(例如@Order(10)
,@ @Order(20)
),以防我需要在其間添加其他安全過濾器。 通過這樣做,我調查了以下行為:
@Order(10)
添加基本身份驗證過濾器和使用@Order(20)
OAuth過濾器,則只有OAuth過濾器有效。 @Order(1)
或@Order(2)
添加基本auth過濾器和使用@Order(4)
OAuth過濾器,則兩個過濾器都可以正常工作。 @Order(3)
添加一個過濾器,我收到一個錯誤,說明訂單3已經在使用中,並且無法配置兩次。 因此,有一個默認的spring安全適配器(或其他)具有默認順序3.我想我通過添加@EnableWebSecurity
禁用每個默認的spring安全行為。 在我沒有通過谷歌找到答案后,我的問題是:
WebSecurityConfig:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Order(10)
@Configuration
public class BasicAuthConfig extends WebSecurityConfigurerAdapter {
@Value("${security.user.password}")
private String password;
@Value("${security.user.name}")
private String username;
private static final String ROLE_ADMIN = "ADMIN";
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(username).password(password).roles(ROLE_ADMIN);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.requestMatchers().antMatchers("/basic/**", "/") //
.and().authorizeRequests().anyRequest().authenticated() //
.and().httpBasic();
}
}
@Order(20)
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class Oauth2ServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
System.out.println("Filter called");
// @formatter:off
http.csrf().disable();
http.authorizeRequests().antMatchers("/application/**").authenticated()
// .antMatchers(GET, "/application/secure").authenticated()
.anyRequest().authenticated();
// @formatter:on
}
// offline token validator
}
這是一個古老的問題,但如果有人仍然想知道問題是什么,這是我的觀察:
@EnableResourceServer
導入ResourceServerConfiguration
,其順序為3。 order 3
資源服務器配置器之前添加2個以上的過濾器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.