• springboot 添加拦截器


    前提

    已经写好了一个controller

    public class HelloController {
    
        @GetMapping("/hello")
        public Object hello(){
            return "hello world";
        }
    }
    

    1.在controller包中创建一个interceptor包

    2.在interceptor包中创建一个UserTokenInterceptor.class 并去实现 HandlerInterceptor

    package com.imooc.controller.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class UserTokenInterceptor implements HandlerInterceptor {
    
        /**
         * 拦截请求,在访问controller调用之前
         * @param request
         * @param response
         * @param handler
         * @return
         * @throws Exception
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            System.out.println("进入到拦截器,被拦截。。");
    
            /**
             * false:请求被拦截,被驳回,验证出现问题
             * true: 请求在经过验证效验以后,是OK的,是可以放行
             */
            return false;
        }
    
        /**
         * 请求访问controller之后,渲染视图之前
         * @param request
         * @param response
         * @param handler
         * @param modelAndView
         * @throws Exception
         */
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
        }
    
        /**
         * 请求访问controller之后,渲染视图之后
         * @param request
         * @param response
         * @param handler
         * @param ex
         * @throws Exception
         */
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
        }
    }
    
    

    3.注册到容器里

    package com.imooc.config;
    
    import com.imooc.controller.interceptor.UserTokenInterceptor;
    import org.springframework.context.annotation.Bean;
    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 WebMvcConfig implements WebMvcConfigurer {
    
        @Bean
        public UserTokenInterceptor userTokenInterceptor(){
            return new UserTokenInterceptor();
        }
    
        /**
         * 注册拦截器
         * @param registry
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(userTokenInterceptor())
                    .addPathPatterns("/hello");
    
            WebMvcConfigurer.super.addInterceptors(registry);
        }
    }
    
    

    4.请求/hello

  • 相关阅读:
    初赛Part2
    数据结构
    ES 匹配条件后分组聚合
    SpringBoot starter 懒人包介绍
    HTTP1.1中容易忽视的知识点
    ​netcore 中间件浅析
    spring boot请求参数验证
    spring boot中统一对响应做处理
    并发请求工具
    抓apk中的https包(​含破解https的SSL Pinning)
  • 原文地址:https://www.cnblogs.com/hardy-wang/p/13857202.html
Copyright © 2020-2023  润新知