Spring Boot实现跨域访问实现代码
程序员文章站
2023-11-25 17:39:28
当前使用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 + LayIM + t-io 实现文件上传、 监听用户状态的实例代码
-
Spring实现处理跨域请求代码详解
-
Spring Boot+AngularJS+BootStrap实现进度条示例代码
-
Spring Boot 2.0 设置网站默认首页的实现代码
-
3行代码快速实现Spring Boot Oauth2服务功能
-
Spring Boot实现跨域访问实现代码
-
spring boot整合Shiro实现单点登录的示例代码
-
Spring boot通过HttpSessionListener监听器统计在线人数的实现代码
-
c#关于JWT跨域身份验证的实现代码
-
详解可跨域的单点登录(SSO)实现方案【附.net代码】