重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要为大家展示了“SpringBoot2中多个拦截器配置和使用场景的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“SpringBoot2中多个拦截器配置和使用场景的示例分析”这篇文章吧。
成都创新互联公司专注于通道网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供通道营销型网站建设,通道网站制作、通道网页设计、通道网站官网定制、微信平台小程序开发服务,打造通道网络公司原创品牌,更为您提供通道网站排名全网营销落地服务。
拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。 拦截器主要用来按照指定规则拒绝请求。
Token令牌验证 请求数据校验 用户权限校验 放行指定接口
自定义类实现HandlerInterceptor接口
1)OneInterceptor 拦截器
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 拦截器一 */ public class OneInterceptor implements HandlerInterceptor { private static final Logger LOGGER = LoggerFactory.getLogger(OneInterceptor.class.getName()); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { String url =String.valueOf(request.getRequestURL()) ; LOGGER.info("1、url=="+url); // 放开拦截 return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { LOGGER.info("1、postHandle"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { LOGGER.info("1、afterCompletion"); } }
2)TwoInterceptor 拦截器
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 拦截器二 */ public class TwoInterceptor implements HandlerInterceptor { private static final Logger LOGGER = LoggerFactory.getLogger(TwoInterceptor.class.getName()); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { String url =String.valueOf(request.getRequestURL()) ; LOGGER.info("2、url=="+url); // 放开拦截 return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { LOGGER.info("2、postHandle"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { LOGGER.info("2、afterCompletion"); } }
import com.boot.intercept.intercept.OneInterceptor; import com.boot.intercept.intercept.TwoInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * Web配置文件 */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { public void addInterceptors(InterceptorRegistry registry) { // 拦截所有路径 // 注册自定义两个拦截器 registry.addInterceptor(new OneInterceptor()).addPathPatterns("/**"); registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/**"); } }
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class InterceptController { @RequestMapping("/reqUrl") public String reqUrl (){ return "success" ; } }
日志输出内容如下
intercept.OneInterceptor : 1、url==http://127.0.0.1:8005/reqUrl intercept.TwoInterceptor : 2、url==http://127.0.0.1:8005/reqUrl intercept.TwoInterceptor : 2、postHandle intercept.OneInterceptor : 1、postHandle intercept.TwoInterceptor : 2、afterCompletion intercept.OneInterceptor : 1、afterCompletionla
拦截器的拦截顺序,是按照Web配置文件中注入拦截器的顺序执行的。
以上是“SpringBoot2中多个拦截器配置和使用场景的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!