小编典典

将多个客户端添加到Spring OAuth2 Auth Server

java

我有Spring OAuth授权服务器,我想增加对一个以上client(id)的支持。我这样配置客户端:

clients
            .inMemory().withClient(client).secret(clientSecret)
            .resourceIds(resourceId)
            .authorizedGrantTypes("client_credentials", "password", "refresh_token", "implicit", "authorization_code")
            .authorities("ROLE_USER")
            .scopes("read", "write")
            .autoApprove(true)
            .and()
            .inMemory().withClient("acme").secret("acmesecret")
            .resourceIds(resourceId)
            .authorizedGrantTypes("client_credentials", "password", "refresh_token", "implicit", "authorization_code")
            .authorities("ROLE_USER_ACME")
            .scopes("read", "write")
            .autoApprove(true);

我可以使用第一个客户端获取访问令牌,但是尝试使用第二个客户端获取访问令牌时出现此错误:

{
  "timestamp": 1456822249638,
  "status": 401,
  "error": "Unauthorized",
  "message": "Bad credentials",
  "path": "/oauth/token"
}

是否可以添加多个客户端,以及如何添加呢?那么,如何从数据库读取客户端?


阅读 603

收藏
2020-12-03

共1个答案

小编典典

不要使用多个inMemory构建器,而是将多个withClients 连接在一个内部inMemory

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
                .withClient("first")
                .secret("secret")
                .scopes("read")
                .authorizedGrantTypes("password")
            .and()
                .withClient("sec")
                .secret("secret")
                .scopes("read")
                .authorizedGrantTypes("password");
}
2020-12-03