跨域处理
图
SpringMVC项目总会遇到跨域问题,尤其是前后端分离后,前端项目本身就有自己项目的端口,所以更会遇到跨域的问题,解决好跨域问题,才能更好的做下一步接口开发,不然别人调用接口是会报错的

代码示例

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域处理,方便Vue开发
 */

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS", "TRACE", "CONNECT")
                .allowCredentials(true)
                .maxAge(1800)
                .allowedHeaders("*");
    }
}

注意:SpringBoot2.4.0后不适用

2.4后跨域

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;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Collections;


/**
 * 跨域处理
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1. 允许任何来源
        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
        //2. 允许任何请求头
        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
        //3. 允许任何方法
        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
        //4. 允许凭证
        corsConfiguration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }

}

自定义拦截器与跨域冲突问题

当自定义了拦截器进行token验证的情况,会发现跨域的请求过来时token时丢失的。

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;

/**
 * 配置全局跨域
 * SpringBoot自定义拦截器和跨域配置冲突
 * 当有请求发送到后台时,先被自定义拦截器拦截,如果拦截器验证没有问题,才会开始执行跨域配置。
 * 解决办法:
 * 让跨域配置在自定义拦截器之前执行,由于 Filter 的执行顺序大于自定义拦截器,因此可以在 Filter 中实现跨域的配置
 */
@Configuration
public class GlobalCorsFilter {

    private CorsConfiguration corsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
                corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.setMaxAge(3600L);
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig());
        return new CorsFilter(source);
    }

}