2015-04-28 156 views
0

我想實現CORSFilter,但無法獲取它來修改頭文件。我在執行中錯過了什麼?我在WildFly8上使用Java7。無法獲取CORSFilter工作

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"> 
    <context-param> 
    <description>Automatically scan WEB-INF/lib jars and WEB-INF/classes directory for both @Provider and JAX-RS resource classes (@Path, @GET, @POST etc..) and register them.</description> 
    <param-name>resteasy.scan</param-name> 
    <param-value>true</param-value> 
    </context-param> 

    <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, HEAD, POST, DELETE, PUT, OPTIONS</param-value> 
    </init-param> 

    <init-param> 
     <param-name>cors.maxAge</param-name> 
     <param-value>3600</param-value> 
    </init-param> 
    </filter> 

    <filter-mapping> 
    <filter-name>CORS</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

雙龍項:

<dependency> 
    <groupId>com.thetransactioncompany</groupId> 
    <artifactId>cors-filter</artifactId> 
    <version>2.4</version> 
    </dependency> 

我已經寫了一個過濾器來做到這一點,它正確地增加了訪問控制允許的信息,但是我從什麼也得不到CORS過濾器。任何人都可以發現我可能做錯了,它沒有添加標題條目?

----更新--------- 這裏是我寫的這是工作的過濾器:

import java.io.IOException; 

import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.annotation.WebFilter; 
import javax.servlet.http.HttpServletResponse; 

@WebFilter(filterName = "HTML5CorsFilter", urlPatterns = {"/resources/api-docs/*"}) 
public class HTML5CorsFilter implements javax.servlet.Filter { 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     System.out.println("************In the DoFilter method.**************"); 
     HttpServletResponse res = (HttpServletResponse) response; 
     res.addHeader("Access-Control-Allow-Origin", "*"); 
     res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); 
     res.addHeader("Access-Control-Allow-Headers", "Content-Type"); 
     chain.doFilter(request, response); 
    } 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
    } 

    @Override 
    public void destroy() { 
    } 
} 

我只想用這種過濾器,除了我一直負責使用com.thetransactioncompany.cors.CORSFilter。我需要這些(最初)的細節是允許一個單獨的swagger ui安裝服務器多個應用程序,所以我試圖添加的標題需要在swagger jsons的請求上。

回答

1

Servlet過濾器用於servlet。您是否試圖過濾請求到JAX-RS資源?在這種情況下,您應該使用javax.ws.rs.container.ContainerRequestFilter

我相信(但並未確認)JAX-RS處理接管在HTTP請求處理管道很早就有了,之前也Servlet過濾一命嗚呼。

+0

我增加了額外的信息我原來的職位。我包括我正在工作的過濾器。如果我可以使用它並繼續前進,那將會更簡單。 – SteveS