3

Just the mere existence of an empty WebSecurityConfigurerAdapter ruins my app's OAuth2. I get

$ 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

When I expect

$ 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"}

I have to comment out the ENTIRE class in order for the Oauth2 to work. Even just commenting configure method doesn't work. Why?

@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, but it didn't print out any useful information.

Chloe
  • 25,162
  • 40
  • 190
  • 357

1 Answers1

0

I threw away @EnableWebSecurity and WebSecurityConfigurerAdapter which just totally breaks the app. I thought they were required to get access to HttpSecurity which I thought I needed. I discovered this simple new class will solve the problem.

@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();   
    }
Chloe
  • 25,162
  • 40
  • 190
  • 357