Spring Boot Security OAuth2:WebSecurityConfigurerAdapter:302重定向到/错误

时间:2018-05-25 02:28:06

标签: java spring-boot spring-security-oauth2

仅仅存在一个空的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"}

我必须注释掉 ENTIRE 类才能使Oauth2正常工作。即使只是评论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();
    }

}

I learned how to add security logging,但it didn't print out any useful information

1 个答案:

答案 0 :(得分:0)

我扔掉了@EnableWebSecurityWebSecurityConfigurerAdapter,这完全打破了应用。我以为他们需要访问我认为我需要的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();   
    }
相关问题