如何在 Spring OAuth2 中仅通过 client_id 和 secret 进行授权?

Posted

技术标签:

【中文标题】如何在 Spring OAuth2 中仅通过 client_id 和 secret 进行授权?【英文标题】:How authorize in Spring OAuth2 by client_id and secret only? 【发布时间】:2018-07-29 20:13:52 【问题描述】:

现在我使用的是下一个配置:

@Configuration
@EnableAuthorizationServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class OAuth2Config extends AuthorizationServerConfigurerAdapter 

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception 
        clients.jdbc(mAppConfig.dataSource())
                .withClient("virto")
                .secret("secret")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token");
    

它需要发送client_id, client_secret, username, password...但我需要为我的受信任服务器提供access_token,只有client_idclient_secret... 我该怎么做?有可能吗?

【问题讨论】:

OAuth2 中有一些流,例如password flow(需要client_id、client_secret、用户名和密码),client_credentials 流只需要client_id and client_secret。您需要研究如何在 Spring OAuth 应用程序中启用 client_credentials 流。有关更多信息,请参阅此***.com/questions/37534073/… SO 帖子。 【参考方案1】:

您需要为客户端配置 client_credentials 授权

@Configuration
@EnableAuthorizationServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class OAuth2Config extends AuthorizationServerConfigurerAdapter 

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception 
        clients.jdbc(mAppConfig.dataSource())
                .withClient("virto")
                .secret("secret")
                .authorizedGrantTypes("client_credentials");
    

要获取令牌,您需要发送一个 post 请求,并使用 base 64 编码的凭据。

POST http://localhost:8080/paymentiq/oauth/token?grant_type=client_credentials -h 'Authorization: [clientId:secret]'

【讨论】:

以上是关于如何在 Spring OAuth2 中仅通过 client_id 和 secret 进行授权?的主要内容,如果未能解决你的问题,请参考以下文章