【问题标题】:Spring Security OAuth2 Credentiais through headerSpring Security OAuth2 Credentiais 通过标头
【发布时间】:2019-11-12 02:36:12
【问题描述】:

好吧,我正在使用 Spring Security OAuth 项目创建一个授权服务器,这是我的配置器类:

@Configuration
public class AuthConfig extends AuthorizationServerConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    public AuthConfig(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("clientid")
                .secret(passwordEncoder().encode("secret"))
                .authorizedGrantTypes("authorization_code", "client_credentials", "password")
                .scopes("myscope")
                .redirectUris("http://localhost:8080/oauth/login/client-app");
    }

    /**
     * Precisamos para uso do Password Flow
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager).
                tokenStore(tokenStore())
                .accessTokenConverter(accessTokenConverter());
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }


    @Bean
    TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean
    JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey("ABC");
        return jwtAccessTokenConverter;
    }
}

好吧,当我尝试检索访问令牌时,我需要通过标头传递“clientid”和“secret”,就像这样(返回 JWT 令牌效果很好):

curl clientid:secret@localhost:8080/oauth/token -dgrant_type=client_credentials -dscope=myscope

但如果我试试这个:

 curl localhost:8080/oauth/token -dgrant_type=client_credentials -dscope=transferir-valores -dclient_id=clientid -dclient_secret=secret

我收到未经授权的消息。

【问题讨论】:

    标签: spring-security-oauth2


    【解决方案1】:

    第二个请求不正确,这就是您收到未经授权的消息的原因。 查看specification的以下摘录:

    拥有客户端密码的客户端可以使用 HTTP Basic [RFC2617] 中定义的身份验证方案,用于与 授权服务器。客户端标识符使用 "应用程序/x-www-form-urlencoded"

    或者,授权服务器可以支持在请求正文中包含客户端凭据。

    不推荐使用这两个参数在请求正文中包含客户端凭据

    建议将客户端凭据作为基本身份验证发送(将凭据编码为 base64 并将其添加到标头)。像这样的:

    curl -X POST \ http://localhost:8443/auth-service/oauth/token \ -H 'Authorization: Basic VEVTVF9TRVJWSUNFOnBhc3N3b3Jk' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d grant_type=client_credentials

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 2019-12-04
      • 2019-09-25
      • 2020-03-13
      • 1970-01-01
      • 2019-04-29
      • 2017-09-11
      • 2019-04-28
      • 2015-11-14
      相关资源
      最近更新 更多