[英]Spring Security : Falls back to formLogin on authentication error
我嘗試實現類似於https://stackoverflow.com/a/33608459和https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity的 SecurityConfig
我希望我的 API ( /rest/**
) 由 HttpBasic 保護,並通過 FormLogin 進行其他請求。 這很有效……只要我向 HttpBasic 提供正確的憑據。
如果我提供正確的憑據 - 它會以正常答案進行響應。
如果我提供任何憑證-它回應一個401 Unauthorized
完美- !
如果我提供了錯誤的憑據 - 它會響應302 Found
with Location: /login
最后一部分是我不想要的 - 我還想要一個401 未經授權的錯誤憑據。
http http://localhost:8081/rest/
HTTP/1.1 401
WWW-Authenticate: Basic realm="My Realm"
http -a correct:password http://localhost:8081/rest/some/api/
HTTP/1.1 200
http -a wrong:password http://localhost:8081/rest/some/api/
HTTP/1.1 302
Location: http://hive.local:8081/login WWW-Authenticate: Basic realm="My Realm"
@Configuration
@Order(1)
public static class RestSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthorizationProperties properties;
@Override protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher("/rest/**")
.authorizeRequests()
.anyRequest().hasRole("API").and()
.httpBasic()
.realmName(properties.getRealm()).and()
.formLogin().disable()
.csrf().disable();
// @formatter:on
}
}
@Configuration
@Order(2)
public static class FrontendSecurityConfig extends WebSecurityConfigurerAdapter {
@Override public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/app/**", "/webjars/**", "/static/**", "/js/**");
}
@Override protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.anyRequest().hasAnyRole("USER").and()
.formLogin();
// @formatter:on
}
}
我能夠對此有所了解。
在基本身份驗證請求失敗后重定向到表單登錄是由調度程序 servlet 在驗證憑據失敗后嘗試重定向到 URL /error
引起的。
要獲得適當的錯誤響應,您需要將/error
添加到在您的網絡安全配置中被忽略的antMatchers
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.