首页 文章

跨域Ajax请求WSO2 IS

提问于
浏览
1
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/' .

2 回答

  • 1

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

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

  • 1

    我不认为IS的Token API返回CORS头 . 为了实现这一点,您可以通过API Manager来使用IS的Token API . 默认情况下,APIM会在您在其中创建的所有REST API中返回CORS标头 .

    实际上,API管理器附带了一个API(即 https://localhost:8243/token ),它位于IS的令牌API前面(即 https://localhost:9443/oauth2/token )(注意:APIM中安装了某些IS功能) . 但它也没有返回CORS头 . 要使其返回CORS标头,您应该遵循您提到的SO链接 .

    另一个简单的选择是,如果你只需要CORS头,你可以简单地通过另一个在响应中返回CORS头的自定义服务来使用IS的Token API .

相关问题