簡體   English   中英

原因:即使在Java資源中明確指定,CORS標頭“ Access-Control-Allow-Origin”也丟失

[英]Reason: CORS header ‘Access-Control-Allow-Origin’ missing even though explictly specificed in java resource

我正在兩個不同的端口上測試我的后端(在tomcat服務器上使用Jersey的Java)和前端(使用webpack進行服務的Angular 4),因此我得到了一個cors訪問控制源模塊。 對於我的get方法,一切正常,可以在UI上找到任何請求的數據。 現在,我正在測試POST方法,並且標題中始終出現相同的消息。

我的post方法應該持久保存發送給它的數據,並返回帶有新持久實體位置的響應。

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response persistAccountLocation(AccountLocation entity) throws URISyntaxException {


    accountLocationService.persist(entity);

    JsonObject object = Json.createObjectBuilder()
            .add("location", "api/v1/accounts_locations/"+entity.getLocation_id()).build();

    return Response.status(Response.Status.CREATED)// 201
            .entity("Location created")
            .header("Access-Control-Allow-Origin","*")
            .header("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE")
            .allow("OPTIONS")
            .entity(object.toString()).build();
}

在Firefox瀏覽器的“網絡”標簽中,我只能看到狀態為200的選項

Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: access-control-allow-origin,content-type
Origin: http://localhost:4200
Connection: keep-alive

但是在那之后,這個帖子就永遠不會發生。 我假設那時CORS阻止了它。 除了資源類之外,我還應該允許訪問控制嗎? 我已經讀到典型的所有CORS配置都是在服務器端完成的。 完全迷失了。 任何反饋表示贊賞

編輯

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, OPTIONS");         
        headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type,");
    }

}

我對濾鏡做了些微調整,然后將其注冊到我的應用中

public class JaxRsApplication extends ResourceConfig{
    public JaxRsApplication() {

        //  register application resources - unmapped resources will throw exception
        register(AccountLocationResource.class);

        register(CORSResponseFilter.class);
    }

如何使用帶有Jersey堆棧溢出問題的JAX-RS處理CORS提供了很好的答案。

您提供了指向Jersey 1的鏈接,但請確保使用的是哪個版本。

這樣您就不必寫

        .header("Access-Control-Allow-Origin","*")
        .header("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE")
        .allow("OPTIONS")

對於每個請求。 過濾器將為您應用選項。

我注意到的另一件事

return Response.status(Response.Status.CREATED)// 201
        .entity("Location created") // <- entity() call here
        .header("Access-Control-Allow-Origin","*")
        .header("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE")
        .allow("OPTIONS")
        .entity(object.toString()).build(); // <- one more entity() call here (not sure what effect it may have)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM