简体   繁体   中英

How to let Interceptor work in springboot

I migrate the code to springboot and our API works well. Only interceptor can't be triggerred. I googled related solutions and modify the code to right format which still failed to trigger the interceptor.

In our project, we also have the filter which extends OncePerRequestFilter and works. It makes me confused. They should be no big difference.

Btw, AOP is used in the project. It's my code.

JerseyConfig.class

@Configuration
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig(){
        packages("com.xxx");
    }
}

VaultAuthorizationInterceptor.class

@Component
public class VaultAuthorizationInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(VaultAuthorizationInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("test");
        return true;
    }
}

VaultAuthConfig.class

@Configuration
public class VaultAuthConfig implements WebMvcConfigurer {

    @Bean
    public VaultAuthorizationInterceptor getVaultInterceptor() {
        return new VaultAuthorizationInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getVaultInterceptor()).addPathPatterns("/**");
    }
}

When you are using the spring-boot-starter-jersey, you use jersey as your web stack. That means any requests will processed by jersey. So you have to register a jersey filter or interceptor. Take a look at the jersey documantation . There is described how to use filters and interceptors. I think you want to use a filter because interceptors in the jersey stack used to manipulate the input or output stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM