Spring Boot Security OAuth2:WebSecurityConfigurerAdapter:302重定向到/ error。

3
仅仅存在一个空的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起作用,我必须注释掉整个类。即使只注释掉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();
    }

}

我学会了如何添加安全日志记录,但是它没有打印任何有用的信息

1个回答

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();   
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接