springboot跨域过滤器与swagger拦截器冲突的解决方案
程序员文章站
2022-07-10 16:14:33
...
最近在做一个项目,有这样的使用场景,首先是前后端分离,联调的时候出现了跨域问题。所以添加了一个处理跨域的拦截器。代码如下:
@Order(1)
@Component
@WebFilter(urlPatterns = "/*", filterName = "cooCorsFilter")
public class CooCorsFilter implements Filter {
String ORIGIN = "Origin";
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String origin = httpRequest.getHeader(ORIGIN);
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("application/json; charset=utf-8");
httpResponse.setHeader("Access-Control-Allow-Origin", origin);
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
httpResponse.setHeader("Access-Control-Allow-Methods", "*");
httpResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization,token");
httpResponse.setHeader("Access-Control-Expose-Headers", "*");
filterChain.doFilter(httpRequest, httpResponse);
}
@Override
public void destroy() {
}
}
然后就解决了跨域问题。
但是又需要对token的登陆有效性做校验,于是乎就写了一个拦截器,对所有请求做了拦截:大致代码如下:
这么写完还不行,需要在注册一下:
代码如下:
/**
* 拦截器配置
*
* @author liuShuai
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Bean
public TokenInterceptor tokenInterceptor() {
return new TokenInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(tokenInterceptor())
.addPathPatterns("/**");
}
然后已测试完美生效。 但是突然发现了一个问题,就是swagger测试不行了,因为都被拦截器给拦截了,导致所有的swagger-ui页面都没法正常使用。
所以就需要在拦截器里把swagger排除掉,百度了一圈之后,找到了解决方案,拦截器代码进行了如下修改:
/**
* 拦截器配置
*
* @author liuShuai
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Bean
public TokenInterceptor tokenInterceptor() {
return new TokenInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(tokenInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/user/login")
.excludePathPatterns("/user/downloadExcel")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
加完之后发现swagger正常了。但是又出问题了,原来的跨域又不生效了,原来是这两个东西冲突了。有找了一圈,最后解决了,解决方案是把上面的跨域过滤器删掉,换成下面的写法:
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;
/**
* @className: CorsConfig
* @description:
* @author: sh.Liu
* @date: 2020-12-02 10:16
*/
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.setAllowCredentials(true);
config.addAllowedMethod("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
return new CorsFilter(configSource);
}
}
重新启动项目,发现swagger和跨域都正常了,记录一下,希望帮到你们!!有用的点拨关注
!
上一篇: 弄懂this,看这一篇就够了!