详解springboot设置cors跨域请求的两种方式-创新互联-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

详解springboot设置cors跨域请求的两种方式-创新互联

1、第一种:

专注于为中小企业提供成都网站制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业沧县免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
public class CorsFilter extends OncePerRequestFilter {

  static final String ORIGIN = "Origin";

  protected void doFilterInternal(
    HttpServletRequest request, 
    HttpServletResponse response, 
    FilterChain filterChain) throws ServletException, IOException {
  
    String origin = request.getHeader(ORIGIN);
  
    response.setHeader("Access-Control-Allow-Origin", "*");//* or origin as u prefer
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Methods", "PUT, POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "content-type, authorization");
  
    if (request.getMethod().equals("OPTIONS"))
      response.setStatus(HttpServletResponse.SC_OK);
    else 
      filterChain.doFilter(request, response);
  
  }
}
@Bean
public CorsFilter corsFilter() throws Exception {
  return new CorsFilter();
}

http
  .addFilterBefore(corsFilter(), UsernamePasswordAuthenticationFilter.class)
  .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class)
  .headers()
  .cacheControl();


分享文章:详解springboot设置cors跨域请求的两种方式-创新互联
链接地址:http://kswsj.cn/article/dgooej.html

其他资讯