欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot实现过滤器和拦截器demo

程序员文章站 2024-03-05 17:14:25
整理文档,搜刮出一个spring boot实现过滤器和拦截器demo ,稍微整理精简一下做下分享。 拦截器定义: @webservlet public c...

整理文档,搜刮出一个spring boot实现过滤器和拦截器demo ,稍微整理精简一下做下分享。

拦截器定义:

@webservlet
public class actioninterceptor implements handlerinterceptor {

  @override
  public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler)
      throws exception {
    // system.out.println(">>>myinterceptor1>>>>>>>在请求处理之前进行调用(controller方法调用之前)");

    // 获取系统时间
    calendar ca = calendar.getinstance();
    int hour = ca.get(calendar.hour_of_day);
    // 设置限制运行时间 0-4点
    if (hour < 4) {
      return true;
    }
    return false;
  }

  @override
  public void posthandle(httpservletrequest request, httpservletresponse response, object handler,
      modelandview modelandview) throws exception {
    // system.out.println(">>>myinterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(controller方法调用之后)");

  }

  @override
  public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex)
      throws exception {
    // system.out.println(">>>myinterceptor1>>>>>>>在整个请求结束之后被调用,也就是在dispatcherservlet
    // 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");
  }
}

拦截器使用:  关于注解 我使用的是@component  其实也可能声明成配置

@component
public class applicationconfig {extends webmvcconfigureradapter 

  @override
  public void addinterceptors(interceptorregistry registry) {
    // 多个拦截器组成一个拦截器链
    // addpathpatterns 用于添加拦截规则
    // excludepathpatterns 用户排除拦截
    registry.addinterceptor(new actioninterceptor()).addpathpatterns("/service/extract/json/**");
    super.addinterceptors(registry);
  }
}

过滤器:

定义:

public class actionfilter implements filter {

  @override
  public void init(filterconfig filterconfig) throws servletexception {

  }

  @override
  public void dofilter(servletrequest request, servletresponse response, filterchain chain)
      throws ioexception, servletexception {
    // 获取系统时间
    calendar ca = calendar.getinstance();
    int hour = ca.get(calendar.hour_of_day);
    // 设置限制运行时间 0-4点
    if (hour < 4) {
      httpservletresponse httpresponse = (httpservletresponse) response;
      httpresponse.setcharacterencoding("utf-8");
      httpresponse.setcontenttype("application/json; charset=utf-8");
      
      // 消息
      map<string, object> messagemap = new hashmap<>();
      messagemap.put("status", "1");
      messagemap.put("message", "此接口可以请求时间为:0-4点");
      objectmapper objectmapper=new objectmapper();
      string writevalueasstring = objectmapper.writevalueasstring(messagemap);
      response.getwriter().write(writevalueasstring);
      
    } else {
      chain.dofilter(request, response);
    }

  }

  @override
  public void destroy() {

  }

}

使用:

@component
public class applicationconfig { 


  @bean
  public filterregistrationbean filterregistrationbean() {
    filterregistrationbean registrationbean = new filterregistrationbean();
    actionfilter actionfilter = new actionfilter();
    registrationbean.setfilter(actionfilter);
    list<string> urlpatterns = new arraylist<string>();
    urlpatterns.add("/service/extract/json/*");
    registrationbean.seturlpatterns(urlpatterns);
    return registrationbean;
  }
  

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。