【问题标题】:unable to get Oauth2 token from auth server无法从身份验证服务器获取 Oauth2 令牌
【发布时间】:2015-11-02 15:23:27
【问题描述】:

我在 spring 中配置了一个 auth-server

clients
        .inMemory()
        .withClient("my-web-app")
        .secret("my-web-app-secret")
        .authorizedGrantTypes(//
            "authorization_code",//
            "refresh_token",//
            "password"//
        ).scopes("openid")

现在我想为 webapp 开发一个命令行应用程序。因此,我需要使用单独的客户端 ID 和密码注册另外一个客户端。 我做过这样的事情

.and()
        .inMemory()
        .withClient("my-cli")
        .secret("my-cli-secret")
        .authorizedGrantTypes("authorization_code","client_credentials")
        .scopes("read","write","trust").authorities("ROLE_USER");

我想要实现的是只需提供用户名/密码,客户端应用程序应该能够从身份验证服务器获取身份验证令牌。

我尝试并理解的是我应该使用资源所有者密码授予。在此之后我必须使用弹簧 Oauth2restTemplate 。 此配置中的问题是当我点击 tokenuri 时,我得到了

{
error: "unauthorized"
error_description: "Full authentication is required to access this resource"
}

每次都是匿名用户。

【问题讨论】:

  • 在启用包的调试级别后添加堆栈跟踪:org.springframework.*

标签: spring authentication spring-security oauth-2.0 resttemplate


【解决方案1】:

如果你想使用用户名/密码来获取访问令牌,你肯定需要使用grant_type=password

您也不需要指定.inMemory() 两次 - 只需两个客户端之间有.and()

所以配置必须类似于

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients
            .inMemory()
                    // First client 
            .withClient("my-web-app")
            .secret("my-web-app-secret")
            .authorizedGrantTypes(//
                    "authorization_code",//
                    "refresh_token",//
                    "password"//
            ).scopes("openid")
            // Second Client(CLI)
            .and()
            .withClient("my-cli")
            .secret("my-cli-secret")
            .authorizedGrantTypes("password")
            .scopes("read", "write", "trust")
            .authorities("ROLE_USER");
}

还有其他非常重要的事情 - 您需要在令牌的 http 请求中设置 Authorization: 标头。所以标题应该是

"Authorization: Basic " + Base64.encodeBase64String((clientId + ":" + clientSecret).getBytes())

在用户名\密码之前检查此标头,并定义客户端(在您的情况下为 CLI)是授权客户端(这可能会导致您的问题出现错误)。

如果你能添加你使用的代码,那将是非常好的Oauth2RestTemplate

【讨论】:

  • 我已经添加了上面提到的代码并尝试使用resttemplate访问它MultiValueMap<String, String> requestParams = new LinkedMultiValueMap<String, String>(); requestParams.add("grant_type", "password"); requestParams.add("client_id", "commandline"); requestParams.add("username", "user"); requestParams.add("password", "pwd"); headers = HttpHeaders(); headers.add("ContentType", "x-www-form-urlencoded"); headers.add("Authorization: Basic ", Base64.encodeBase64String(("my-cli" + ":" + "my-cli-secret").getBytes()));
  • 但我收到这条消息作为回复AuditEvent [timestamp=Tue Aug 11 10:22:42 IST 2015, principal=user, type=AUTHENTICATION_FAILURE, data={message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}]
  • 试试headers.add("Authorization", "Basic " + Base64.encodeBase64String(("my-cli" + ":" + "my-cli-secret").getBytes()));Basic 这里是值的一部分,不是标题名称)
  • 仍然收到异常{message=Bad credentials, type=org.springframework.security.authentication.BadCredentialsException}
  • 使用第一个客户端注册的用户是否可以使用第二个客户端?
猜你喜欢
  • 2022-12-01
  • 2016-09-15
  • 2020-01-16
  • 2023-03-04
  • 2020-06-30
  • 2015-06-21
  • 2021-04-21
  • 2018-09-07
  • 1970-01-01
相关资源
最近更新 更多