前言
springboot跨域问题,是当前主流web开发人员都绕不开的难题。但我们首先要明确以下几点
跨域只存在于浏览器端,不存在于安卓/ios/node.js/python/ java等其它环境
跨域请求能发出去,服务端能收到请求并正常返回结果,只是结果被浏览器拦截了。
之所以会跨域,是因为受到了同源策略的限制,同源策略要求源相同才能正常进行通信,即协议、域名、端口号都完全一致。
浏览器出于安全的考虑,使用 xmlhttprequest对象发起 http请求时必须遵守同源策略,否则就是跨域的http请求,默认情况下是被禁止的。换句话说,浏览器安全的基石是同源策略。
同源策略限制了从同一个源加载的文档或脚本如何与来自另一个源的资源进行交互。这是一个用于隔离潜在恶意文件的重要安全机制。
先给出一个熟悉的报错信息,让你找到家的感觉~
access to xmlhttprequest at 'http://192.168.1.1:8080/app/easypoi/importexcelfile' from origin 'http://localhost:8080' has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested resource.
一、什么是cros?
cors是一个w3c标准,全称是”跨域资源共享”(cross-origin resource sharing),允许浏览器向跨源服务器,发出xmlhttprequest请求,从而克服了ajax只能同源使用的限制。
它通过服务器增加一个特殊的header[access-control-allow-origin]来告诉客户端跨域的限制,如果浏览器支持cors、并且判断origin通过的话,就会允许xmlhttprequest发起跨域请求。
cors header
access-control-allow-origin: http://www.xxx.com
access-control-max-age:86400
access-control-allow-methods:get, post, options, put, delete
access-control-allow-headers: content-type
access-control-allow-credentials: true
含义解释:
cors header属性
解释
access-control-allow-origin
允许http://www.xxx.com域(自行设置,这里只做示例)发起跨域请求
access-control-max-age
设置在86400秒不需要再发送预校验请求
access-control-allow-methods
设置允许跨域请求的方法
access-control-allow-headers
允许跨域请求包含content-type
access-control-allow-credentials
设置允许cookie
二、springboot跨域请求处理方式
方法一、直接采用springboot的注解@crossorigin(也支持springmvc)
简单粗暴的方式,controller层在需要跨域的类或者方法上加上该注解即可
/**
* created with idea
*
* @author chensj
* @date 2020/5/8 10:28
* @description xxxx控制层
* @version 1.0
*/
@restcontroller
@crossorigin
@requestmapping("/situation")
public class situationcontroller extends publicutilcontroller {
@autowired
private situationservice situationservice;
// log日志信息
private static logger logger = logger.getlogger(situationcontroller.class);
}
但每个controller都得加,太麻烦了,怎么办呢,加在controller公共父类(publicutilcontroller)中,所有controller继承即可。
/**
* created with idea
*
* @author chensj
* @date 2020/5/6 10:01
* @description
* @version 1.0
*/
@crossorigin
public class publicutilcontroller {
/**
* 公共分页参数整理接口
*
* @param currentpage
* @param pagesize
* @return
*/
public pageinfoutil proccedpageinfo(string currentpage, string pagesize) {
/* 分页 */
pageinfoutil pageinfoutil = new pageinfoutil();
try {
/*
* 将字符串转换成整数,有风险, 字符串为a,转换不成整数
*/
pageinfoutil.setcurrentpage(integer.valueof(currentpage));
pageinfoutil.setpagesize(integer.valueof(pagesize));
} catch (numberformatexception e) {
}
return pageinfoutil;
}
}
当然,这里虽然指springboot,springmvc也是同样的,但要求在spring4.2及以上的版本。另外,如果springmvc框架版本不方便修改,(nginx同理)
springmvc使用@crossorigin使用场景要求
jdk1.8+spring4.2+
方法二、处理跨域请求的configuration
增加一个配置类,crossoriginconfig.java。继承webmvcconfigureradapter或者实现webmvcconfigurer接口,其他都不用管,项目启动时,会自动读取配置。
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.corsregistry;
import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;
/**
* ajax请求跨域
* @author mr.w
* @time 2018-08-13
*/
@configuration
public class corsconfig extends webmvcconfigureradapter {
static final string origins[] = new string[] { "get", "post", "put", "delete" };
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/**").allowedorigins("*").allowcredentials(true).allowedmethods(origins).maxage(3600);
}
方法三、采用过滤器(filter)的方式
同方法二加配置类,增加一个corsfilter 类,并实现filter接口即可,其他都不用管,接口调用时,会过滤跨域的拦截。
@component
public class corsfilter implements filter {
@override
public void dofilter(servletrequest request, servletresponse response, filterchain chain)
throws ioexception, servletexception {
httpservletresponse res = (httpservletresponse) response;
res.addheader("access-control-allow-credentials", "true");
res.addheader("access-control-allow-origin", "*");
res.addheader("access-control-allow-methods", "get, post, delete, put");
res.addheader("access-control-allow-headers", "content-type,x-caf-authorization-token,sessiontoken,x-token");
if (((httpservletrequest) request).getmethod().equals("options")) {
response.getwriter().println("ok");
return;
}
chain.dofilter(request, response);
}
@override
public void destroy() {
}
@override
public void init(filterconfig filterconfig) throws servletexception {
}
}
总结
好了,关于springboot比较常用的解决跨域问题方式都已经分享给您了,希望对老铁有所帮助。