首页 > 解决方案 > Spring Boot 拦截器 - 如何排除某些 URL

问题描述

我想从我的全局拦截器(整个控制器)中排除这个端点 URL:

@RequestMapping(value = "/api/rest/v1/company/{companyId}/store")
@RestController()
@RequiredArgsConstructor
@Slf4j
public class StoreController {


@GetMapping(with request params)

@PostMapping()

我尝试了很多选择,例如:

 @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(new GlobalInterceptor(taService, authService))
                .excludePathPatterns("**/store")
                .pathMatcher(new AntPathMatcher());
    }

但没有成功。

此外,我想排除带有查询参数的模式,例如:

/api/rest/v1/company/{companyId}/store?param1=abc&param2=def

也许我应该排除所有 Swagger 网址

标签: javaspring-bootinterceptor

解决方案


我找到了一个解决方案(缺少斜杠“/”):

 @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(new GlobalInterceptor(taService, authService))
                .excludePathPatterns("/**/store")
                .pathMatcher(new AntPathMatcher());
    }

推荐阅读