如何将Access-Control-Allow-Origin添加到jetty服务器 [英] How to add Access-Control-Allow-Origin to jetty server

查看:106
本文介绍了如何将Access-Control-Allow-Origin添加到jetty服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jetty服务器来运行我的Web服务。最近我开发了一个使用Web服务的程序并遇到了Access-Control-Allow-Origin问题。

I've got a jetty server to run my web services. Recently I developed a program to consume the web service and ran into Access-Control-Allow-Origin issue.

如何添加Access-Control-Allow-Origin: *到码头嵌入式服务器。

How can I add the Access-Control-Allow-Origin: * to a jetty embedded server.

下面是webappcontext代码。

below is the webappcontext code.

public WebAppContext buildWebAppContext(){
    webAppContext = new WebAppContext();
    webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml");
    webAppContext.setResourceBase(".");
    webAppContext.setContextPath("/posApplication");
    webAppContext.setAttribute("webContext", webAppContext);
    return webAppContext;
}

谢谢。

推荐答案

设置 org.eclipse.jetty.servlets.CrossOriginFilter

关于该主题的旧问题/答案: https://stackoverflow.com/a/8454168/775715

Old question/answer on the topic: https://stackoverflow.com/a/8454168/775715

参见文档中心 href =http://www.eclipse.org/jetty/documentation/current/cross-origin-filter.html\"rel =nofollow noreferrer> CrossOriginFilter使用:

See Jetty Documentation Hub on CrossOriginFilter Use:

快速入门


  1. 抓住一份码头 - servlets.jar

jetty-servlets.jar 放入 WEB-INF / lib

将以下内容添加到 WEB-INF / web .xml



<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
    <init-param>
        <param-name>allowedOrigins</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>allowedMethods</param-name>
        <param-value>GET,POST,HEAD</param-value>
    </init-param>
    <init-param>
        <param-name>allowedHeaders</param-name>
        <param-value>X-Requested-With,Content-Type,Accept,Origin</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这篇关于如何将Access-Control-Allow-Origin添加到jetty服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆