拦截器是实现登录验证需要,在springboot中,通常把拦截器加在用户的请求到达controller层的时候实现登录验证,实现步骤:
-
新建拦截器:
自定义=拦截器需要实现HandlerInterceptor接口,接口内容如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.lang.Nullable; public interface HandlerInterceptor { default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { } default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { } }
接口中有三个方法,
-
preHandle方法实在是controller执行之前执行
-
postHandle方法是在controller执行后执行
-
afterCompletion方法在模板渲染后执行
举例:
自定义一个拦截器LogHandlerInterceptor:
package com.lys.community.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class LogHandlerInterceptor implements HandlerInterceptor { private static final Logger logger = LoggerFactory.getLogger(LogHandlerInterceptor.class); // controller执行前调用 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.debug("controller前调用"); return true; } // controller执行后页面渲染、 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { logger.debug("controller执行后进行的渲染"); } // 在DispatcherServlet 渲染了对应的视图之后执行,通常用它做资源清理工作 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.debug("在模板渲染后的处理事件"); } }
-
-
注册拦截器
拦截器定义完以后需要注册才能生效,我们可以自定义一个配置类(记得加上@configuration注解),同时要实现WebMvcConfigurer接口,并重写addInterceptors方法,方法参数是要配置的拦截器类对象
package com.lys.community.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyWebConfig implements WebMvcConfigurer { // 注入自定义的拦截器 @Autowired private LogHandlerInterceptor logHandlerInterceptor; // 注册拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(logHandlerInterceptor) // excludePathPatterns可以自定义不需要的拦截的目标,例如静态资源 .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.jpg", "/**/*.png") //addPathPatterns配置要拦截的目标 .addPathPatterns("/hello"); } }
-
测试
启动项目,在浏览器输入我们配置的要拦截的路径,然后回车,可以看到请求正确响应
再回头看idea控制台,可以看到拦截器里面的内容执行了
这样我们就可以在拦截器中写我们的逻辑处理代码了。