SpringBoot 过滤器与 * 实例演示
作者:宇智波波奶茶 时间:2021-08-26 08:33:23
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方法初始化
接着访问可以看到页面输出
控制台打印出 * 中的语句
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 运行访问,查看效果
控制台输出如下
来源:https://blog.csdn.net/layonly/article/details/121390506
标签:SpringBoot,过滤器, ,
0
投稿
猜你喜欢
Android中双击返回键退出应用实例代码
2022-02-20 00:28:44
C# Socket通信的实现(同时监听多客户端)
2023-07-11 09:25:10
Springboot工具类FileCopyUtils使用教程
2023-10-15 18:56:36
Java线程中sleep和wait的区别详细介绍
2021-08-27 02:23:12
浅谈Java字符串比较的三种方法
2023-05-13 12:26:51
java实现倾斜水印铺满整张图
2023-12-24 22:34:21
C#中泛型举例List<T>与DataTable相互转换
2021-07-06 18:54:08
Java如何通过枚举实现有限状态机
2021-08-05 04:21:05
java实现递归文件列表的方法
2022-10-13 13:17:00
Java11中基于嵌套关系的访问控制优化详解
2021-12-28 18:54:20
解决dubbo错误ip及ip乱入问题的方法
2023-08-06 17:18:02
Spring Cloud Config 使用本地配置文件方式
2021-06-03 03:17:53
一文搞懂并学会使用SpringBoot的Actuator运行状态监控组件的详细教程
2022-01-31 10:28:23
MyBatis的注解使用、ORM层优化方式(懒加载和缓存)
2023-04-24 09:16:13
mybatis多表查询的实现(xml方式)
2021-07-11 01:21:38
登录验证全局控制的几种方式总结(session)
2022-09-03 15:25:41
Spring Boot缓存实战 EhCache示例
2023-08-30 12:23:35
Input系统之InputReader处理按键事件详解
2023-11-09 20:57:05
linux环境下java程序打包成简单的hello world输出jar包示例
2023-11-26 11:11:37
深入解析java中的静态代理与动态代理
2023-08-23 13:10:56