zoukankan      html  css  js  c++  java
  • SpringBoot拦截器

    1、登陆拦截器案例

          1、自定义拦截器

    public class LoginHandlerInterceptor implements HandlerInterceptor {
    
    
        /**
         *
         * @param request
         * @param response
         * @param handler
         * @return
         * @throws Exception
         * 在每个项目中,拦截器都是我们经常会去使用的东西,基本上任一一个项目都缺不了拦截器的使用。
         * 如日志记录、登录验证,session验证等,都需要拦截器来拦截URL请求,那springboot中的拦截器是如何去使用的呢,我们一起试试
         * 首先,我们去创建一个名为LoginInterceptor的拦截器,来过滤请求,我们创建的拦截器要去实现HandlerInterceptor接口,
         * 然后定义我们的方法
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            //拦截路径方法
            HandlerMethod handlerMethod = (HandlerMethod) handler; // 这里报异常说明url匹配不到,springboot2.0修改为资源访问
            Method method = ((HandlerMethod) handler).getMethod();
            String methodName = method.getName();
            System.out.println("拦截的方法"+methodName);
            // 通过方法,可以获取该方法上的自定义注解,然后通过注解来判断该方法是否要被拦截
            // @UnInterception 是我们自定义的注解
            // @UnInterception 是我们自定义的注解
            UnInterception unInterception = method.getAnnotation(UnInterception.class);
            if (null != unInterception) {
                return true;
            }
            Object user = request.getSession().getAttribute("user");
            if(user==null){
                request.setAttribute("msg","你的权限不足");
                request.getRequestDispatcher("/index.html").forward(request,response);
                return false;
            }
            return true;
        }

     2、自定义注解

    /**
     * 该注解用来指定某个方法不用拦截
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface UnInterception {
    }

    3、配置类

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //addPathPatterns拦截资源 excludePathPatterns 排除那些资源被拦截
            //addInterceptor添加拦截器
            registry.addInterceptor(getLoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/*");
        }
        @Bean
        public  LoginHandlerInterceptor getLoginHandlerInterceptor(){
            return new LoginHandlerInterceptor();
        }
    }

    4、controller类

    @RestController
    public class HelloController {
    
        @Autowired
        private HelloService helloService;
        
        @RequestMapping("/index")//拦截
        public String index(){
            return helloService.sayHellStarter("1323");
        }
        @RequestMapping("/index")
        @UnInterception//不拦截
        public String register(){
            return "register";
        }
    }

    5、测试

  • 相关阅读:
    C# 隐式转换 显示转换
    C# 枚举几种写法细节
    C# System.Int32 与 int 区别
    JavaScript中的闭包
    JS Arguments对象
    分页存储过程 sql
    JS Select 选项清空
    WebGL学习笔记三
    WebGL学习笔记二
    WebGL学习笔记一
  • 原文地址:https://www.cnblogs.com/cxyyh/p/10628872.html
Copyright © 2011-2022 走看看