大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

详解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://dzwzjz.com/article/dgooej.html
在线咨询
服务热线
服务热线:028-86922220
TOP