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实现跨域访问实现代码,希望对大家有所帮助
推荐阅读
-
Spring Boot实现跨域访问实现代码
-
PHP json格式和js json格式 js跨域调用实现代码_PHP教程
-
SpringBoot+Spring Security无法实现跨域的解决方案
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
-
详解spring boot实现多数据源代码实战
-
spring boot实现软删除的示例代码
-
Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证示例代码详解
-
Spring Boot 实现Restful webservice服务端示例代码
-
spring4.3 实现跨域CORS的方法