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

网友投稿 416 2022-09-13


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

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(requeshttp://t,response);

}

@Override

public void destroy() {

System.out.println("拦截器销毁");

}

}

2 创建测试类LoginCotroller

package com.example.controller;

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

import org.sprinhttp://gframework.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方法初始化

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

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

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 addInterceptorpXYSVvxBTs(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 运行访问,查看效果

控制台输出如下


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:交换机的出现发展及基本配置
下一篇:计算机网络基础(二)-—IP地址详述(计算机网络基础中ip地址的计算)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~