简体   繁体   English

Angular JS,Jetty CORS问题

[英]Angular JS, Jetty CORS issue

I've been ripping my hair out over this issue. 我一直在努力解决这个问题。 Im attempting to enable CORS between an Angular App and a Jersey Server. 我试图在Angular应用程序与Jersey服务器之间启用CORS。 Basically I've implemented a filter for jersey that should allow angular access to the server: 基本上,我为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;
}
}

I've then added this as an init param to my web.xml. 然后,我将其作为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>

When I hit the URL - mysite:8080/ComplaintService/service/user/authenticate from postman with a post containing user details it returns fine (as is expected from postman) and the header contains all the correct CORS related headers. 当我点击URL时-mysite:8080 / ComplaintService / service / user / authentication from postman,其中包含包含用户详细信息的帖子,它返回正常(如postman所期望的那样),并且标题包含所有正确的CORS相关标题。 When I try to make the call from angular for some reason none of the CORS headers are being returned and it fails. 当我出于某种原因尝试从角度进行呼叫时,没有返回任何CORS标头,并且失败。 Even if I just hit the URL from the browser as a GET the CORS headers are being returned. 即使我刚从GET中点击浏览器中的URL,也会返回CORS标头。 This is my angular call: 这是我的电话:

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

Basically I'm just wondering am I missing something simple? 基本上我只是想知道我是否缺少一些简单的东西? Or perhaps could it be related to the spring security filter? 还是可能与弹簧安全过滤器有关? I was thinking the application could potentially be blocked from reaching the jersey filter. 我当时以为该应用程序可能被阻止访问jersey过滤器。 Here's the spring filter: 这是弹簧过滤器:

<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 is currently deployed on jersey - mysite:8081 and the server is deployed on mysite:8080. Angular当前部署在jersey-mysite:8081上,服务器部署在mysite:8080上。 Any help would be greatly appreciated! 任何帮助将不胜感激!

I overcame this issue using a library - com.thetransactioncompany:cors-filter:1.3.2 and adding some config to my web.xml. 我使用库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>

I think the issue was because the spring security filter was being called first, this seemed to resolve the issue. 我认为问题是因为首先调用了spring安全过滤器,这似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM