如何使用 Postman 身份验证助手调用 JHipster (Spring) OAuth2 Rest 服务器

How to call JHipster (Spring) OAuth2 Rest server using Postman Authentication helpers

Postman Authentication helpers to help with authenticated calls and I'm trying to use the OAuth 2.0 helper 使用 Spring 调用由 JHipster 创建的 REST 服务器(安全、社交等)。

我尝试了很多配置,这是屏幕(client ID 和 Secret 被屏蔽了):

对于授权URL我试过:

我离收到返回给 Postman 的令牌越近:

不知道为什么会这样。也许我设置的回调 URL 不正确?我需要在服务器或客户端 (AngularJS) 中执行此操作吗?

有人知道哪里出了问题吗?感谢您的帮助。

JHipster 当前设置为使用 "password" oauth2 授权类型。助手 oauth2 助手似乎只适用于 "authorization code" 和 "client credentials" 授权类型。

您要做的是首先像 angular 应用程序那样直接调用您应用程序的令牌端点 src/main/webapp/scripts/components/auth/provider/auth.oauth2.service.js

POST http://localhost:8080/oauth/token?username=MY_USERNAME&password=MY_PASSWORD&grant_type=password&scope=read%20write

您的用户名和密码可以分别为 "user" 和 "user",例如,设置一个 header:

Authorization: Basic AAAAAA

其中 AAAAAA 是您的 (clientId + ":" + clientSecret)--所有 base64 编码。您可以使用 https://www.base64encode.org/。例如,如果您的 clientId 是 "jhipsterapp" 而您的 clientSecret 是 "mySecretOAuthSecret",请将 AAAAAA 替换为 "amhpcHN0ZXJhcHA6bXlTZWNyZXRPQXV0aFNlY3JldA==",因为它是 "jhipsterapp:mySecretOAuthSecret" base64 编码的。

那应该 return 你 access_token。现在,通过在您的 header 中使用您的密码请求中的 access_token 来调用您的 API 端点,就像这样。

Authorization: Bearer access_token_from_earlier_token_request

更新:如果您使用的是微服务和 UAA,请参阅 Niel 的回答 https://whosebug.com/a/45549789/1098564

以@sdoxsee 的回答为基础:

目前(2017 年 8 月)JHipster 生成一个名为 UaaConfiguration 的 class,使用 configure(ClientDetailsServiceConfigurer) 方法设置客户端 ID、客户端密码、范围和授权类型。参考这些设置(包括 application*.yml 中引用的 JHipster 属性)来填充 Postman 身份验证助手,使用 /oauth/token 作为 Auth URL访问令牌URL.


示例:

@Override                                                                                                     
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {                              
    /*                                                                                                        
    For a better client design, this should be done by a ClientDetailsService (similar to UserDetailsService).
     */                                                                                                       
    clients.inMemory()                                                                                        
        .withClient("web_app")                                                                                
        .scopes("openid")                                                                                     
        .autoApprove(true)                                                                                    
        .authorizedGrantTypes("implicit", "refresh_token", "password", "authorization_code")                  
        .and()                                                                                                
        .withClient(jHipsterProperties.getSecurity().getClientAuthorization().getClientId())                  
        .secret(jHipsterProperties.getSecurity().getClientAuthorization().getClientSecret())                  
        .scopes("web-app")                                                                                    
        .autoApprove(true)                                                                                    
        .authorizedGrantTypes("client_credentials");                                                          
}  

并且,

jhipster:
    security:
        client-authorization:
            client-id: internal
            client-secret: internal

表示您的身份验证助手应按如下方式填充: