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

SpringBoot 过滤器与拦截器实例演示

程序员文章站 2022-03-15 19:30:13
springboot中的过滤器拦截器操作与springmvc中的几乎一样所以这里也不过多介绍了,下面举两个简单的栗子演示一下1、过滤器        1 创建过滤器类loginfilter,实现ser...

   springboot中的过滤器拦截器操作与springmvc中的几乎一样所以这里也不过多介绍了,下面举两个简单的栗子演示一下

1、过滤器        

1 创建过滤器类loginfilter,实现servlet包下的filter接口(包不要导错),加入注解webfilter

package com.example.filter;
 
import javax.servlet.*;
import javax.servlet.annotation.webfilter;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
 
@webfilter(urlpatterns = "/filter/*")
public class loginfilter implements filter {
    @override
    public void init(filterconfig filterconfig) throws servletexception {
        system.out.println("初始化拦截器");
    }
 
    @override
    public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception {
        system.out.println("loginfilter");
        httpservletrequest request = (httpservletrequest)servletrequest;
        httpservletresponse response = (httpservletresponse)servletresponse;
        //放行
        filterchain.dofilter(request,response);
    }
 
    @override
    public void destroy() {
        system.out.println("拦截器销毁");
    }
}

     2 创建测试类logincotroller

package com.example.controller;
 
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@restcontroller
@requestmapping("/filter")
public class filtercontroller {
 
    @requestmapping("/login")
    public string login(){
        system.out.println("登录");
        return "login";
    }
 
}

      3 在启动类上加注解

package com.example;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.servlet.servletcomponentscan;
 
@springbootapplication
@servletcomponentscan
//servlet、filter、listener可以直接通过@webservlet、@webfilter、@weblistener注解自动注册
public class demoapplication {
 
    public static void main(string[] args) {
        springapplication.run(demoapplication.class, args);
    }
 
}

  4 启动测试,启动的时候可以看到filter调用init方法初始化

SpringBoot 过滤器与拦截器实例演示

 接着访问可以看到页面输出

SpringBoot 过滤器与拦截器实例演示

 控制台打印出拦截器中的语句

SpringBoot 过滤器与拦截器实例演示

2、拦截器        

1 创建自定义拦截器

package com.example.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 myinterceptor implements handlerinterceptor {
   //进入controller方法之前调用的
    @override
    public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {
        system.out.println("prehandle");
        return true;//true表示放行 false表示不放行
    }
    //调用完controller之后,视图渲染层之前
    @override
    public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception {
        system.out.println("posthandle");
    }
    //页面跳转之后,整个流程执行之后,一般用于资源的清理
    @override
    public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception {
        system.out.println("aftercompletion");
    }
}

      2 创建拦截器配置类,注意要加上配置类的注解

package com.example.config;
 
import com.example.interceptor.myinterceptor;
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 myinterceptorconfig implements webmvcconfigurer {
 
    @override
    public void addinterceptors(interceptorregistry registry) {
        //设置拦截器并指定拦截路径
        registry.addinterceptor(new myinterceptor()).addpathpatterns("/interceptor/*");
        //registry.addinterceptor(new myinterceptor()).addpathpatterns("/**");//拦截所有
        //registry.addinterceptor(new myinterceptor()).addpathpatterns("/**").excludepathpatterns("/test");//指定不拦截
 
        //添加自定义拦截器
        webmvcconfigurer.super.addinterceptors(registry);
    }
}

        3 创建logincontroller测试类

package com.example.controller;
 
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@restcontroller
@requestmapping("/interceptor")
public class interceptorcontroller {
    @requestmapping("login")
    public string login(){
        system.out.println("interceptor-login");
        return "login";
    }
}

      4 运行访问,查看效果

SpringBoot 过滤器与拦截器实例演示

控制台输出如下

SpringBoot 过滤器与拦截器实例演示

到此这篇关于springboot 过滤器与拦截器的文章就介绍到这了,更多相关springboot 过滤器与拦截器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!