@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * 若要在Interceptor中进行依赖注入,则需要:
     * 将拦截器注册为一个 Bean
     * @return
     */
    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor())
                .addPathPatterns("/**")                             //默认过滤全部
                .excludePathPatterns("/swagger-resources/**")       //需要排除的地址
                .excludePathPatterns("/v2/api-docs/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

}

 

 

public class PermissionInterceptor implements HandlerInterceptor {

    @Autowired
    LogRepository logRepository; //在Configuration中配置Bean后,该@Autowired才会生效

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {}
 posted on 2020-01-15 17:15  11HAN  阅读(595)  评论(0)    收藏  举报