是被判断登录过滤器拦截下来的那些请求都没办法进行跨域。
出现跨域失效的主要原因是:登录过滤器的执行顺序在跨域过滤器之前执行,导致跨域配置没有生效。解决方案也很简单,就是让跨域过滤器放在过滤链的前面。
解决方案:
跨域过滤器
/**
* 使用CORS,用于解决ajax跨域访问问题
*/
@Configuration
public class GlobalCorsConfig {
@Bean
public FilterRegistrationBean corsFilter() {
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
//1) 允许的域,不要写*,否则cookie就无法使用了
//config.addAllowedOrigin("http://manage.leyou.com");
//config.addAllowedOrigin("http://www.leyou.com");
config.addAllowedOrigin("*");
//2) 是否发送Cookie信息
config.setAllowCredentials(true);
//3) 允许的请求方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
config.setMaxAge(3600L);
// 4)允许的头信息
config.addAllowedHeader("*");
//2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
//3.返回新的CorsFilter.
//return new CorsFilter(configSource);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(configSource));
bean.setOrder(0);
return bean;
}
}
因为这个CorsConfig并没有实现Filter接口,即使加上 @Order 注解也不会生效,需要通过如下新的方式返回一个新的FilterRegistrationBean出去,并设置order
import com.nanase.takeshi.constants.JwtConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* CorsConfig
* 跨域请求配置
*
* @author 725
* @date 2020/12/10 18:17
*/
@Slf4j
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 1 设置访问源地址
corsConfiguration.addAllowedOrigin("*");
// 2 设置访问源请求头
corsConfiguration.addAllowedHeader("*");
// 3 设置访问源请求方法
corsConfiguration.addAllowedMethod("*");
// 4 暴露哪些头部信息
corsConfiguration.addExposedHeader("*");
return corsConfiguration;
}
/**
@Bean
public CorsFilter corsFilter() {
log.info("跨域设置。。。。");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// 对接口配置跨域设置
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
*/
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
log.info("跨域设置。。。。");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// 5 对接口配置跨域设置
source.registerCorsConfiguration("/**", buildConfig());
//有多个filter时此处设置改CorsFilter的优先执行顺序
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}
这样配置完即可
在启动过程中可能会有
错误
只需要在配置文件中添加对应的配置即可
如果有更好的解决方案,欢迎交流。
添加拦截器跨域失效:https://blog.csdn.net/weixin_48469176/article/details/131696795?spm=1001.2014.3001.5502