【问题标题】:Cross-Domain Ajax Requests WSO2 IS跨域 Ajax 请求 WSO2 IS
【发布时间】:2017-04-06 13:24:43
【问题描述】:
curl -v -X POST -H "Authorization: Basic <base64 encoded client id:client secret value>" -k -d "grant_type=password&username=<username>&password=<password>" -H "Content-Type:application/x-www-form-urlencoded" https://localhost:9443/oauth2/token 

(来自https://docs.wso2.com/display/IS520/Resource+Owner+Password+Credentials+Grant )

这个'curl'命令完美运行。但是当尝试通过ajax调用它时:

  $.ajax({
      type: 'POST',
      data: 'grant_type=password&username=admin&password=admin',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic  Z0hLUlA3eThYTmJydl9JR1pJemxDQ1VJQlo0YTozT2VLaUlmUVViMk1yZzZ2YU5uQVFsWFpDU29h'
      },
      url: 'https://localhost:9443/oauth2/token',
      success: function(data) {
        console.log(data);
      }
    })
    };

我收到此错误:“XMLHttpRequest 无法加载 https://localhost:9443/oauth2/token。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。来源'null' 因此不允许访问。”

如何配置 WSO2 IS 来处理它?或者有替代方案吗?

WSO2 API Manager (WSO2 API Manager CORS) 也存在类似问题。但是 WSO2 IS 中没有这样的目录 '/repository/deployment/server/synapse-configs/'。

【问题讨论】:

    标签: ajax rest wso2 cors wso2is


    【解决方案1】:

    这是未在 Identity Server 上启用跨源资源共享 (CORS) 时的预期行为。事实上,您可以为 Identity Server 启用 CORS。检查 [1] 以了解如何操作。

    [1]http://hasanthipurnima.blogspot.com/2016/05/applying-cors-filter-to-wso2-identity.html

    【讨论】:

    • 这正是我想要的。谢谢。
    【解决方案2】:

    我认为 IS 的 Token API 不会返回 CORS 标头。要使其正常工作,您可以通过 API Manager 前置 IS 的 Token API。默认情况下,APIM 在您在其中创建的所有 REST API 中返回 CORS 标头。

    事实上,API Manager 提供了一个 API(即https://localhost:8243/token),它位于 IS 的令牌 API(即https://localhost:9443/oauth2/token)的前面(注意:APIM 中安装了某些 IS 功能)。但它也不会返回 CORS 标头。要使其返回 CORS 标头,您应该遵循您提到的 SO 链接。

    另一个简单的选择是,如果您只需要 CORS 标头,您可以简单地将 IS 的 Token API 放在另一个自定义服务的前面,该服务在响应中返回 CORS 标头。

    【讨论】:

    • 感谢您的快速回复。
    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多