簡體   English   中英

Angular JS,Jetty CORS問題

[英]Angular JS, Jetty CORS issue

我一直在努力解決這個問題。 我試圖在Angular應用程序與Jersey服務器之間啟用CORS。 基本上,我為jersey實現了一個過濾器,該過濾器應允許對服務器進行有角度的訪問:

public class SimpleCORSFilter implements ContainerResponseFilter {
/**
 * Add the cross domain data to the output if needed
 * 
 * @param creq The container request (input)
 * @param cres The container request (output)
 * @return The output request with cross domain if needed
 */
@Override
public ContainerResponse filter(ContainerRequest creq, ContainerResponse cres) {
    cres.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
    cres.getHttpHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
    cres.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
    cres.getHttpHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
    cres.getHttpHeaders().add("Access-Control-Max-Age", "1209600");
    return cres;
}
}

然后,我將其作為init參數添加到了web.xml中。

<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.complaints.security.SimpleCORSFilter</param-value>
</init-param>

當我點擊URL時-mysite:8080 / ComplaintService / service / user / authentication from postman,其中包含包含用戶詳細信息的帖子,它返回正常(如postman所期望的那樣),並且標題包含所有正確的CORS相關標題。 當我出於某種原因嘗試從角度進行呼叫時,沒有返回任何CORS標頭,並且失敗。 即使我剛從GET中點擊瀏覽器中的URL,也會返回CORS標頭。 這是我的電話:

$http.post('http://mysite:8080/ComplaintService/service/user/authenticate', user).then(function(data) {
        alert(JSON.stringify(data));
    });

基本上我只是想知道我是否缺少一些簡單的東西? 還是可能與彈簧安全過濾器有關? 我當時以為該應用程序可能被阻止訪問jersey過濾器。 這是彈簧過濾器:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Angular當前部署在jersey-mysite:8081上,服務器部署在mysite:8080上。 任何幫助將不勝感激!

我使用庫com.thetransactioncompany:cors-filter:1.3.2並向我的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.supportedHeaders</param-name>
        <param-value>origin, content-type, accept</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我認為問題是因為首先調用了spring安全過濾器,這似乎可以解決問題。

暫無
暫無

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

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