SpringBoot2.x系列:整合SpringMVC之CORS跨域访问处理(下)
2020-04-26 18:35
726 查看
上一章节中,我给大家讲解了同源策略,跨域访问,以及CORS跨域访问的解决方案。接下来我就要讲解一下SpringBoot中如何实现跨域访问。
SpringBoot中跨域访问实现方案
- 全局配置实现方案
- 基于过滤器的实现方案
- @CrossOrigin注解实现方案
一. 全局配置实现方案
这是一种全局配置的实现方式,一般都是这种解决办法。
[code]package com.yyb.boot.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @Description Description * @Author 一一哥Sun * @Date Created in 2020/3/28 * * 注意: WebMvcConfigurerAdapter这个类,@deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made * possible by a Java 8 baseline) and can be implemented directly without the * need for this adapter. * WebMvcConfigurerAdapter这个类从5.0以后就过失了,5.0之后直接实现WebMvcConfigurer接口就行. * * 这是一种全局配置的实现方式,一般都是这种解决办法. */ @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { //针对的映射 registry.addMapping("/**") //针对的origin域名 .allowedOrigins("*") //针对的方法 .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") //是否允许发送Cookie .allowCredentials(true) //从预检请求得到相应的最大时间,默认30分钟 .maxAge(3600) //针对的请求头 .allowedHeaders("*"); } }
二. 基于过滤器的实现方案
该方式基于过滤器来实现,简单明了。
[code]package com.yyb.boot.filter; import org.springframework.context.annotation.Configuration; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @Description Description * @Author 一一哥Sun * @Date Created in 2020/3/28 * * 基于过滤器的实现方式,简单明了. */ @Configuration @WebFilter(filterName = "CorsFilter") public class CorsFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); filterChain.doFilter(servletRequest, servletResponse); } }
三. @CrossOrigin注解实现方案
@CrossOrigin注解来实现,更为简单。
[code]package com.yyb.boot.web; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Description Description * @Author 一一哥Sun * @Date Created in 2020/3/28 * * 以@CrossOrigin注解方式实现跨域访问. */ @RestController public class UserController { @CrossOrigin(origins = "http://localhost:8088") @GetMapping("/msg") public String showMsg() throws Exception { return "success"; } }
注意:
以上三种实现方法都可以解决跨域问题,最常用的是第一种和第二种两种方式。
如果三种方式都用了的话,则采用就近原则。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Springboot整合token的权限管理实现(二)—— 配置服务器拦截器与跨域访问权限
- Springboot CORS跨域访问
- spring-boot-plus CORS跨域处理
- Springboot(9)轻松搞定跨域访问(CORS)
- Spring boot 总结之跨域处理cors
- SpringBoot2.x系列第八章 (整合Thymeleaf)
- spring boot 数据访问2 整合mybatis,pagehelper分页,多数据源事务处理
- SpringBoot2.x系列第九章 (静态资源处理)
- 解决Springboot与前端VUE整合 出现的跨域访问报错问题
- Spring Boot如何通过CORS处理跨域问题
- SpringBoot 实现前后端分离的跨域访问(CORS)
- 第二十五章:SpringBoot添加支持CORS跨域访问
- spring boot / cloud (六) 开启CORS跨域访问
- SpringBoot整合系列-整合SpringMVC
- SpringBoot系列教程21--整合SpringMVC之简介
- SpringBoot配置CORS跨域访问
- SpringBoot2.x系列第五章 (跨域配置)
- SpringBoot2.x系列第七章 (全局异常处理)
- springboot-springmvc-mybatis整合
- Springboot配置跨域访问