java - 具有隐式授予类型 Spring boot 应用程序的 OAuth 2.0

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

我们正在构建一个应用程序,并确定了以下技术规范。

  • Angular 4/5 [前端]
  • SpringBoot 框架[后端]
  • OAuth 2.0 [授权]
  • MySQL [数据库]

Note : We ourselves are having Resource Server, Authorization Server

<小时/>

流量

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

我们经历了这个 Github repo 并实现与示例相同的方式。

AuthServer 配置代码如下:

@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");

}

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

  • .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 验证每个客户端并为该客户端生成一个 token 。我们是否需要任何其他 Grant_types 来满足此类要求?

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

最佳答案

第一个问题: 在您的示例中,客户端是硬编码的(因此是 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”

您需要了解这些流程之间的区别。

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

关于java - 具有隐式授予类型 Spring boot 应用程序的 OAuth 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47646854/

相关文章:

java - 多对多额外列 Spring JPA

swift - 使用 YouTube API (Swift) 对 YouTube 视频进行评分

java - 当存在类似的命名类时,我可以将 Eclipse 配置为导入 java.util 类吗?

java - 如何在 lucene 中索引/搜索 + 和 - 符号?

java - 运行单元测试右键单击上下文项在 IntelliJ 12 中不可见

java - 在ubuntu linux中使用java通过网络摄像头进行人脸检测

java - 在 spring boot 2.4.0 版本中包含配置文件

java - Jackson Unmarshalling 未提供的值

java - appEngine云端点单参数问题

azure - 可能发生此错误的原因列表:OAuth2::Error,invalid_grant:AADSTS65001