【问题标题】:Angular JS, Jetty CORS issueAngular JS,码头 CORS 问题
【发布时间】:2014-08-09 10:00:14
【问题描述】:

我一直在为这个问题烦恼。我试图在 Angular 应用程序和 Jersey 服务器之间启用 CORS。基本上我已经为球衣实现了一个过滤器,它应该允许对服务器进行角度访问:

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;
}
}

然后我将此作为初始化参数添加到我的 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/authenticate 并发布包含用户详细信息的帖子时,它返回正常(正如邮递员所期望的那样),并且标头包含所有正确的 CORS 相关标头。当我出于某种原因尝试从角度进行调用时,没有返回任何 CORS 标头并且它失败了。即使我只是从浏览器中点击 URL 作为 GET,也会返回 CORS 标头。这是我的角度调用:

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

基本上我只是想知道我是否缺少一些简单的东西?或者它可能与弹簧安全过滤器有关?我在想该应用程序可能会被阻止到达球衣过滤器。这是弹簧过滤器:

<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 上。任何帮助将不胜感激!

【问题讨论】:

    标签: angularjs jersey cors


    【解决方案1】:

    我使用库解决了这个问题 - 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>
    

    我认为问题是因为首先调用了弹簧安全过滤器,这似乎解决了问题。

    【讨论】: