在 Spring Boot 中从拦截器中排除路径

问题描述 投票:0回答:1

我有一个简单的拦截器来检查子域,我想从中排除一些路径。我遵循了 spring 文档 Documentation 并尝试了在这里找到的多种解决方案,但没有一个对我有用。这是我的拦截器。

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Autowired
JdbcTemplate jdbcTemplate;

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new HandlerInterceptor() {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            String subdomain = extractSubdomain(request);
            verifySubdomain(subdomain);
            return true;
        }
    }).excludePathPatterns("/**/clientcontrol/**", "/**/localhost/**").pathMatcher(new AntPathMatcher());
    }
}

我还尝试了以下方法:

.excludePathPatterns("/clientcontrol/**", "/localhost/**").pathMatcher(new AntPathMatcher());

.excludePathPatterns("/clientcontrol/**", "/localhost/**");

.addPathPatterns("/**").excludePathPatterns("/clientcontrol/**", "/localhost/**").pathMatcher(new AntPathMatcher());

但是,拦截器仍然启动,我的

verifySubdomain
方法抛出异常。

java spring spring-boot interceptor
1个回答
0
投票

由于某种原因,以下内容对我不起作用

.excludePathPatterns("/**/clientcontrol/**");

我的整个地址是

http://localhost:8080/api/clientcontrol/foo,
,从技术上讲,它应该有效,但没有。当我尝试以下方法时,它起作用了。

.excludePathPatterns("/api/clientcontrol/**");
© www.soinside.com 2019 - 2024. All rights reserved.