欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot实现跨域访问实现代码

程序员文章站 2024-01-12 16:47:52
当前使用spring版本是4.3.9 import org.springframework.stereotype.component; import javax...

当前使用spring版本是4.3.9

import org.springframework.stereotype.component;
import javax.servlet.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
@component
public class corsfilter implements filter {
  @override
  public void init(filterconfig filterconfig) throws servletexception {
  }
  @override
  public void dofilter(servletrequest servletrequest,
             servletresponse servletresponse,
             filterchain filterchain) throws ioexception, servletexception {
    httpservletrequest request = (httpservletrequest) servletrequest;
    httpservletresponse response = (httpservletresponse) servletresponse;
    response.setheader("access-control-allow-origin", request.getheader("origin"));
//    response.setheader("access-control-allow-origin", "*");//允许跨域访问的域
    response.setheader("access-control-allow-methods", "post, get, options, delete");//允许使用的请求方法,以逗号隔开
    response.setheader("access-control-max-age", "3600");// 缓存此次请求的秒数
    //允许使用的请求方法,以逗号隔开
    response.setheader("access-control-allow-headers", "x-requested-with,cache-control,pragma,content-type,token");
    response.setheader("access-control-allow-credentials","true");//是否允许请求带有验证信息
    filterchain.dofilter(servletrequest, servletresponse);
  }
  @override
  public void destroy() {
  }
}

ps: spring boot 服务器端设置允许跨域访问

 import org.springframework.stereotype.component; 
import javax.servlet.*; 
import javax.servlet.http.httpservletresponse; 
import java.io.ioexception; 
/** 
 * 
 * 跨域过滤器 
 * @author meng 
 * @version 
 * @since 2016年6月19日 
 */ 
@component 
public class corsfilter implements filter { 
  final static org.slf4j.logger logger = org.slf4j.loggerfactory.getlogger(corsfilter.class); 
  public void dofilter(servletrequest req, servletresponse res, filterchain chain) throws ioexception, servletexception { 
    httpservletresponse response = (httpservletresponse) res; 
    response.setheader("access-control-allow-origin", "*"); 
    response.setheader("access-control-allow-methods", "post, get, options, delete"); 
    response.setheader("access-control-max-age", "3600"); 
    response.setheader("access-control-allow-headers", "x-requested-with"); 
    system.out.println("*********************************过滤器被使用**************************"); 
    chain.dofilter(req, res); 
  } 
  public void init(filterconfig filterconfig) {} 
  public void destroy() {} 
} 

2017-04-13更新:

第二种方法,在appplication.java添加:

private corsconfiguration buildconfig() { 
    corsconfiguration corsconfiguration = new corsconfiguration(); 
    corsconfiguration.addallowedorigin("*"); 
    corsconfiguration.addallowedheader("*"); 
    corsconfiguration.addallowedmethod("*"); 
    return corsconfiguration; 
  } 
  /** 
   * 跨域过滤器 
   * @return 
   */ 
  @bean 
  public corsfilter corsfilter() { 
    urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource(); 
    source.registercorsconfiguration("/**", buildconfig()); // 4 
    return new corsfilter(source); 
  } 

总结

以上所述是小编给大家介绍的spring boot实现跨域访问实现代码,希望对大家有所帮助