CORS跨域设置

CORS跨域设置

CORS(Cross-origin resource sharing),跨域资源共享,是⼀份浏览器技术的规范,⽤来避开 浏览器的同源策略 简单来说就是解决跨域问题的除了jsonp外的另⼀种⽅法;⽐jsonp更加优雅。

  • 1.(‘Access-Control-Allow-Origin’, ‘*’) //这个表示任意域名都可以访问,默认不能携带 cookie了。(必须字段)

//这个表示任意域名都可以访问。
//问题:1.不安全 2.不能携带凭证
 res.header('Access-Control-Allow-Origin', '*'); 
 
res.header('Access-Control-Allow-Origin', 'http://www.baidu.com'); //这样写,只有 www.baidu.com 可以访问。
  • 2.Access-Control-Allow-Headers :设置允许requset设置的头部;
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  • 3.Access-Control-Expose-Headers 允许客户端获取的头部key;
('Access-Control-Expose-Headers''Content-Type, Content-Length, Authorization, Accept, X- Requested-With , yourHeaderFeild') 
  • 4.设置前端允许请求的方法
ctx.set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,HEAD,OPTIONS");
  • 5.允许携带凭证

前端

允许跨域请求携带凭证
 xhr.withCredentials = true;

后端

 ctx.set("Access-Control-Allow-Credentials",true);
  • 6.预检请求
    1.预检请求 options
    2.允许前端设置头部信息

前端

xhr.setRequestHeader("Content-type","application/json");

后端

router.options("/ * ",ctx=>{
    //1.允许跨域
    // ctx.set("Access-Control-Allow-Origin","*"); // 1.不安全 2.不能携带凭证
    ctx.set("Access-Control-Allow-Origin","http://localhost:3000");
   
    //2.允许获取的头部信息;(响应头)
    ctx.set("Access-Control-Expose-Headers","Content-Type, Content-Length,Date")
    
    //3.设置前端允许请求的方法;
    ctx.set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,HEAD,OPTIONS");
    
    //4.允许前端设置的头部(请求头)
    ctx.set("Access-Control-Allow-Headers","Content-Type, Content-Length, Authorization,test");
    ctx.set("Access-Control-Max-Age",3600*24);


    //5.允许携带凭证
    ctx.set("Access-Control-Allow-Credentials",true);

    console.log("有预检请求");
    ctx.body = "";
});
  • 7.设置预检请求的缓存时间
ctx.set("Access-Control-Max-Age",3600*24);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java后端cors跨域设置白名单可以通过以下步骤实现: 1. 在web.xml文件中添加CorsFilter过滤器。 2. 在CorsFilter过滤器中设置允许跨域的域名白名单,可以使用通配符*表示允许所有域名跨域访问。 3. 在CorsFilter过滤器中设置允许跨域求方法,例如GET、POST等。 4. 在CorsFilter过滤器中设置允许跨域求头,例如Content-Type、Authorization等。 5. 在CorsFilter过滤器中设置允许跨域的响应头,例如Access-Control-Allow-Origin、Access-Control-Allow-Methods等。 具体实现可以参考以下代码: ``` public class CorsFilter implements Filter { private String allowOrigin; private String allowMethods; private String allowCredentials; private String allowHeaders; private String exposeHeaders; @Override public void init(FilterConfig filterConfig) throws ServletException { allowOrigin = filterConfig.getInitParameter("allowOrigin"); allowMethods = filterConfig.getInitParameter("allowMethods"); allowCredentials = filterConfig.getInitParameter("allowCredentials"); allowHeaders = filterConfig.getInitParameter("allowHeaders"); exposeHeaders = filterConfig.getInitParameter("exposeHeaders"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest request = (HttpServletRequest) servletRequest; if (StringUtils.isNotBlank(allowOrigin)) { response.setHeader("Access-Control-Allow-Origin", allowOrigin); } if (StringUtils.isNotBlank(allowMethods)) { response.setHeader("Access-Control-Allow-Methods", allowMethods); } if (StringUtils.isNotBlank(allowCredentials)) { response.setHeader("Access-Control-Allow-Credentials", allowCredentials); } if (StringUtils.isNotBlank(allowHeaders)) { response.setHeader("Access-Control-Allow-Headers", allowHeaders); } if (StringUtils.isNotBlank(exposeHeaders)) { response.setHeader("Access-Control-Expose-Headers", exposeHeaders); } filterChain.doFilter(request, response); } @Override public void destroy() { } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值