spring boot filter 不拦截某个接口
时间: 2023-07-30 13:05:23 浏览: 286
您可以在Spring Boot中自定义Filter,然后在配置类中添加FilterRegistrationBean来注册Filter。如果您想要排除某个接口不被该Filter拦截,可以使用`@WebFilter(urlPatterns = {"/exclude/path"})`注解来排除该接口。
以下是一个示例:
```java
@Component
@WebFilter(urlPatterns = {"/path/*"}) // 拦截 /path 下的所有接口
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String requestURI = req.getRequestURI();
// 如果请求的是 /path/exclude,则不执行过滤器
if (requestURI.equalsIgnoreCase("/path/exclude")) {
chain.doFilter(request, response);
return;
}
// 执行过滤器逻辑
// ...
chain.doFilter(request, response);
}
}
```
其中,`@WebFilter(urlPatterns = {"/path/*"})`表示该Filter会拦截所有以`/path/`开头的接口,而通过判断`requestURI`是否为`/path/exclude`,可以排除该接口不被该Filter拦截。
阅读全文