【问题标题】:How to call JHipster (Spring) OAuth2 Rest server using Postman Authentication helpers如何使用 Postman 身份验证助手调用 JHipster (Spring) OAuth2 Rest 服务器
【发布时间】:2026-01-22 16:35:01
【问题描述】:

Postman 有 Authentication helpers 来帮助进行经过身份验证的调用,我正在尝试使用 OAuth 2.0 helper 调用 JHipster 使用 Spring 创建的 REST 服务器(安全、社交等)。

我已经尝试了很多配置,这是屏幕(客户端 ID 和 Secret 被屏蔽):

对于我尝试过的授权 URL

收到令牌后我离 Postman 的距离越近:

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

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

【问题讨论】:

    标签: spring-security spring-security-oauth2 jhipster postman spring-oauth2


    【解决方案1】:

    JHipster 当前设置为使用“密码”oauth2 授权类型。 helper oauth2 助手似乎只适用于“授权码”和“客户端凭据”授权类型。

    您要做的是首先直接调用您应用的令牌端点,就像 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
    

    您的用户名和密码可以分别为“用户”和“用户”,例如,并设置一个标头:

    Authorization: Basic AAAAAA
    

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

    这应该会返回一个 access_token。现在,通过在您的标头中使用您的密码请求中的 access_token 调用它们来访问您的 API 端点,如下所示。

    Authorization: Bearer access_token_from_earlier_token_request
    

    更新:如果您使用微服务和 UAA,请查看 Niel 的回答 https://*.com/a/45549789/1098564

    【讨论】:

      【解决方案2】:

      以@sdoxsee 的回答为基础:

      目前(2017 年 8 月)JHipster 生成一个名为 UaaConfiguration 的类,该类使用 configure(ClientDetailsServiceConfigurer) 方法设置客户端 ID、客户端密码、范围和授权类型。请参考这些设置(包括 application*.yml 中引用的 JHipster 属性)以填充 Postman 身份验证帮助程序,使用 /oauth/token 作为 Auth URLAccess Token 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
      

      意味着您的身份验证助手应按如下方式填充:

      【讨论】:

        最近更新 更多