实现拦截功能需要完成的步骤:
1、在创建的类中 implements HandlerInterceptor
可以在你想要实现的方法前后去添加一些你想实现的功能。
2、写配置类(implements WebMvcConfigurer)
将你写好的拦截器进行注册,这样spring在调用时才会知道以及调用。
在Spring框架中,拦截器是一种非常重要的组件,可以用于实现对请求的预处理、后处理等操作。为了让Spring框架知道我们定义的拦截器并能够正确调用它,我们需要通过实现WebMvcConfigurer接口来进行注册。
具体来说,我们需要创建一个配置类,并实现WebMvcConfigurer接口。在该配置类中,我们可以通过重写addInterceptors方法来注册我们定义的拦截器。在addInterceptors方法中,我们可以通过调用addInterceptor方法来添加拦截器,并指定其拦截的路径。
以下代码案例仅供参考:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private AlphaInterceptor alphaInterceptor;
@Autowired
private LoginTicketInterceptor loginTicketInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(alphaInterceptor)
.excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jpeg")
.addPathPatterns("/register","/login");
registry.addInterceptor(loginTicketInterceptor)
.excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jpeg");
}
}