Spring Boot 2和OAuth2客户端凭据不受支持的授权类型

时间:2018-05-30 09:30:07

标签: java spring spring-boot spring-security

我想使用我的组件中包含的Spring Security的Resource Server和Authorization Server来保护我的应用。 所需的流程包括仅使用客户端凭证授予类型并将client_id与client_secret一起作为base64头传递,在命中oauth/token端点后应该返回令牌以获取更多请求。我还在POST请求参数

中包含grant_type:client-credentials

目前我收到错误: "Full authentication is required to access this resource"。 奇怪的是,尽管我的配置Spring仍然生成随机安全密码,可以在控制台日志中看到。

这是我第一次接触Spring Security,所以也许我错过了什么?

以下是我的配置:

AuthorizationServerConfig

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends 
AuthorizationServerConfigurerAdapter {

@Override
public void configure(final AuthorizationServerSecurityConfigurer security) {
    security
            .tokenKeyAccess("permitAll()")
            .checkTokenAccess("isAuthenticated()");
}

@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
    clients
            .inMemory()
            .withClient("some-client")
            .authorizedGrantTypes("client-credentials")
            .authorities("ROLE_CLIENT")
            .scopes("read", "write", "trust")
            .accessTokenValiditySeconds(3600)
            .secret("somePass")
            .refreshTokenValiditySeconds(24*3600);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints
            .tokenStore(tokenStore())
            .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}

@Bean
public TokenStore tokenStore() {
    return new InMemoryTokenStore();
}     
}

ResourceServerConfig:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

@Override
public void configure(final HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/oauth/token", "/oauth/authorize", "/oauth/confirm_access").permitAll()
            .antMatchers("/**").authenticated()
            .and().httpBasic().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}

我正在使用Spring Boot 2.0.1.RELEASE和Spring Security OAuth2 2.0.14.RELEASE。 在我的情况下,使用InMemoryTokenStore它将使用一个实例,如果想要创建多个app实例,那么最好的替代是什么?

1 个答案:

答案 0 :(得分:0)

当您将用户存储在内存中时,您将以纯文本形式提供密码,然后当您尝试从java.lang.IllegalStateException: Task is not yet complete检索编码器以验证密码时,它无法找到密码。

您可以尝试以下更改,以便通过添加DelegatingPasswordEncoder

来覆盖密码编码
{noop}

并且还在WebSecurityConfigurer中更改您的用户,为密码做同样的事情。

相关问题