OAuth 2.0 with Implict Grant类型Spring启动应用程序

时间:2017-12-05 05:47:23

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

我们正在构建一个具有以下技术规格的应用程序。

  • Angular 4/5 [前端]
  • SpringBoot Framework [BackEnd]
  • OAuth 2.0 [授权]
  • MySQL [数据库]
  

注意:我们自己有资源服务器,授权服务器

流量

我们为多个客户[我们的客户]提供单实例应用程序,他们将拥有自己的用户。每个用户都会收到一封电子邮件,通过我们的应用程序为各自的客户授权。电子邮件链接将包含client_id,record_id已加密和编码。当用户点击链接时,它应该转到AuthServer,通过其client_id授权客户端,并将令牌传递给用户代理,以进行任何进一步的操作。

我们经历了 Github repo 并实施了相同的示例。

AuthServer Configure代码如下:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
       clients.inMemory().withClient("my-trusted-client")
             .authorizedGrantTypes("password", "authorization_code",
                            "refresh_token", "implicit")
             .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
             .scopes("read", "write", "trust").resourceIds("sparklr")
             .accessTokenValiditySeconds(60).and()
             .withClient("my-client-with-registered-redirect")
      .authorizedGrantTypes("authorization_code").authorities("ROLE_CLIENT")
             .scopes("read", "trust").resourceIds("sparklr")
             .redirectUris("http://anywhere?key=value").and()
             .withClient("my-client-with-secret")
             .authorizedGrantTypes("client_credentials", "password")
                    .authorities("ROLE_CLIENT").scopes("read").resourceIds("sparklr")
             .secret("secret");

}

我们对传递给configure方法的值有一些疑问。

  • .inMemory().withClient("my-trusted-client")代表什么?这总是硬编码吗?由于我们将根据收到的client_id验证每个客户端,我们将在哪里提供此动态以进行动态验证?
  • 什么是.withClient("my-client-with-registered-redirect")?即使这对每个客户来说都是不变的吗?
  • 回购中的作者也说我们可以通过验证设置 $ curl -H "Accept: application/json" my-client-with-secret:secret@localhost:8080/oauth/token -d grant_type=client_credentials我看到my-client-with-secret:secret已经过了这里。如果要为不同的客户更改此内容,我如何将此值提供给.withClient("my-client-with-secret").secret("secret")

我们很难理解这些概念。我们的要求很简单,我们将使用client_id验证每个客户端并为该客户端生成令牌。我们是否需要为此类要求提供任何其他Grant_types

有人请指出我们正确的方向。

1 个答案:

答案 0 :(得分:1)

第一个问题: 在您的示例中,客户端是硬编码的(因此clients.inMemory())。您可以配置数据源并使用:

@Autowired
DataSource dataSource;

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.jdbc(dataSource); // Get clients from database, table = oauth_client_details
}

您可以在documentation

中找到更多信息

第二个问题在该示例中,配置了三个客户端:

  1. my-trusted-client:此客户端可以授权使用这些OAuth2流程:"password", "authorization_code", "refresh_token", "implicit"
  2. my-client-with-registered-redirect:此客户端可以授权使用这些OAuth2流程:"authorization_code"
  3. my-client-with-secret:此客户端可以授权使用这些OAuth2流程:"client_credentials"
  4. 您需要了解这些流量之间的差异。

    第三个问题如果您想使用其他客户端,您必须将它们添加到您的代码/数据库中