问题描述
我正在使用AngularJS和Resteasy开发一个应用程序。正如预期的,我面对着名的问题
I am developing an app using AngularJS and Resteasy. As expected I am facing the well known problem of
XMLHttpRequest无法加载http:// localhost:8080 / ..... No'Access -Control-Allow-Origin'头在请求的资源上。原因'http:// localhost:3000'因此不允许访问。该响应具有HTTP状态代码403.
在其他堆栈溢出帖子上可以看到,我尝试从一个Feature对象使用Resteasy的CorsFilter,但我得到:
As seen on other stack overflow posts [1], I tried using Resteasy's CorsFilter from a Feature object, but I get:
[33m02:06:57,883 WARN [org.jboss.resteasy.core.ExceptionHandler] (default task-1) failed to execute: javax.ws.rs.ForbiddenException: Origin not allowed: http://localhost:3000
at org.jboss.resteasy.plugins.interceptors.CorsFilter.checkOrigin(CorsFilter.java:194)
at org.jboss.resteasy.plugins.interceptors.CorsFilter.filter(CorsFilter.java:134)
我的 CorsFeature p>
My CorsFeature object:
@Provider
public class CorsFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().add("*");
context.register(corsFilter);
return true;
}
}
在 web.xml 我添加了:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>org.jboss.resteasy.plugins.interceptors.CorsFilter</param-value>
</context-param>
我看到当我评论这个上下文参数时,我没有得到上述异常和
I see that when I comment this context-param, I don't get the aforementioned Exception and the response status is 200, rather than 403.
在有角模块配置中添加:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
我知道可以选择创建一个node.js代理,但我想解决这个难的方式。
I know there is the option of creating a node.js proxy, but I'd like to solve this the hard way.Can you please help me overcome this big obstacle in life?
感谢:)
推荐答案
Wildfly解决方案 b
$ b
Solution for Wildfly
- 编辑
standalone.xml
:
- edit
standalone.xml
:
config
<subsystem xmlns="urn:jboss:domain:undertow:3.0">
<buffer-cache name="default"/>
<server name="default-server">
<http-listener name="default" socket-binding="http" redirect-socket="https"/>
<host name="default-host" alias="localhost">
<location name="/" handler="welcome-content"/>
<filter-ref name="server-header"/>
<filter-ref name="x-powered-by-header"/>
<filter-ref name="Access-Control-Allow-Origin"/>
<filter-ref name="Access-Control-Allow-Methods"/>
<filter-ref name="Access-Control-Allow-Headers"/>
<filter-ref name="Access-Control-Allow-Credentials"/>
<filter-ref name="Access-Control-Max-Age"/>
</host>
</server>
<servlet-container name="default">
<jsp-config/>
<websockets/>
</servlet-container>
<handlers>
<file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
</handlers>
<filters>
<response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
<response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
<response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="*"/>
<response-header name="Access-Control-Allow-Methods" header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT"/>
<response-header name="Access-Control-Allow-Headers" header-name="Access-Control-Allow-Headers" header-value="accept, authorization, content-type, x-requested-with"/>
<response-header name="Access-Control-Allow-Credentials" header-name="Access-Control-Allow-Credentials" header-value="true"/>
<response-header name="Access-Control-Max-Age" header-name="Access-Control-Max-Age" header-value="1"/>
</filters>
</subsystem>
- 重新启动Wildfly
这篇关于CORS:AngularJS + Resteasy 3 + Wildfly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!