CORS 在 wso2 身份服务器中被阻止
CORS blocked in wso2 identity server
我在我的 angular 6 应用程序中使用 WSO2is 5.7.0,我尝试从我的应用程序进行 api 调用,但我有一个 cors 错误:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
我已经激活了在 oauth 和 authenticationendpoint 中启用的 cors,我用以下行编辑了两个 webapps 的 WEB-INF/web.xml
:
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
在 authenticationendpoint 中,我将 cors 库从 oauth 复制到 lib 文件夹中的 authenticationendpoint webapps,并使用以下行编辑 pom.xml 文件:
<dependency>
<groupId>com.thetransactioncompany.wso2</groupId>
<artifactId>cors-filter</artifactId>
<version>1.7.0.wso2v1</version>
</dependency>
之后,重启服务,我遇到同样的问题Cross-Origin Request Blocked
在我的 angular 应用程序服务中,我提出如下请求:
const httpHeaders = {
headers: new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Access-Control-Allow-Origin', '*')
};
this.http.post<any>(`http://localhost:9443/commonauth`, payload, httpHeaders)
我认为从 tomcat 启用 cors 支持是必要的,但我不知道该怎么做,除了启用 cors 之外我还有什么其他选择?
我的消息来源:
https://docs.wso2.com/display/IS570/Invoking+an+Endpoint+from+a+Different+Domain
https://hasanthipurnima.blogspot.com/2016/05/applying-cors-filter-to-wso2-identity.html
您尝试调用的端点已在 Identity Server 中注册为 servlet,您需要在 repository/conf/tomcat/carbon/WEB-INF/
中配置 web.xml 文件以将 headers 应用到您的端点.
您可以将 org.apache.catalina.filters.CorsFilter
添加到 above-mentioned 文件以允许所需的域。可以从 tomcat documentation 找到更多信息。示例配置如下所示。
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>https://www.somedomain.com</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/commonauth</url-pattern>
</filter-mapping>
我在我的 angular 6 应用程序中使用 WSO2is 5.7.0,我尝试从我的应用程序进行 api 调用,但我有一个 cors 错误:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
我已经激活了在 oauth 和 authenticationendpoint 中启用的 cors,我用以下行编辑了两个 webapps 的 WEB-INF/web.xml
:
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
在 authenticationendpoint 中,我将 cors 库从 oauth 复制到 lib 文件夹中的 authenticationendpoint webapps,并使用以下行编辑 pom.xml 文件:
<dependency>
<groupId>com.thetransactioncompany.wso2</groupId>
<artifactId>cors-filter</artifactId>
<version>1.7.0.wso2v1</version>
</dependency>
之后,重启服务,我遇到同样的问题Cross-Origin Request Blocked
在我的 angular 应用程序服务中,我提出如下请求:
const httpHeaders = {
headers: new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Access-Control-Allow-Origin', '*')
};
this.http.post<any>(`http://localhost:9443/commonauth`, payload, httpHeaders)
我认为从 tomcat 启用 cors 支持是必要的,但我不知道该怎么做,除了启用 cors 之外我还有什么其他选择?
我的消息来源:
https://docs.wso2.com/display/IS570/Invoking+an+Endpoint+from+a+Different+Domain
https://hasanthipurnima.blogspot.com/2016/05/applying-cors-filter-to-wso2-identity.html
您尝试调用的端点已在 Identity Server 中注册为 servlet,您需要在 repository/conf/tomcat/carbon/WEB-INF/
中配置 web.xml 文件以将 headers 应用到您的端点.
您可以将 org.apache.catalina.filters.CorsFilter
添加到 above-mentioned 文件以允许所需的域。可以从 tomcat documentation 找到更多信息。示例配置如下所示。
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>https://www.somedomain.com</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/commonauth</url-pattern>
</filter-mapping>