代码之家  ›  专栏  ›  技术社区  ›  Chloe

Spring引导安全OAuth2:WebSecurityConfigureAdapter:302重定向到/错误

  •  2
  • Chloe  · 技术社区  · 6 年前

    只是空虚的存在 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();
        }
    
    }
    

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Chloe    6 年前

    我扔掉了 @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();   
        }