springBoot跨域问题
主要发生在不同的域名之间,进行相互请求,比如在http://localhost:8080中使用axios去请求http://localhost:8081.这就属于跨域请求(简单来说,需要请求的地址与发送请求的地址,域名不同)
方法1:全局配置
| |
| @Configuration |
| public class CorsConfig implements WebMvcConfigurer { |
| @Override |
| public void addCorsMappings(CorsRegistry registry) { |
| |
| registry.addMapping("/**") |
| |
| .allowCredentials(true) |
| |
| .allowedOriginPatterns("*") |
| |
| .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"}) |
| |
| |
| .allowedHeaders("*") |
| |
| .exposedHeaders("*"); |
| } |
| } |
方法2:局部跨域
Controller层在需要跨域的类或者方法上加上@CrossOrigin该注解即可。
| @CrossOrigin(origins = "*",maxAge = 3600) |
| public class UserController { |
| final UserService userService; |
| |
| @GetMapping("/getOne/{id}") |
| public User getOne(@PathVariable("id") Integer id) { |
| return userService.getById(id); |
| } |
| 我们也可以设置更小的粒度,在方法上设置跨域: |
| |
| @Controller |
| @RequestMapping("/shop") |
| public class ShopController { |
| |
| @GetMapping("/") |
| @ResponseBody |
| |
| @CrossOrigin(originPatterns = "http://localhost:8080") |
| public Map<String, Object> findAll() { |
| |
| return DataSchool.getStudents(); |
| } |
| } |
方法3:定义跨域过滤器
1)编写过滤器
| |
| @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("Success"); |
| return; |
| } |
| chain.doFilter(request, response); |
| } |
| |
| @Override |
| public void destroy() { |
| |
| } |
| |
| @Override |
| public void init(FilterConfig filterConfig) throws ServletException { |
| |
| } |
| } |
2)注册过滤器
| @Configuration |
| public class CorsConfig { |
| |
| @Bean |
| public CorsFilter corsFilter() { |
| CorsConfiguration corsConfiguration = new CorsConfiguration(); |
| corsConfiguration.addAllowedOrigin("*"); |
| corsConfiguration.addAllowedHeader("*"); |
| corsConfiguration.addAllowedMethod("*"); |
| corsConfiguration.setAllowCredentials(true); |
| UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); |
| urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); |
| return new CorsFilter(urlBasedCorsConfigurationSource); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了