先说下springboot的拦截器!
我的需求是测试提出了,在生产环境下,swagger-ui.html 不应该可以访问,再网上看了很多yml的配置,由于我集成的swagger是引入提取的公共jar包,不方便大规模改动
于是就考虑用拦截器拦截浏览器的swagger-ui.html 这个请求
如下,是写了个配置文件,拦截请求,返回404,但是有个问题,就是 /v2/api-docs这个文件是拦截不了的,貌似是因为他是静态文件,但是我也不知道是什么原因,反正就是很郁闷,拦截不了
package com.ehl.citycockpit.web.swagger; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Profile("!swagger") @RestController @Slf4j @Configuration public class SwaggerConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { /** *此处拦截路径(/**) * 注意两个**。一个*号只拦截一级路径下,两个*号拦截所有 */ registry.addInterceptor(new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { log.info("request请求地址path[{}] uri[{}]", request.getServletPath(), request.getRequestURI()); //这里的异常是我自定义的异常,系统抛出异常后框架捕获异常然后转为统一的格式返回给前端, 其实这里也可以返回false response.setStatus(HttpStatus.NOT_FOUND.value()); return false; } //下面的这个两个方法如果用不到可以不实现 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }).addPathPatterns("/swagger-ui.html"); } }
然后继续说另外一种过滤器的方法
首先,实现implement filter 然后重写 doFilter 代码如下
package com.ehl.citycockpit.config; import com.esotericsoftware.minlog.Log; import net.hasor.utils.StringUtils; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class ApiCorsFilter implements Filter { //过滤swagger的相关请求,如果需要用到swagger删除如下代码即可,想拦截其他内容可以在下方配置! private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>( Arrays.asList("/swagger-ui.html", "/v2/api-docs"))); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response;
} HttpServletRequest request1 = (HttpServletRequest) request; //获取请求路径 String path = request1.getRequestURI().substring(request1.getContextPath().length()).replaceAll("[/]+$", ""); boolean filterPath = ALLOWED_PATHS.contains(path); //判断路径是否包含你想过滤的内容 if (filterPath) { Log.info("这里是需要处理的url进入的方法"); } else { Log.info("这里是不需要处理的url进入的方法"); chain.doFilter(request, response); } } }
如果不想用这种方式可以参考其他yml配置的方式,根据不同问题找合适的结局方法,还请大佬指点为什么拦截器不能拦截 /v2/api-docs