首页 热点 业界 科技快讯 数码 电子消费 通信 前沿动态 电商

最新springboot解决跨域的几种方式小结

2022-05-16 05:24:45 来源 : 软件开发网

目录

什么是跨域

springboot解决跨域的几种方式

方法一、SpringBoot的注解@CrossOrigin

方式二:使用CorsFilter

方式三:自定义过滤(web filter)的方式

方式四:实现WebMvcConfigurer中addCorsMappings方法

方法五:采用nginx做动态代理

什么是跨域

跨域:指的是浏览器不能执⾏其他⽹站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。例如:a页⾯想获取b页⾯资源,如果a、b页⾯的协议、域名、端⼝、⼦域名不同,所进⾏的访问⾏动都是跨域的,⽽浏览器为了安全问题⼀般都限制了跨域访问,也就是不允许跨域请求资源。注意:跨域限制访问,其实是浏览器的限制。理解这⼀点很重要同源策略:是指协议,域名,端⼝都要相同,其中有⼀个不同都会产⽣跨域;

springboot解决跨域的几种方式方法一、SpringBoot的注解@CrossOrigin

直接在Controller方法或者类上增加@CrossOrigin注解,SpringMVC使用@CrossOrigin使用场景要求 jdk1.8+ Spring4.2+

@GetMapping("/hello")@CrossOriginpublic String hello() { return "hello:" + simpleDateFormat.format(new Date());}方式二:使用CorsFilterimport org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;@Configurationpublic class ConfigConfiguration { @Bean public CorsFilter CorsFilter() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOriginPattern("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); corsConfiguration.setAllowCredentials(true); UrlBasedCorsConfigurationSource ub = new UrlBasedCorsConfigurationSource(); ub.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(ub); }}方式三:自定义过滤(web filter)的方式@Componentpublic class CustomFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse res = (HttpServletResponse) servletResponse; // 设置允许Cookie res.addHeader("Access-Control-Allow-Credentials", "true"); // 允许http://www.xxx.com域(自行设置,这里只做示例)发起跨域请求 res.addHeader("Access-Control-Allow-Origin", "*"); // 设置允许跨域请求的方法 res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); // 允许跨域请求包含content-type res.addHeader("Access-Control-Allow-Headers", "Content-Type,X-CAF-Authorization-Token,sessionToken,X-TOKEN"); if (((HttpServletRequest) servletRequest).getMethod().equals("OPTIONS")) { servletResponse.getWriter().println("ok"); return; } filterChain.doFilter(servletRequest, servletResponse); }}方式四:实现WebMvcConfigurer中addCorsMappings方法import org.springframework.stereotype.Component;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Componentpublic class MyWebMvcConfigurer implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 匹配所有的路径 .allowCredentials(true) // 设置允许凭证 .allowedHeaders("*") // 设置请求头 .allowedMethods("GET", "POST", "PUT", "DELETE") // 设置允许的方式 .allowedOriginPatterns("*"); }}方法五:采用nginx做动态代理

到此这篇关于springboot解决跨域的几种方式的文章就介绍到这了,更多相关springboot解决跨域内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!

标签: 支持软件 相关文章 软件开发

相关文章

最近更新
观焦点:超萌相机 2023-03-01 12:29:37
海南百货网 2023-03-01 12:13:44
焦点热讯:宜点充 2023-02-28 10:10:16
天天关注:小铺CEO 2023-02-28 10:07:13
【世界聚看点】KaFit 2023-02-28 09:31:37
葱天下 2023-02-28 09:17:03
渔界竞钓 2023-02-28 08:15:29
焦点快看:鲸奇视频 2023-02-28 06:30:37
环球热议:萝小逗 2023-02-27 23:25:49
简讯:小码公交 2023-02-27 23:16:12
彼岸花 2023-02-27 22:32:52
时时夺宝 2023-02-27 21:37:50
天天动态:袜之源 2023-02-27 21:29:50
天天资讯:AI空气 2023-02-27 20:19:46
世界时讯:绘读 2023-02-27 20:19:41
看点:一元得购 2023-02-27 19:26:28