这篇文章主要介绍“springboot解决跨域的方式有哪些”,在日常操作中,相信很多人在springboot解决跨域的方式有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”springboot解决跨域的方式有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
跨域:指的是浏览器不能执⾏其他⽹站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。
例如:a页⾯想获取b页⾯资源,如果a、b页⾯的协议、域名、端⼝、⼦域名不同,所进⾏的访问⾏动都是跨域的,⽽浏览器
为了安全问题⼀般都限制了跨域访问,也就是不允许跨域请求资源。注意:跨域限制访问,其实是浏览器的限制。理解这⼀点
很重要
同源策略:是指协议,域名,端⼝都要相同,其中有⼀个不同都会产⽣跨域;
直接在Controller方法或者类上增加@CrossOrigin注解,SpringMVC使用@CrossOrigin使用场景要求 jdk1.8+ Spring4.2+
@GetMapping("/hello")
@CrossOrigin
public String hello() {
return "hello:" + simpleDateFormat.format(new Date());
}
import 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;
@Configuration
public 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);
}
}
@Component
public 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);
}
}
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Component
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 匹配所有的路径
.allowCredentials(true) // 设置允许凭证
.allowedHeaders("*") // 设置请求头
.allowedMethods("GET", "POST", "PUT", "DELETE") // 设置允许的方式
.allowedOriginPatterns("*");
}
}
到此,关于“springboot解决跨域的方式有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。