跨域 问题:When allowCredentials is true, allowedOrigins cannot contain the specia

解决办法:跨域配置报错,将.allowedOrigins替换成.allowedOriginPatterns即可。
修改前
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

/**
 * 开启跨域
 */
@Override
public void addCorsMappings(CorsRegistry registry) {
    // 设置允许跨域的路由
    registry.addMapping("/**")
            // 设置允许跨域请求的域名
            .allowedOrigins("*")    // 注意此处
            // 是否允许证书(cookies)
            .allowCredentials(true)
            // 设置允许的方法
            .allowedMethods("*")
            // 跨域允许时间
            .maxAge(3600);
}

}

或者

@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
/**
* 跨域配置
*/
@Bean
public CorsFilter corsFilter()
{
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();

    config.setAllowCredentials(true);
    // 设置访问源地址
    config.addAllowedOrigin("*");

// config.addAllowedOriginPattern("");
// 设置访问源请求头
config.addAllowedHeader("
");
// 设置访问源请求方法
config.addAllowedMethod("*");
// 对接口配置跨域设置
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}

修改后
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

/**
 * 开启跨域
 */
@Override
public void addCorsMappings(CorsRegistry registry) {
    // 设置允许跨域的路由
    registry.addMapping("/**")
            // 设置允许跨域请求的域名
            .allowedOriginPatterns("*")    // 注意此处
            // 是否允许证书(cookies)
            .allowCredentials(true)
            // 设置允许的方法
            .allowedMethods("*")
            // 跨域允许时间
            .maxAge(3600);
}

}

第二个
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
/**
* 跨域配置
*/
@Bean
public CorsFilter corsFilter()
{
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();

    config.setAllowCredentials(true);
    // 设置访问源地址
    //config.addAllowedOrigin("*");
    config.addAllowedOriginPattern("*");
    // 设置访问源请求头
    config.addAllowedHeader("*");
    // 设置访问源请求方法
    config.addAllowedMethod("*");
    // 对接口配置跨域设置
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}

}


版权声明:本文为m0_51426110原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。