springboot跨域问题以及SMM跨域问题

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

/**

  • 跨域配置
    */
    @Configuration
    public class CorsConfig implements WebMvcConfigurer {
//springboot解决跨域问题
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
            .allowedOrigins("*")
//          .allowCredentials(true)
            .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
            .allowCredentials(false)
            .maxAge(3600);
}

private CorsConfiguration buildConfig() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
   //  你需要跨域的地址  注意这里的 127.0.0.1 != localhost
   // corsConfiguration.addAllowedOrigin("http://localhost:3030");
    // * 表示对所有的地址都可以访问
    corsConfiguration.addAllowedOrigin("*");
    //  跨域的请求头
    corsConfiguration.addAllowedHeader("*");
    //  跨域的请求方法
    corsConfiguration.addAllowedMethod("*");

    corsConfiguration.addExposedHeader("Authorization");

    return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//配置 可以访问的地址
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}

}

图片.png

你可能感兴趣的:(springboot跨域问题以及SMM跨域问题)