SpringBoot中如何详细实现HandlerInterceptor拦截器功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计745个文字,预计阅读时间需要3分钟。
前言:在平常项目开发过程中,会遇到登录拦截、权限校验、参数处理、防止重复提交等问题。那截断器就能帮助我们统一处理这些问题。一、实现方式
1.1 自定义截断器自定义截断器
前言
平常项目开发过程中,会遇到登录拦截,权限校验,参数处理,防重复提交等问题,那拦截器就能帮我们统一处理这些问题。
一、实现方式
1.1 自定义拦截器
自定义拦截器,即拦截器的实现类,一般有两种自定义方式:
定义一个类,实现org.springframework.web.servlet.HandlerInterceptor接口。
定义一个类,继承已实现了HandlerInterceptor接口的类,例如org.springframework.web.servlet.handler.HandlerInterceptorAdapter抽象类。
1.2 添加Interceptor拦截器到WebMvcConfigurer配置器中
自定义配置器,然后实现WebMvcConfigurer配置器。
以前一般继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter类,不过SrpingBoot 2.0以上WebMvcConfigurerAdapter类就过时了。有以下2中替代方法:
直接实现org.springframework.web.servlet.config.annotation.WebMvcConfigurer接口。(推荐)
继承org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport类。但是继承WebMvcConfigurationSupport会让SpringBoot对mvc的自动配置失效。不过目前大多数项目是前后端分离,并没有对静态资源有自动配置的需求,所以继承WebMvcConfigurationSupport也未尝不可。
二、HandlerInterceptor 方法介绍
preHandle:预处理,在业务处理器处理请求之前被调用,可以进行登录拦截,编码处理、安全控制、权限校验等处理;
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; }
postHandle:后处理,在业务处理器处理请求执行完成后,生成视图之前被调用。即调用了Service并返回ModelAndView,但未进行页面渲染,可以修改ModelAndView,这个比较少用。
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { }
afterCompletion:返回处理,在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。已经渲染了页面。
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { }
三、拦截器(Interceptor)实现
3.1 实现HandlerInterceptor
此拦截器演示了通过注解形式,对用户权限进行拦截校验。
package com.nobody.interceptor; import com.nobody.annotation.UserAuthenticate; import com.nobody.context.UserContext; import com.nobody.context.UserContextManager; import com.nobody.exception.RestAPIError; import com.nobody.exception.RestException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.github.com/LucioChn/springboot-common.git
到此这篇关于SpringBoot之HandlerInterceptor拦截器的使用详解的文章就介绍到这了,更多相关SpringBoot HandlerInterceptor拦截器内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计745个文字,预计阅读时间需要3分钟。
前言:在平常项目开发过程中,会遇到登录拦截、权限校验、参数处理、防止重复提交等问题。那截断器就能帮助我们统一处理这些问题。一、实现方式
1.1 自定义截断器自定义截断器
前言
平常项目开发过程中,会遇到登录拦截,权限校验,参数处理,防重复提交等问题,那拦截器就能帮我们统一处理这些问题。
一、实现方式
1.1 自定义拦截器
自定义拦截器,即拦截器的实现类,一般有两种自定义方式:
定义一个类,实现org.springframework.web.servlet.HandlerInterceptor接口。
定义一个类,继承已实现了HandlerInterceptor接口的类,例如org.springframework.web.servlet.handler.HandlerInterceptorAdapter抽象类。
1.2 添加Interceptor拦截器到WebMvcConfigurer配置器中
自定义配置器,然后实现WebMvcConfigurer配置器。
以前一般继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter类,不过SrpingBoot 2.0以上WebMvcConfigurerAdapter类就过时了。有以下2中替代方法:
直接实现org.springframework.web.servlet.config.annotation.WebMvcConfigurer接口。(推荐)
继承org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport类。但是继承WebMvcConfigurationSupport会让SpringBoot对mvc的自动配置失效。不过目前大多数项目是前后端分离,并没有对静态资源有自动配置的需求,所以继承WebMvcConfigurationSupport也未尝不可。
二、HandlerInterceptor 方法介绍
preHandle:预处理,在业务处理器处理请求之前被调用,可以进行登录拦截,编码处理、安全控制、权限校验等处理;
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; }
postHandle:后处理,在业务处理器处理请求执行完成后,生成视图之前被调用。即调用了Service并返回ModelAndView,但未进行页面渲染,可以修改ModelAndView,这个比较少用。
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { }
afterCompletion:返回处理,在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。已经渲染了页面。
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { }
三、拦截器(Interceptor)实现
3.1 实现HandlerInterceptor
此拦截器演示了通过注解形式,对用户权限进行拦截校验。
package com.nobody.interceptor; import com.nobody.annotation.UserAuthenticate; import com.nobody.context.UserContext; import com.nobody.context.UserContextManager; import com.nobody.exception.RestAPIError; import com.nobody.exception.RestException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.github.com/LucioChn/springboot-common.git
到此这篇关于SpringBoot之HandlerInterceptor拦截器的使用详解的文章就介绍到这了,更多相关SpringBoot HandlerInterceptor拦截器内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

