【问题标题】:Oauth2.0 auhtorization server configurationOauth 2.0 授权服务器配置
【发布时间】:2016-08-12 05:53:57
【问题描述】:

我正在创建一个基于 oauth2.0 框架的受保护的 rest api。

我成功搭建了授权服务器资源服务器

AuthorizationServer 扩展了 AuthorizationServerConfigurerAdapter 并覆盖了一些方法,我遇到了这个扩展方法的问题

public void configure(ClientDetailsS​​erviceConfigurer clients) 抛出异常{}

这里是解释

当我运行这个版本的 config() 授权服务器

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory().withClient("clientapp").authorizedGrantTypes("password", "refresh_token")
                .scopes("read", "write").resourceIds(RESOURCE_ID).secret("123456");

   }

这个方法工作得很好,当我要求它时会返回一个 access_token。

但是当我运行相同的方法并进行一些增强时,当我请求 access_token 时,我什么也没得到,而是 401 未经授权的 http 响应。

    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    int n = appMetier.getAppsCount();
    for (App app:appMetier.findAll(0, n).getApps()) {
        clients.inMemory().withClient(app.getClientPublicId()).authorizedGrantTypes("password", "refresh_token")
                .scopes("read", "write").resourceIds(RESOURCE_ID).secret(app.getClientSecretId());
    }
  }

这里的 n 变量等于 17,这意味着我在内存中有 17 个有权接收 access_token 的客户端。

唯一从17中获得access_token的是第一个。

请您的回答,并在此先感谢。

【问题讨论】:

  • 同一个 OAuth 提供者需要多个客户端?

标签: java spring rest oauth


【解决方案1】:

您多次调用inMemory(),每次都会覆盖构建器服务。它应该只被调用一次。

以下代码应该可以工作。

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    int n = appMetier.getAppsCount();
    InMemoryClientDetailsServiceBuilder clientBuilder = clients.inMemory();

    for (App app:appMetier.findAll(0, n).getApps()) {
        clientBuilder.withClient(app.getClientPublicId()).authorizedGrantTypes("password", "refresh_token")
            .scopes("read", "write").resourceIds(RESOURCE_ID).secret(app.getClientSecretId());
    }
}

【讨论】:

    猜你喜欢
    • 2013-10-05
    • 2015-01-15
    • 2012-10-01
    • 2014-09-08
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多