第一种就是跨域注解:
@CrossOrigin
@CrossOrigin:只能解决普通跨域问题;
第二种:解决复杂跨域的方案:
1.能解决token响应头
2.cookies的跨域问题
要导入包:
<!--复杂跨域--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
在写一个配置跨域的(配置类)
package com.xiaohou.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * @author 曾文斌 */ @Configuration public class SaTokenCorsWebFilter extends WebMvcConfigurationSupport { @Bean public CorsWebFilter corsWebFilter(){ UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource(); CorsConfiguration config=new CorsConfiguration(); // 配置跨域 config.setAllowCredentials(true); // 允许cookies跨域 //修改的地方 // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin config.addAllowedOriginPattern("*"); config.addAllowedOrigin("*"); // #允许访问的头信息,*表示全部 config.addAllowedHeader("*"); // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 // config.setMaxAge(18000L); // 允许提交请求的方法,*表示全部允许 config.addAllowedMethod("*"); source.registerCorsConfiguration("/**",config); return new CorsWebFilter(source); } }
第三:学习篇:
![](https://i-blog.csdnimg.cn/blog_migrate/1548ec9c742135c53f0b207f777e2e2e.png)
HTTP学习篇: