• springboot拦截器之自定义拦截器Interceptor以及新旧配置的对比(四)


    1结构

     2loginController中的代码:

    package com.example.demo.controller.login;

    import java.util.HashMap;
    import java.util.Map;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;


    @RestController
    public class LoginController {

    private Map<String,Object> params=new HashMap<>();

    @RequestMapping(value="/api2/v1/account")
    public Object account() {
    System.out.println("controller中的account方法");
    params.put("money", "1000");
    return params;
    }


    }

     

    3自定义拦截器LoginIntercepter

    代码:

    package com.example.demo.Interceptor;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;

    public class LoginIntercepter implements HandlerInterceptor {

    /**
    * 进入controller方法之前
    */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    System.out.println("LoginIntercepter-------->preHandle");
    String token=request.getParameter("token");
    /*
    * if(token!=null) { response.getWriter().print("token不为空"); }else {
    * response.getWriter().print("token 为空"); }
    */
    return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
    * 调用controller之后,但是在视图渲染之前
    */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
    ModelAndView modelAndView) throws Exception {
    System.out.println("LoginIntercepter-------->postHandle");
    HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
    * 整个完成之后,通常用于资源清理
    */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    throws Exception {
    System.out.println("LoginIntercepter-------->afterCompletion");
    HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    }

    4@Configuaration

    旧版本  继承WebMvcConfigurerAdapter(springBoot2.x之前的老版本)

    package com.example.demo.Interceptor;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    /**
    * 旧版本的拦截器(springboot2.x之前的拦截器),不建议使用,我的版本是2.3.3,所以这里WebMvcConfigurerAdapter是横线
    * @author Administrator
    *
    */
    @Configuration
    public class OldIntercepter extends WebMvcConfigurerAdapter{

    /**
    * 添加旧版拦截器
    */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api/*/**");
    super.addInterceptors(registry);
    }

    }

    新版本  springBoot2.x 新版本配置拦截器 implements WebMvcConfigurer

    package com.example.demo.Interceptor;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    /**
    * jdk8之后,可以在接口中写方法,属于jdk的新特性
    * springboot2.x之后通过接口的形式引用拦截器
    * @author Administrator
    *
    */
    @Configuration
    public class NewIntercepter implements WebMvcConfigurer{

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    //拦截全部接口为
    registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
    WebMvcConfigurer.super.addInterceptors(registry);
    }

    }

    5run application

     

  • 相关阅读:
    08-STM32+W5500+AIR202基本控制篇-实现功能1,功能2和功能4服务器搭建完成-测试通信
    07-STM32+W5500+AIR202基本控制篇-实现功能1,功能2和功能4服务器搭建-安装MQTT软件(Linux系统)
    07-STM32+W5500+AIR202基本控制篇-实现功能1,功能2和功能4服务器搭建-安装MQTT软件(.Windows系统)
    Ceph剖析:Paxos算法实现
    Linux命令小结:crontab/netstat/iostat/sar
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    Prometheus监控+Grafana+Alertmanager告警安装使用 (图文详解)
    Go语言mgo使用情况
    gob,protobuf,json在golang中的序列化效率对比
    golang json性能分析详解
  • 原文地址:https://www.cnblogs.com/zhushilai/p/13564343.html
Copyright © 2020-2023  润新知