同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。
    同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号(port)

    跨域:一个请求url的协议、域名、端口三者之间任意一个与当前页面url不同

    非同源限制:
    【1】无法读取非同源网页的 Cookie、LocalStorage 和 IndexedDB
    【2】无法接触非同源网页的 DOM
    【3】无法向非同源地址发送 AJAX 请求

    实现 CORS 跨域请求:

    1. 返回新的CorsFilter
    2. 重写 WebMvcConfigurer(全局跨域)
    3. 使用注解 @CrossOrigin
    4. 手动设置响应头 (HttpServletResponse)
    5. 自定web filter 实现跨域
    1. @Configuration
    2. public class GlobalCorsConfig {
    3. @Bean
    4. public CorsFilter corsFilter() {
    5. //1. 添加 CORS配置信息
    6. CorsConfiguration config = new CorsConfiguration();
    7. //放行哪些原始域
    8. config.addAllowedOrigin("*");
    9. //是否发送 Cookie
    10. config.setAllowCredentials(true);
    11. //放行哪些请求方式
    12. config.addAllowedMethod("*");
    13. //放行哪些原始请求头部信息
    14. config.addAllowedHeader("*");
    15. //暴露哪些头部信息
    16. config.addExposedHeader("*");
    17. //2. 添加映射路径
    18. UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
    19. corsConfigurationSource.registerCorsConfiguration("/**",config);
    20. //3. 返回新的CorsFilter
    21. return new CorsFilter(corsConfigurationSource);
    22. }
    23. }
    24. @Configuration
    25. public class CorsConfig implements WebMvcConfigurer {
    26. @Override
    27. public void addCorsMappings(CorsRegistry registry) {
    28. registry.addMapping("/**")
    29. //是否发送Cookie
    30. .allowCredentials(true)
    31. //放行哪些原始域
    32. .allowedOrigins("*")
    33. .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
    34. .allowedHeaders("*")
    35. .exposedHeaders("*");
    36. }
    37. }
    1. @RestController
    2. @CrossOrigin(origins = "*")
    3. public class HelloController {
    4. @RequestMapping("/hello")
    5. public String hello() {
    6. return "hello world";
    7. }
    8. }
    9. @RequestMapping("/hello")
    10. @CrossOrigin(origins = "*")
    11. //@CrossOrigin(value = "http://localhost:8081") //指定具体ip允许跨域
    12. public String hello() {
    13. return "hello world";
    14. }
    15. @RequestMapping("/index")
    16. public String index(HttpServletResponse response) {
    17. response.addHeader("Access-Allow-Control-Origin","*");
    18. return "index";
    19. }
    1. @Component
    2. public class MyCorsFilter implements Filter {
    3. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    4. HttpServletResponse response = (HttpServletResponse) res;
    5. response.setHeader("Access-Control-Allow-Origin", "*");
    6. response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    7. response.setHeader("Access-Control-Max-Age", "3600");
    8. response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
    9. chain.doFilter(req, res);
    10. }
    11. public void init(FilterConfig filterConfig) {}
    12. public void destroy() {}
    13. }
    14. Web.xml 配置
    15. <!-- 跨域访问 START-->
    16. <filter>
    17. <filter-name>CorsFilter</filter-name>
    18. <filter-class>com.mesnac.aop.MyCorsFilter</filter-class>
    19. </filter>
    20. <filter-mapping>
    21. <filter-name>CorsFilter</filter-name>
    22. <url-pattern>/*</url-pattern>
    23. </filter-mapping>
    24. <!-- 跨域访问 END -->