【问题标题】:jhipster oauth : How can i get the token via CURLjhipster oauth:如何通过 CURL 获取令牌
【发布时间】:2019-07-30 01:06:58
【问题描述】:

我正在尝试使用 jhipster 创建一个具有 oauth2 身份验证的新项目。项目示例运行良好,我可以使用 angularjs 界面登录。但是,当我尝试在命令行中使用 CURL 检索 access_token 时,我得到的响应为:

"error":"Unauthorized","message":"Bad credentials"

有人可以帮助我了解如何使用 curl 获取 access_token 吗?

【问题讨论】:

  • 您需要出示您的代码。
  • 看看 Gatling 性能测试,我正在那里做同样的事情,它正在工作

标签: angularjs curl oauth jhipster


【解决方案1】:

给你!

curl http://127.0.0.1:8080/oauth/token --request POST --insecure --data 
"username=[xxx]&password=[yyy]&grant_type=password&scope=read%20write&    
client_secret=[your app secret]&client_id=[your app id] " -H     
"Authorization:Basic [base64 of your appid:appsecrt]"

【讨论】:

    【解决方案2】:

    在 jhipster 中的 application.yml 中取消注释 cors

    cors: #By default CORS are not enabled. Uncomment to enable.
            allowed-origins: "*"
            allowed-methods: GET, PUT, POST, DELETE, OPTIONS
            allowed-headers: "*"
            exposed-headers:
            allow-credentials: true
            max-age: 1800
    

    要在 ionic 中使用 Oauth2 身份验证访问 REST API,您必须首先在 ionic 应用程序中获取令牌

    $http({
        method: "post", 
        url: "http://192.168.0.4:8085/[Your app name]/oauth/token",
        data:  "username=admin&password=admin&grant_type=password&scope=read write&client_secret=my-secret-token-to-change-in-production&client_id=auth2Sconnectapp",
        withCredentials: true,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json',
          'Authorization': 'Basic ' + 'YXV0aDJTY29ubmVjdGFwcDpteS1zZWNyZXQtdG9rZW4tdG8tY2hhbmdlLWluLXByb2R1Y3Rpb24='
          }
      })                
      .success(function(data) {
          alert("success: " + data);
      })
      .error(function(data, status) {
          alert("ERROR: " + data);
      });
    

    这里 "YXV0aDJTY29ubmVjdGFwcDpteS1zZWNyZXQtdG9rZW4tdG8tY2hhbmdlLWluLXByb2R1Y3Rpb24=" 等于 (clientId + ":" + clientSecret)--所有 base64 编码

    您可以使用https://www.base64encode.org/ 自己验证或重新创建它

    如果成功,aboue $http 会给你这个 JSON,其中包含令牌和到期时间

    {
      "access_token": "2ce14f67-e91b-411e-89fa-8169e11a1c04",
      "token_type": "bearer",
      "refresh_token": "37baee3c-f4fe-4340-8997-8d7849821d00",
      "expires_in": 525,
      "scope": "read write"
    }
    

    如果您想访问任何 API,请注意“access_token”和“token_type”,这是您必须使用的。我们使用 API 发送令牌以访问数据,直到令牌过期,然后我们要么刷新它,要么访问新的令牌。 例如

    $http({
        method: "get", 
        url: "http://192.168.0.4:8085/auth-2-sconnect/api/countries",
        withCredentials: true,
        headers: {
          'Authorization':' [token_type] + [space] + [access_token] '
          }
      })                
      .success(function(data) {
          alert("success: " + data);
      })
      .error(function(data, status) {
          alert("ERROR: " + data);
      });
    

    【讨论】:

      【解决方案3】:

      一个简单的方法:

      1. 只需在 Firefox 浏览器中打开 FireBug,使用正确的凭据模拟登录过程
      2. 在“NET”选项卡中找到登录请求。
      3. 右键单击它,然后单击“复制为 cURL”
      4. 将复制的值粘贴到终端中以查看 cURL 请求中的预期内容:它看起来很冗长,但您可以省略某些 参数。 @Rajender Saini 回答中提到了所需的参数 在那里。

      一切都完成了。

      【讨论】:

      • 我对 Chrome 做了同样的事情。它没有用。尽管我已经在 J​​Hipster 的 application.yml 中取消注释“cors”配置,但它显示“无法验证提供的 CSRF 令牌,因为未找到您的会话”。
      猜你喜欢
      • 2015-03-31
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多