[英]Spring Boot Security OAuth2: WebSecurityConfigurerAdapter: 302 Redirect to /error
空WebSecurityConfigurerAdapter
存在WebSecurityConfigurerAdapter
破壞了我的應用程序的OAuth2。 我懂了
$ curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 27f9e2b7-4441-4c03-acdb-7e7dc358f783" -d '{"apiKey": "key", "tag": "tag"}' localhost:8080/isTagAvailable
HTTP/1.1 302
Location: http://localhost:8080/error
當我期望
$ curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 27f9e2b7-4441-4c03-acdb-7e7dc358f783" -d '{"apiKey": "key", "tag": "tag"}' localhost:8080/isTagAvailable
HTTP/1.1 401
{"error":"invalid_token","error_description":"Invalid access token: 27f9e2b7-4441-4c03-acdb-7e7dc358f783"}
為了使Oauth2能夠正常工作,我必須注釋掉ENTIRE類。 即使只是注釋configure
方法也不起作用。 為什么?
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/robots.txt").permitAll()
.and()
.authorizeRequests()
.antMatchers("/isTagAvailable").hasRole("USER")
// .anyRequest().authenticated()
.and()
.httpBasic().disable();
}
}
我學習了如何添加安全日志 ,但是它沒有輸出任何有用的信息 。
我扔掉了@EnableWebSecurity
和WebSecurityConfigurerAdapter
,這完全破壞了應用程序。 我認為他們需要訪問我認為需要的HttpSecurity
。 我發現這個簡單的新類將解決問題。
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
String[] ignoredPaths = new String[]{...};
@Override
public void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers(ignoredPaths).permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.