Spring Boot OAuth2 implicit flow + form login and Request method 'POST' not supported error

烂漫一生 提交于 2019-12-06 15:00:08

I had this same problem today, your answer didn't help me, but I found a solution to my problem.

Hopefully this helps someone else in the same position.

The problem for my app is that I'm using Basic Auth to cover most of my web application's protected resources, but I'm using OAuth2 in order to protect a public API that I'm allowing people to connect to.

So this caused my application to have two separate public void configure(HttpSecurity http) methods in two separate configuration files.

The solution was to add an Order annotation to the configuration classes.

So my main configuration class that used Basic Auth had Order(1) assigned to it, and the ResourceServerConfigurerAdapter had an Order(2) annotation assigned to it.

For example:

@EnableWebSecurity
@Order(1)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
  /// ... my normal configuration information
  protected void configure(HttpSecurity http) {
    // my basic auth config
  }
}

@Configuration
@Order(2)
public class OAuth2ServerConfig
{
  @Configuration
  @EnableResourceServer
  @Order(3)
  protected static class Oauth2ServerConfig extends ResourceServerConfigurerAdapter
  {
    @Override
    public void configure(HttpSecurity http) throws Exception
    {
      http.authorizeRequests()
      .antMatchers("/oauth/**").permitAll()
      .antMatchers("/api/v1/**").access("#oauth2.hasScope('read')");
    }
  }


  @Configuration
  @EnableAuthorizationServer
  protected static class AuthorizationServer extends AuthorizationServerConfigurerAdapter
  {
    // authorization server settings
  }
}

It was my issue with a wrong configuration of ResourceServer.

With a following configuration everything is working fine:

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Autowired
    private ResourceServerTokenServices tokenService;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        // @formatter:off
        resources           
            .resourceId(RESOURCE_ID)
            .tokenServices(tokenService);
        // @formatter:on
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http                
            .antMatcher("/api/**" )
            .authorizeRequests().anyRequest().authenticated()
            .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(STATELESS);
        // @formatter:on
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!