401 需要授权从 Webapp 集成 Hyperledger Composer REST API

401 Authorization Required integrating Hyperledger Composer REST API from Webapp

简介

我按照这个 link https://hyperledger.github.io/composer/integrating/enabling-rest-authentication.html

在安全模式下有一个 hyperledger env 运行

如果我按照文档中指定的方式进行身份验证(直接从浏览器点击http://mydomain:3000/auth/github)然后访问 Rest API http://mydomain:3000/explorer 并且可以授权为各种参与者(即,颁发身份并将它们添加到钱包并一次将一个设置为默认值)并且可以根据 .acl 文件查看资产。

问题

但是当我开始从我的 Web 应用程序而不是直接从浏览器集成 Rest API 的 时,我开始遇到问题。作为第一步 从我的网络应用程序 ,我调用 http://mydomain:3000/auth/github 进行身份验证,然后开始调用其他 APIs(transaction/list,等等。 )但我总是得到 错误 401:'Authorization Required'

我试过的

将我的 Web 应用程序 URL 作为 hyperledger 环境变量中的 'Redirect URL'。在成功验证(调用 http://mydomain:3000/auth/github)后,它成功重定向到我的 webapp 主页,但之后访问 Rest API(从 web 应用程序)再次抛出 'Authorization Required' 错误。

环境变量如下:

export COMPOSER_PROVIDERS='{
 "github": {
  "provider": "github",
  "module": "passport-github",
  "clientID": "CLIENT_ID",
  "clientSecret": "CLIENT_SECRET",
  "authPath": "/auth/github",
  "callbackURL": "/auth/github/callback",
  "successRedirect": "http://localhost:8080/home.html",
  "failureRedirect": "/"
 }
}'

在我的 web 应用程序中加入 passport-github2 机制(即,使用 github 的 oauth 注册我的应用程序)并成功登录到我的 web 应用程序;调用 http://mydomain:3000/auth/github 对区块链进行身份验证,但也没有成功。

我有几个问题:

  1. 从另一个 Web 应用程序调用受保护的超级账本 Rest API 是否可行?
  2. 如果是,怎么办?我在 hyperledger composer 文档中找不到该信息。

这个问题已经试了一个星期了,还是没有答案。任何帮助将不胜感激。如果有任何不清楚的地方,请告诉我。谢谢。

我在一个现有的超级账本 github 问题(低于 link)上评论了这个问题,我想分享为我解决这个问题的解决方案。 https://github.com/hyperledger/composer/issues/142

解决方法:如用户所述sstone1

Since the REST server is on a different port number to your web application, you need to specify an additional option to your HTTP client to pass the cookies to the REST server. Using the Angular HTTP client, you add the withCredentials flag, for example:

通过Angular:

this.http.get('http://mydomain:3000/api/MyAsset', { withCredentials: true })

通过JQuery AJAX:

  $.ajax({
    url: 'http://mydomain:3000/api/MyAsset',
    xhrFields: {
      withCredentials: true
    },
    headers: {
      ...
    }
  })