Jax-rs -jersey 跨源请求启用
Jax-rs -jersey Cross Origin request enablement
我正在尝试使用版本 2.X 制作我的第一件球衣休息 api,但我对 CORS 有疑问。
在配置文件之前,这是我的 curl 输出:
curl -i http://localhost:8080/camp/rest/user/register
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 32
Date: Sat, 07 Feb 2015 15:34:28 GMT
从浏览器我可以得到正确的输出,没有问题。但是我无法在我的 tomcat 服务器上启用 cors。
这是我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>camp</display-name>
<servlet>
<servlet-name>camp</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.yigit.camp.api.out</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.yigit.camp.api.security</param-value>
</init-param>
<init-param>
<param-name>org.glassfish.jersey.container.ContainerResponseFilters</param-name>
<param-value>com.yigit.camp.api.security.CORSResponseFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>camp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<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>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是我的过滤器 class:
package com.yigit.camp.api.security;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
@Provider
public class CORSResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext creq, ContainerResponseContext cres) {
cres.getHeaders().add("Access-Control-Allow-Origin", "");
cres.getHeaders().add("Access-Control-Allow-Headers", "");
cres.getHeaders().add("Access-Control-Allow-Credentials", "");
cres.getHeaders().add("Access-Control-Allow-Methods", "");
cres.getHeaders().add("Access-Control-Max-Age", "");
}
}
我也在 tomcats web.xml 上做了同样的配置,但结果是一样的。
解决这个问题的正确方法是什么?
大家好
将此添加到您的 web.xml 中:
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.yigit.camp.api.MyAppConfig</param-value>
</init-param>
然后添加这个 class:
package com.yigit.camp.api;
public class MyAppConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(CORSResponseFilter.class);
return classes;
}
}
现在应该使用您的响应过滤器。
此解决方案适用于 tomcat 服务器
将以下代码添加到您的 web.xml 并将以下两个 jar 添加到您的构建路径
*粗过滤器1.7.jar
* java-属性-utils-1.7.1.jar
添加这个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>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last- Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
更多信息请参考
https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html
我正在尝试使用版本 2.X 制作我的第一件球衣休息 api,但我对 CORS 有疑问。
在配置文件之前,这是我的 curl 输出:
curl -i http://localhost:8080/camp/rest/user/register
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 32
Date: Sat, 07 Feb 2015 15:34:28 GMT
从浏览器我可以得到正确的输出,没有问题。但是我无法在我的 tomcat 服务器上启用 cors。
这是我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>camp</display-name>
<servlet>
<servlet-name>camp</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.yigit.camp.api.out</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.yigit.camp.api.security</param-value>
</init-param>
<init-param>
<param-name>org.glassfish.jersey.container.ContainerResponseFilters</param-name>
<param-value>com.yigit.camp.api.security.CORSResponseFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>camp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<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>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是我的过滤器 class:
package com.yigit.camp.api.security;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
@Provider
public class CORSResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext creq, ContainerResponseContext cres) {
cres.getHeaders().add("Access-Control-Allow-Origin", "");
cres.getHeaders().add("Access-Control-Allow-Headers", "");
cres.getHeaders().add("Access-Control-Allow-Credentials", "");
cres.getHeaders().add("Access-Control-Allow-Methods", "");
cres.getHeaders().add("Access-Control-Max-Age", "");
}
}
我也在 tomcats web.xml 上做了同样的配置,但结果是一样的。
解决这个问题的正确方法是什么?
大家好
将此添加到您的 web.xml 中:
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.yigit.camp.api.MyAppConfig</param-value>
</init-param>
然后添加这个 class:
package com.yigit.camp.api;
public class MyAppConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(CORSResponseFilter.class);
return classes;
}
}
现在应该使用您的响应过滤器。
此解决方案适用于 tomcat 服务器
将以下代码添加到您的 web.xml 并将以下两个 jar 添加到您的构建路径 *粗过滤器1.7.jar * java-属性-utils-1.7.1.jar
添加这个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>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last- Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
更多信息请参考