我正在尝试实现一个CORS响应文件管理器,以允许从我的JavaScript前端进行跨域引用。如果我理解正确的话,我正在使用Wildfly 10.0.final
附带Resteasy,JAX-RS 2.0
赞美。
编辑:将@Provider添加到CorsResponseFilter,并作为RestServiceConfig的单例添加。
我需要做些什么来调用我的CorsResponseFilter?
PS。阅读这些帖子,但他们没有帮助解决问题。
ContainerRequestFilter ContainerResponseFilter dosent get called
ResourceConfig and Application
CorsResponseFilter.java
@Provider
public class CorsResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
//headers.add("Access-Control-Allow-Origin", "http://podcastpedia.org"); //allows CORS requests only coming from podcastpedia.org
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");
headers.add("Access-Control-Max-Age", "1209600");
}
}
RestServiceConfig.java
public class RestServiceConfig extends Application {
private final Set<Object> singletons = new HashSet<>();
public RestServiceConfig() {
singletons.add(new CorsResponseFilter());
singletons.add(new ApplicationService());
singletons.add(new TweetObsService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
的web.xml
...
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.mycorp.myapp.service.RestServiceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>org.clearbyte.obs.service.CorsResponseFilter</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/service</param-value>
</context-param>
...
Wildfly日志控制台
13:56:01,672 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] RESTEASY002225: Deploying javax.ws.rs.core.Application: class org.clearbyte.obs.service.RestServiceConfig
13:56:01,672 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] RESTEASY002200: Adding class resource org.clearbyte.obs.service.TweetObsService from Application class org.clearbyte.obs.service.RestServiceConfig
13:56:01,672 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] RESTEASY002200: Adding class resource org.clearbyte.obs.service.ApplicationService from Application class org.clearbyte.obs.service.RestServiceConfig
答案 0 :(得分:2)
所以我从零开始用一个新项目来消除错误来源。感谢您使用@Provider
和添加OPTIONS
的输入。另外,我从web.xml中删除了所有配置REST。
@Provider对于过滤器工作至关重要
ServiceCorsFilter.java
@Provider
public class ServiceCorsFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
responseContext.getHeaders().putSingle("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
responseContext.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type");
}
}
@ApplicationPath使web.xml配置过时
ServiceConfig.java
@ApplicationPath("service")
public class ServiceConfig extends Application {
private Set<Object> singletons = new HashSet<>();
public ServiceConfig() {
singletons.add(new UserServiceV1());
singletons.add(new ServiceCorsFilter());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
这是web.xml中的内容
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>MyApp</display-name>
<!-- No REST related config due the the @Provider and inheritance of Application-->
</web-app>
答案 1 :(得分:0)
我会尝试将其声明为标准过滤器,而不是servlet调度程序的参数:
<filter>
<filter-name>CorsHeadersFilter</filter-name>
<filter-class>org.clearbyte.obs.service.CorsResponseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsHeadersFilter</filter-name>
<url-pattern>/service/*</url-pattern>
</filter-mapping>
答案 2 :(得分:0)
某些浏览器(即:Chrome)会在发出请求之前发送OPTION请求。 由于您明确指定了&#39; GET&#39; &#39; PUT&#39; &#39; POST&#39; &#39;删除&#39;&#39;选项&#39;调用没有得到标题信息:)
将OPTION添加到列表中应解决问题