zoukankan      html  css  js  c++  java
  • springboot的interceptor(拦截器)的应用

    一、SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。在web开发中,拦截器是经常用到的功能。它可以帮我们验证是否登陆、预先设置数据以及统计方法的执行效率等等。spring中拦截器主要分两种,一个是HandlerInterceptor,一个是MethodInterceptor。这里我介绍HandlerInterceptor的一些用法

    二、继承HandlerInterceptor接口

    @Component
    public class LoginInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            return false;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    
        }
    }

    @Component 目的是加入容器管理

    三、配置、在springboot中需要配置线管请求处理,来实现放行的目的

    @Configuration
    public class LoginConfigurtion extends WebMvcConfigurerAdapter{
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new LoginInterceptor())
                    .addPathPatterns("/*")
                    .excludePathPatterns("/user/login");
        }
    }

    这样可以进行相关的内容处理,在使用上面也可以根据自己的需求来实现。

    preHandle方法的应用也很方便,通过就返回true。

    四、springmvc的处理和配置参考:http://www.cnblogs.com/ll409546297/p/6434102.html

  • 相关阅读:
    Ubuntu安装php7.0环境
    PHP-FPM参数详情
    phpize是干嘛的
    Ubuntu忘记密码
    Ubuntu下面删除和卸载软件
    Js验证正则表达式
    JS发送验证码;并设置cookie
    Shell脚本之sed的使用
    Bash基本功能:输入输出重定向
    shell常用快捷键
  • 原文地址:https://www.cnblogs.com/ll409546297/p/7423019.html
Copyright © 2011-2022 走看看