zoukankan      html  css  js  c++  java
  • springboot拦截器导致@CrossOrigin跨域失效

    springboot拦截器导致@CrossOrigin失效。

    原因:

    1、CROS复杂请求时会首先发送一个OPTIONS请求做嗅探,来测试服务器是否支持本次请求,请求成功后才会发送真实的请求;而OPTIONS请求不会携带数据,导致这个请求被拦截了,直接返回了状态码,响应头中没携带解决跨域问题的头部信息,出现了跨域问题。

    方法一:

    因此解决方案是把所有的OPTIONS请求统统放行。详细分析见:https://blog.csdn.net/MrKorbin/article/details/104066979

    在preHandle加以下内容:

    if("OPTIONS".equals(request.getMethod().toUpperCase())) {
                return true;
            }

     方法二:

    不使用@CrosOrigin注解解决跨域问题,使用过滤器:示例使用CorsFilter,也就是一个封装了解决跨域问题的filter而已。

    package com.pacmp.config;
    
    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;
    
    /**
     * @Author 
     * @Date 2020/6/19 15:48
     * @Version 版本号
     * @Description 描述
     */
    @Configuration
    public class GlobalCorsConfig {
    
        @Bean
        public CorsFilter corsFilter() {
            CorsConfiguration config = new CorsConfiguration();
            config.addAllowedOrigin("*");
            config.setAllowCredentials(true);
            config.addAllowedMethod("*");
            config.addAllowedHeader("*");
            UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
            configSource.registerCorsConfiguration("/**", config);
            return new CorsFilter(configSource);
        }
    
    }
  • 相关阅读:
    抓包之网络分析器- Wiresshark
    IT 技术网站收集
    XAMPP Apache + MariaDB + PHP + Perl
    http://101.132.165.115/
    LNMP 网站搭建
    10X Genomics : Single Cell Gene Expression
    PacBio下机数据如何看?
    光模块
    ubuntu 'yuan' update
    Perl 中 `cmd` 和system"cmd"的区别
  • 原文地址:https://www.cnblogs.com/penghq/p/13163779.html
Copyright © 2011-2022 走看看