dropwizard支持cors的配置如下:

public void run(Configuration conf, Environment environment)  {
// Enable CORS headers
final FilterRegistration.Dynamic cors =
environment.servlets().addFilter("CORS", CrossOriginFilter.class); // Configure CORS parameters
cors.setInitParameter("allowedOrigins", "*");
cors.setInitParameter("allowedHeaders", "X-Requested-With,Content-Type,Accept,Origin");
cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
// Add URL mapping
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}

  

  支持自定义header,需要加上allowedHeaders。否则在浏览器中的response header中可以看到自定义header,js却取不到值。

public void run(Configuration conf, Environment environment)  {
// Enable CORS headers
final FilterRegistration.Dynamic cors =
environment.servlets().addFilter("CORS", CrossOriginFilter.class); // Configure CORS parameters
cors.setInitParameter("allowedOrigins", "*");
cors.setInitParameter("allowedHeaders", "X-Requested-With,Content-Type,Accept,Origin,X-Export-Result");
cors.setInitParameter("allowedHeaders", "X-Export-Result");
    cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD"); 
// Add URL mapping
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); }

  CrossOriginFilter的几个参数的定义:

allowedOrigins
a comma separated list of origins that are allowed to access the resources. Default value is *, meaning all origins.
If an allowed origin contains one or more * characters (for example http://*.domain.com), then "*" characters are converted to ".*", "." characters are escaped to "\." and the resulting allowed origin interpreted as a regular expression. Allowed origins can therefore be more complex expressions such as https?://*.domain.[a-z]{3} that matches http or https, multiple subdomains and any 3 letter top-level domain (.com, .net, .org, etc.). allowedTimingOrigins
a comma separated list of origins that are allowed to time the resource. Default value is the empty string, meaning no origins.
The check whether the timing header is set, will be performed only if the user gets general access to the resource using the allowedOrigins. allowedMethods
a comma separated list of HTTP methods that are allowed to be used when accessing the resources. Default value is GET,POST,HEAD
allowedHeaders
a comma separated list of HTTP headers that are allowed to be specified when accessing the resources. Default value is X-Requested-With,Content-Type,Accept,Origin. If the value is a single "*", this means that any headers will be accepted.
preflightMaxAge
the number of seconds that preflight requests can be cached by the client. Default value is 1800 seconds, or 30 minutes
allowCredentials
a boolean indicating if the resource allows requests with credentials. Default value is true
exposedHeaders
a comma separated list of HTTP headers that are allowed to be exposed on the client. Default value is the empty list
chainPreflight
if true preflight requests are chained to their target resource for normal handling (as an OPTION request). Otherwise the filter will response to the preflight. Default is true.

dropwizard使用cors支持跨域浏览器取不到自定义header问题的更多相关文章

  1. WebApi开启CORS支持跨域POST

    概念:CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求 ...

  2. springboot基于CORS处理跨域问题

    1. 为什么有跨域问题 跨域不一定都会有跨域问题. 因为跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是与当前页域名相同的路径,这能有效的阻止跨站攻击. 因此:跨域问 ...

  3. 在dotnet core web api中支持CORS(跨域访问)

    最近在写的Office add-in开发系列中,其中有一个比较共性的问题就是在add-in的客户端脚本中访问远程服务时,要特别注意跨域访问的问题. 关于CORS的一些基本知识,请参考维基百科的说明:h ...

  4. springboot webapi 支持跨域 CORS

    1.创建corsConfig 配置文件 @Configuration public class corsConfig { @Autowired varModel varModel_; @Bean pu ...

  5. 信息安全-浏览器-CORS:CORS(跨域资源共享)

    ylbtech-信息安全-浏览器-CORS:CORS(跨域资源共享) 1.返回顶部 1. CORS,全称Cross-Origin Resource Sharing,是一种允许当前域(domain)的资 ...

  6. 使Web Api 支持跨域资源共享(CORS)

    Reference:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api Imp ...

  7. .Net WebApi 支持跨域访问使用 Microsoft.AspNet.WebApi.Cors

    首先导入Cors库,通过程序包管理控制台导入 Install-Package Microsoft.AspNet.WebApi.Cors 引用库之后,我们需要进行简单的配置. 现在WebApiConfi ...

  8. 使用cors解决跨域遇到浏览器发出options嗅探

    前言: 本地开发起的服务器,通过修改hosts文件设置域名映射到本地,接口在测试环境 1. 服务器端设置cors, 配置access-control-allow-origin 头部 使用蚂蚁金服的up ...

  9. Spring MVC 后端接口支持跨域CORS调用

    Spring MVC 从4.2版本开始增加了对CORS的支持,可以全局配置,也可以对类或方法配置:可以通过Java代码,也可以通过xml配置方式. 对于低版本的Spring MVC 可以通过Filte ...

随机推荐

  1. 浅谈log4j-6-xml配置 转自godtrue

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...

  2. 【转】浅谈Nginx负载均衡与F5的区别

    前言 笔者最近在负责某集团网站时,同时用到了Nginx与F5,如图所示,负载均衡器F5作为处理外界请求的第一道“墙”,将请求分发到web服务器后,web服务器上的Nginx再进行处理,静态内容直接访问 ...

  3. Modularizing your graphQL schemas

    转自: https://tomasalabes.me/blog/nodejs/graphql/2018/07/11/modularizing-graphql.html Modularizing you ...

  4. windows 8,关闭随意窗体都提示“已停止工作”的解决的方法

           电脑用的好好的,突然出现了关闭随意窗体都提示"已停止工作",每次都是这样.想想也就是偶尔优化一下,近期也没有改动过什么系统设置呀,突然想到昨晚JLink_V8固件丢失 ...

  5. Databinding in WPF

    https://www.codeproject.com/Articles/680271/Simplest-MVVM-Ever

  6. Zookeeper命令行auth,digest

    一.auth auth:user:pwd:cdrwa digest:user:BASE64(SHA1(PWD)):cdrwa addauth digest user:pwd 增加用户和密码都是zhan ...

  7. pyhanlp 中文词性标注与分词简介

    pyhanlp 中文词性标注与分词简介 pyhanlp实现的分词器有很多,同时pyhanlp获取hanlp中分词器也有两种方式 第一种是直接从封装好的hanlp类中获取,这种获取方式一共可以获取五种分 ...

  8. 自然语言处理之:搭建基于HanLP的开发环境(转)

    环境搭建比FNLP的简单,具体参考:https://github.com/hankcs/HanLP 各个版本的下载:https://github.com/hankcs/HanLP/releases 完 ...

  9. shell 查看去掉windons中的换行符

    查看 cat -v 1.sh 替换 sed -i 's/\r//g' 1.sh

  10. 解决iScroll横向滚动区域无法拉动页面的问题

    近期项目中使用iScroll遇到一个问题,在设定wrapper为横向滚动时,如果你手指放在该区域,将无法拉动页面,也就是说该区域取消了默认事件.这个体验是实在是无法接受,特别是页面中有多个横向滚动区域 ...