springboot中的跨域处理
在Web开发中,如果前端页面和后端接口不在同一个域名下,就会发生跨域请求的问题。同源策略是浏览器的一种安全策略,它限制了来自不同源的客户端脚本在浏览器中运行时的交互。
同源指的是协议、域名、端口都相同。
一、jsonp请求实现跨域
jsonp方式只能通过get请求方式来传递参数
二、@CrossOrigin实现跨域
一个例子:
@CrossOrigin(value = {"http://localhost:8081","http://localhost:8082"},maxAge = 3600)
origins: 允许可访问的域列表,可以为“*”
maxAge:准备响应前的缓存持续的最大时间(以秒为单位)。
1、局部实现跨域,指定可访问的方法上@CrossOrigin(value = "http://localhost:8081")设置接受跨域地址的请求,也可以用*表示所有
RestController public class HelloController {
@CrossOrigin(value = "http://localhost:8081") @GetMapping("/hello1") public String hello() { return "hello"; }
@CrossOrigin(value = "http://localhost:8081") @PostMapping("/hello2") public String hello2() { return "hello2"; } }
2,、或者指定整个类
@CrossOrigin(value = "http://localhost:8081") @RestController public class HelloController { @GetMapping("/hello1") public String hello() { return "hello"; } @PostMapping("/hello2") public String hello2() { return "hello2"; } }
3、或者在application启动类上加@CrossOrigin(value = "http://localhost:8081")
三、全局跨域设置,addCorsMappings添加允许跨域的请求地址,可以为*
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:8081") .allowedMethods("*") .allowedHeaders("*"); } }
四、filter实现跨域
这里不依赖spring boot。主要思路就是开发一个Filter,并让Spring容器去扫描注册。
Component public class CORSFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest request = (HttpServletRequest) servletRequest; 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", "content-type,Authorization"); response.setHeader("Access-Control-Allow-Credentials", "true"); chain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
五、通过CorsFilter
import org.springframework.context.annotation.Bean; org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class HttpCorsConfiguration{ @Bean public CorsFilter corsFilter(){ //初始化cors配置对象 CorsConfiguration corsConfiguration = new CorsConfiguration(); //配置允许跨域域名,如果写*表示所有请求都可以跨域 corsConfiguration.addAllowedOrigin("http://localhost:9001"); //允许使用cookie corsConfiguration.setAllowCredentials(true); //配置允许跨域的请求方式(get,post,put等等), *表示所有 corsConfiguration.addAllowedMethod("*"); //配置允许跨域的请求头信息,*表示所有 corsConfiguration.addExposedHeader("*"); //初始化cors配置源对象 UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource(); corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration); return new CorsFilter(corsConfigurationSource); } }