Spring引导处理程序拦截器

时间:2018-02-14 07:20:45

标签: spring spring-boot filter interceptor

我有一个包含拦截器的spring boot应用程序。问题是请求是直接命中api而不是拦截器。

这是我注册拦截器的配置类。

@Configuration

public class AuthenticationConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private AuthorizeInterceptor authorizeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizeInterceptor);
    }
}

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class AuthorizeInterceptor extends HandlerInterceptorAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebSecurityConfig.class);

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        LOGGER.debug("POST HANDLE REQUEST------------");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        LOGGER.debug("PREHANDLE REQUEST-------------");     
    }

}

2 个答案:

答案 0 :(得分:0)

我在你的情况下创建了完全相同的项目,拦截器工作正常。您可能需要检查Logger配置或应用程序属性中的logging.level.org.springframework.web=DEBUG

您是否尝试使用System.out.println而不是LOGGER.debug?

答案 1 :(得分:0)

代替

 @Autowired
    private AuthorizeInterceptor authorizeInterceptor;

添加

 @Autowired
    HandlerInterceptor interceptor;