Java REST 启用 CORS(访问控制允许来源) Glassfish

Posted

技术标签:

【中文标题】Java REST 启用 CORS(访问控制允许来源) Glassfish【英文标题】:Java REST Enabling CORS (Access-Control-Allow-Origin) Glassfish 【发布时间】:2017-01-16 10:51:49 【问题描述】:

您好,我正在使用 Glassfish 4.1.1 作为服务器和 java EE 7 在 java 中开发 REST api,作为前端,我也在 netbeans 上使用 AngularJS。

我正在学习这个简单的教程 https://www.youtube.com/watch?v=2B3qL7XtKnE 并成功创建了背面,但是当我尝试从前端使用 GET 调用时,出现常见错误 Access-Control-Allow-Origin。视频中妹子使用了netbeans自带的跨域分享过滤模板,修复了这个问题。

package entities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;

@Provider
public class awdawdCrossOriginResourceSharingFilter implements    ContainerResponseFilter 

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) 
        response.getHeaders().putSingle("Access-Control-Allow-Origin", "*");
        response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
        response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type");
    


但我仍然遇到同样的错误。看过很多指南和教程,但无法修复。 注意:我是 java、netbeans 等方面的初学者。所以你可以给我的任何信息都会有所帮助。

谢谢

这是浏览器日志的输出:

XMLHttpRequest cannot load http://localhost:8080/AngularBackEnd/rs/customer. Origin http://localhost:8383 is not allowed by Access-Control-Allow-Origin. (09:09:12:047 | error, javascript) at app/index.html

来自 Angular 的调用:

empService.factory('Emps', function($resource)
    return $resource('http://localhost:8080/AngularBackEnd/rs/customer', , 
        findAll:method:'GET', isArray:true
    )
);

再次感谢。

已编辑

现在我使用https://github.com/ebay/cors-filter 并按照他的指南进行操作。添加 web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
    <filter-name>CORS Filter</filter-name>
    <filter-class>org.ebaysf.web.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORS Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

为了测试我运行了命令:

curl -D -"http://localhost:8080/BackEndTest/webresources/service.customer"

这给了我:

Server: GlassFish Server Open Source Edition  4.1.1 
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1.1  Java/Oracle Corporation/1.8)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
Content-Type: application/xml
Date: Fri, 09 Sep 2016 17:50:00 GMT
Content-Length: 6875

正如它显示的那样,它为我提供了所需的访问控制,但我仍然遇到与以前相同的错误。

【问题讨论】:

您是否将应用容器配置为使用您创建的过滤器? 您好,我刚读到它,但我不知道在哪里可以配置它。 How to enable Cross domain requests on JAX-RS web services?的可能重复 我看到了@MartinHansen 的帖子,但我的问题仍然存在,并编辑了更多信息。 【参考方案1】:

客户端必须发送 Origin 标头。

查看How does Access-Control-Allow-Origin header work?了解更多信息,查看How to send a header using a HTTP request through a curl call?了解如何通过 curl 设置标题

在您的情况下,您应该能够执行以下操作: curl --header "来源:http://localhost" -D -"http://localhost:8080/BackEndTest/webresources/service.customer"

【讨论】:

以上是关于Java REST 启用 CORS(访问控制允许来源) Glassfish的主要内容,如果未能解决你的问题,请参考以下文章

启用 Cors 服务的 ionic post in rest 服务错误

Django Angular cors错误:不允许访问控制允许来源

Spring Boot - 如何在 REST 控制器 HTTP PUT 上允许 CORS

状态 200 OK(),角度承诺抛出错误——没有“访问控制允许来源”

CORS 已启用,但 toDataURL 仍会引发警告

使用Azure功能启用CORS通配符子域