学习谷粒商城项目基础篇,处理跨域问题时,在网关添加了跨域设置,并注释了renren-fast 项目里CorsConfig中关于跨域的设置,可在前端项目登录时,
依旧提示重复跨域错误:
网关代码如下:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
/**
* @author: xlk
* 跨域处理
*/
@Configuration
public class GulimallCorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
//允许哪些头跨域
corsConfiguration.addAllowedHeader("*");
// 允许哪些方式跨域 get post delete 等方式
corsConfiguration.addAllowedMethod("*");
//允许哪些请求来源跨域 * 任意来源
corsConfiguration.addAllowedOrigin("*");
// 是否允许携带cooker跨域
corsConfiguration.setAllowCredentials(true);
//注册跨越配置 /**配置请求路径
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
// boot2.4以上的把addAllowedOrigin("*");改成addAllowedOriginPattern("*");
}
}
renren-fast 注释掉的跨域设置:
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package io.renren.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/*
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(3600);
}
}*/
这个问题处理了好久了,麻烦哪位帮忙看看,万分感谢!
在网关写了跨域配置类的话,检查一下controller,一定不能再加跨域注解