【问题标题】:Oauth 2.0 - Single resource server but multiple client applicationsOauth 2.0 - 单个资源服务器但多个客户端应用程序
【发布时间】:2019-05-11 23:27:48
【问题描述】:

问候,

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

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

如果这是一个有效的用例,我们如何使用授权服务器配置多个客户端。无法使用 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

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

【问题讨论】:

    标签: java spring spring-boot oauth-2.0 spring-security-oauth2


    【解决方案1】:

    所以如果您有多个客户端,您可以通过扩展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 repo:

    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

    【讨论】:

    • 感谢代码 sn-p。我们需要配置两个地方: 1. 授权服务器 2. 资源服务器。这将解决授权服务器配置(我想我们无法使用 application.properties 进行配置)。对于第 2 部分。资源服务器没有选项可以在 application.properties 或使用 RemoteTokenServices 中执行此操作。多次在 tokenService 上调用 set client 只会覆盖它。您能否在答案中也添加第 2 部分的答案。谢谢
    • 对于第 2 部分,您只需要使用 @EnabledResourceServer 并使用 remoteTokenServices 通过来自授权服务器的 checkTokenUrl 来检查令牌。 RemoteTokenServices 中的 SetClientId 和 SetSecret 只是 checkToken 端点的基本身份验证,以防某些授权服务器需要它。但是,这 3 个客户端应用程序的 clientId 和 secret 请求的访问令牌将返回不同的范围和权限。 --在下一条评论中继续
    • 如果您想限制某些客户端应用程序的权限,您可以在资源服务器中使用@PreAuthroized。默认情况下,他们应该能够访问您的资源服务器进行身份验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多