赞
踩
同源,就是咱们域名、端口号、ip、采用的协议都相同,那么我们就是同源的
反之就是不同源的!!!
出于浏览器的同源策略限制。同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。
所以,用最简单的话来说,就是前端可以发请求给服务器,服务器也可以进行响应,只是因为浏览器会对请求头进行判断,所以要么前端设置请求头,要么后端设置请求头
跨域报错如下:
Access to XMLHttpRequest at 'http://localhost:8080/t1' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
test1.html?_ijt=aekdfma33ut4n31cgsohdrjt89:17 {readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
jquery-1.9.1.min.js:5 GET http://localhost:8080/t1 net::ERR_FAILED 200
对于 CORS的跨域请求,主要有以下几种方式可供选择:
注意:
CorFilter / WebMvConfigurer / @CrossOrigin 需要 SpringMVC 4.2以上版本才支持,对应springBoot 1.3版本以上
上面前两种方式属于全局 CORS 配置,后两种属于局部 CORS配置。如果使用了局部跨域是会覆盖全局跨域的规则,所以可以通过 @CrossOrigin 注解来进行细粒度更高的跨域资源控制。
其实无论哪种方案,最终目的都是修改响应头,向响应头中添加浏览器所要求的数据,进而实现跨域
1、使用CorsFilter
- @Configuration
- public class corsFilter {
- @Bean
- public CorsFilter CorsFilter() {
- CorsConfiguration corsConfiguration = new CorsConfiguration();
-
- corsConfiguration.addAllowedOriginPattern("*");
- corsConfiguration.addAllowedHeader("*");
- corsConfiguration.addAllowedMethod("*");
- corsConfiguration.setAllowCredentials(true);
-
-
- UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
- urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
-
- return new CorsFilter(urlBasedCorsConfigurationSource);
- }
- }
2、实现WebMvcConfigurer里面的addCorsMappings方法
- @Configuration
- public class corsFilter1 implements WebMvcConfigurer {
- @Override
- public void addCorsMappings(CorsRegistry registry) {
- registry.addMapping("/**") // 匹配所有的路径
- .allowCredentials(true) // 设置允许凭证
- .allowedHeaders("*") // 设置请求头
- .allowedMethods("GET","POST","PUT","DELETE") // 设置允许的方式
- .allowedOriginPatterns("*");
- }
- }
3、@CrossOrigin局部跨域通过
- @GetMapping("/t2")
- @CrossOrigin
- public Map t2() {
- HashMap<String, Object> map = new HashMap<>();
- User user = new User();
- user.setUsername("123456");
- user.setPassword("程世玉");
- map.put("user",user);
-
- return map;
- }
4、添加响应头解决跨域
- @RequestMapping(value = "/user-1")
- public User getUser_1(HttpServletResponse response ) {
-
- // 允许所有,不安全
- response.addHeader("Access-Control-Allow-Origin", "*");
- response.addHeader("Access-Control-Max-Age", "10");
- response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
- response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
- response.setHeader("Access-Control-Allow-Credentials", "true");
-
-
- return new User(1L, "Booker", "admin", "sdfsdkjf93hu8dvn");
- }
5、使用自定义filter实现跨域
- public class MyCorsFilter implements Filter {
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- HttpServletResponse response = (HttpServletResponse) res;
- 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", "x-requested-with,content-type");
- chain.doFilter(req, res);
- }
- public void init(FilterConfig filterConfig) {}
- public void destroy() {}
- }
-
- @Configuration
- public class FilterConfig {
- @Bean
- public FilterRegistrationBean filterRegistrationBean(){
- FilterRegistrationBean bean = new FilterRegistrationBean();
- bean.setFilter(new MyCorsFilter());
- bean.addUrlPatterns("/*");
- return bean;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。