快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

详解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://6mz.cn/article/dgooej.html

其他资讯