繁体   English   中英

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

[英]Spring Boot 2 and OAuth2 client-credentials unsupported grant type

我想使用组件中包含的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可以与一个实例一起使用,如果一个人想要创建多个应用程序实例,最好的替代方法是什么?

当您将用户存储在内存中时,您将以纯文本格式提供密码,然后当您尝试从DelegatingPasswordEncoder检索编码器以验证找不到密码时。

您可以尝试以下更改,以便通过添加{noop}覆盖密码编码

@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("{noop}somePass") //change here
            .refreshTokenValiditySeconds(24*3600);
}

并在WebSecurityConfigurer中更改您的用户的密码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM