在 Jersey REST API 中为 CORS 配置 Web.xml
Configure Web.xml for CORS in Jersey REST API
我已经创建了 rest web 服务并希望启用 CORS。
我已将以下代码添加到我的 API 的代码
package RestClient;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
public class CORSResponseFilter
implements ContainerResponseFilter {
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-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");
}
}
为了便于访问,我需要在 web.xml 文件中的 class 上方进行设置。所以这是我的 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>GenerateToken</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>JAVA WS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>RestClient</param-value>
</init-param>
<init-param>
<param-name>org.glassfish.jersey.servlet.ContainerResponseFilters</param-name>
<param-value>RestClient.CORSResponseFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA WS</servlet-name>
<url-pattern>/Test/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>GenerateToken</web-resource-name>
<url-pattern>/Test/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
我只想知道,我是否为 CORS 配置了正确的 web.xml 文件?在我的本地它工作但在我为每个人部署它之前只想确保它是否正确。
任何人都可以对此提出建议吗?
你不需要这个(而且它甚至不正确)
<init-param>
<param-name>org.glassfish.jersey.servlet.ContainerResponseFilters</param-name>
<param-value>RestClient.CORSResponseFilter</param-value>
</init-param>
只需在过滤器 class 之上添加 @Provider
。以下将扫描 RestClient
包中的所有 @Path
和 @Provider
class 并为您注册。
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>RestClient</param-value>
</init-param>
我已经创建了 rest web 服务并希望启用 CORS。 我已将以下代码添加到我的 API 的代码
package RestClient;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
public class CORSResponseFilter
implements ContainerResponseFilter {
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-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");
}
}
为了便于访问,我需要在 web.xml 文件中的 class 上方进行设置。所以这是我的 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>GenerateToken</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>JAVA WS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>RestClient</param-value>
</init-param>
<init-param>
<param-name>org.glassfish.jersey.servlet.ContainerResponseFilters</param-name>
<param-value>RestClient.CORSResponseFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA WS</servlet-name>
<url-pattern>/Test/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>GenerateToken</web-resource-name>
<url-pattern>/Test/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
我只想知道,我是否为 CORS 配置了正确的 web.xml 文件?在我的本地它工作但在我为每个人部署它之前只想确保它是否正确。
任何人都可以对此提出建议吗?
你不需要这个(而且它甚至不正确)
<init-param>
<param-name>org.glassfish.jersey.servlet.ContainerResponseFilters</param-name>
<param-value>RestClient.CORSResponseFilter</param-value>
</init-param>
只需在过滤器 class 之上添加 @Provider
。以下将扫描 RestClient
包中的所有 @Path
和 @Provider
class 并为您注册。
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>RestClient</param-value>
</init-param>