2016-04-14 87 views
2

錯誤截圖:澤西 - 迴應預檢要求未通過訪問控制檢查:沒有「訪問控制允許來源」

enter image description here

以下是我的API類中,我已經寫代碼爲@OPTIONS方法。

@OPTIONS 
    public Response OptionsFirstRequst(){ 
     return Response.ok() 
     .header("Access-Control-Allow-Origin", "*") 
     .header("Access-Control-Allow-Methods", "*") 
     .header("Access-Control-Allow-Headers", "*").build(); 
    } 

我已經創建了一個名爲Response Builder的類,使用它爲每個請求發送響應。 以下是響應生成器類的代碼:

public class ResponseBuilder { 

    public int status; 
    public HashMap data; 
    public String error; 

    public static Response ok(int Status_code, HashMap<String, String> data, String Response_error) { 
     if (data == null) { 
      data = new HashMap(); 
     } 

     ResponseBuilder response = new ResponseBuilder(); 
     response.status = Status_code; 
     response.data = data; 
     response.error = Response_error; 

     return Response.status(Status_code).entity(response) 
       .header("Access-Control-Allow-Origin", "*") 
       .header("Access-Control-Allow-Methods", "*") 
       .header("Access-Control-Allow-Headers", "*").build(); 
    } 

    public static Response error(int Status_code, HashMap<String, String> data, String Response_error) { 
     if (data == null) { 
      data = new HashMap(); 
     } 

     ResponseBuilder response = new ResponseBuilder(); 
     response.status = Status_code; 
     response.data = data; 
     response.error = Response_error; 
     response.data = new HashMap(); 

     return Response.status(Status_code).entity(response) 
       .header("Access-Control-Allow-Origin", "*") 
       .header("Access-Control-Allow-Methods", "*") 
       .header("Access-Control-Allow-Headers", "*").build(); 
    } 

} 

我也有一個請求過濾器,其驗證令牌對於每個請求,除了登錄。

我可以登錄,生成令牌並將其返回給瀏覽器。 但是,如果我點擊個人資料登錄後。

我得到的答覆爲200(如瀏覽器的開發工具網絡所示),但我沒有收到任何數據/正確答案。

我得到以下錯誤。

對預檢請求的響應未通過訪問控制檢查:請求的資源上沒有「Access-Control-Allow-Origin」標頭。

回答

2

要檢查並添加CORS標題,常見的解決方案是使用javax.ws.rs.container.ContainerResponseFilter。這裏一個例子,在允許的起源是在一類ApplicationConfig.accessControlAllowedOrigins配置:

import javax.ws.rs.container.ContainerRequestContext; 
import javax.ws.rs.container.ContainerResponseContext; 
import javax.ws.rs.container.ContainerResponseFilter; 
import javax.ws.rs.core.MultivaluedMap; 
import javax.ws.rs.ext.Provider; 
import java.io.IOException; 

@Provider 
public class ResponseCorsFilter implements ContainerResponseFilter { 

    @Override 
    public void filter(ContainerRequestContext requestContext, 
      ContainerResponseContext responseContext) throws IOException { 

     MultivaluedMap<String, Object> responseHeaders = responseContext.getHeaders(); 
     String origin = requestContext.getHeaderString("Origin"); 
     if (null != origin && 
      (ApplicationConfig.accessControlAllowedOrigins.contains(origin) || 
       ApplicationConfig.accessControlAllowedOrigins.contains("*"))) { 

      responseHeaders.putSingle("Access-Control-Allow-Origin", origin); 
      responseHeaders.putSingle("Access-Control-Allow-Methods", 
       "GET, POST, OPTIONS, PUT, DELETE, HEAD"); 

      String reqHead = requestContext.getHeaderString(
       "Access-Control-Request-Headers"); 

      if (null != reqHead && !reqHead.equals("")) { 
       responseHeaders.putSingle("Access-Control-Allow-Headers", reqHead); 
      } 
     } 
    } 
} 
+0

是什麼'ApplicationConfig.accessControlAllowedOrigins'看起來像'ApplicationConfig'? – antogerva

+0

它是一個包含Access-Control-Allow-Origin值的字符串。請參閱:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORSe.g。通配符或單個網址,或逗號分隔的網址列表。 –

相關問題