Oauth 2.0 - 单个资源服务器但有多个客户端应用程序

3

你好,

我想问一下以下Oauth 2.0的用例是否有效:

  1. 认证服务器(单独)
  2. 单个(或多个)资源服务器
  3. 多个客户端应用程序访问同一个资源服务器。

Diagram for scenario in question

如果这是有效用例,则如何配置具有授权服务器的多个客户端。无法使用application.properties(application.yml)进行配置。

security.oauth2.client.client-id=dummy
security.oauth2.client.client-secret=password

或者
security:
  oauth2:
    resource:
      token-info-uri: http://localhost:8080/oauth/check_token
    client:
      client-id: dummy
      client-secret: password

在这种情况下,多客户端应用程序的正确配置是什么?
1个回答

2

如果您有多个客户端,则可以通过扩展AuthorizationServerConfigurerAdapter在AuthorizationServer中注册客户端详细信息。

以下是在内存中注册客户端详细信息的示例:

@EnableAuthorizationServer
@Configuration
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
    private final AuthenticationManager authenticationManager;

    @Autowired
    public AuthServerConfig(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("egen")
                .secret("{noop}egensecret")
                .authorizedGrantTypes("authorization_code","refresh_token","password")
                .scopes("food_read","food_write")
            .and()
                .withClient("oauthclient")
                .secret("{noop}oauthclient-secret")
                .authorizedGrantTypes("client_credentials", "refresh_token")
                .authorities("ROLE_USER", "ROLE_OPERATOR")
                .scopes("food_read");
    }
///more code
}

更详细的信息,请查看我的Github仓库: https://github.com/Dovchiproeng/spring-cloud-security-oauth2-poc/blob/master/spring-cloud-secure-auth-server/src/main/java/com/egen/springcloudsecureauthserver/config/AuthServerConfig.java

感谢您提供的代码片段。我们需要配置两个地方:1.授权服务器 2.资源服务器。这个将解决授权服务器配置(我猜我们不能使用application.properties进行配置)。对于第二部分,即资源服务器,没有在application.properties或使用RemoteTokenServices中进行配置的选项。多次调用tokenService上的set client只会覆盖它。您能否在您的答案中添加第二部分的答案。谢谢。 - Parik
对于part2,您只需要使用@EnabledResourceServer和使用remoteTokenServices来检查来自Authorization Server的checkTokenUrl的令牌。对于RemoteTokenServices中的SetClientId和SetSecret,只是针对checkToken端点的基本身份验证,以防某些Authorization Server需要它。但是,由这3个客户端应用程序的clientId和secret请求的访问令牌将返回不同的范围和权限。--续下评论 - Chi Dov
如果您想限制某些客户端应用程序的权限,可以在资源服务器中使用@PreAuthorized。默认情况下,他们只能访问您的资源服务器进行身份验证。 - Chi Dov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接