原创: SpringBoot中filter的使用详解及原理
SpringBoot中filter的使用详解及原理
正文链接参考
https://blog.csdn.net/u014627099/article/details/84565603
配置成功,务必在启动配置类前加如下
最后是springboot的主方法入口,注意,由于我们使用注解注入的Filter,所以要在下边这个Application类中加入@ServletComponentScan注解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.example.executor_test; import org.omg.CORBA.PRIVATE_MEMBER; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.ConfigurableApplicationContext; import com.example.executor_test.task.OldTaskThread; @SpringBootApplication @ServletComponentScan public class ExecutorTestApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(ExecutorTestApplication. class , args); } }<br><br> |
自已书代三个步骤
1:创建过滤器类并注到Spring 容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.ruoyi.framework.shiro.web.filter.ibaiqi; import com.ruoyi.common.utils.ShiroUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * @Author zhangxuDev * @Date 2021-06-29 14:58 * @Version 1.0.0 */ @Order ( 1 ) @WebFilter (filterName = "javaFilterTest" ,urlPatterns = "/*" ) public class JavaFilterTest implements Filter { // private static final Logger LOGGER = LoggerFactory.getLogger(JwtFilter.class); private static final Logger LOGGER=LoggerFactory.getLogger(JavaFilterTest. class ); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println( "javaFilterTest.java 过滤器1 " ); System.out.println(servletRequest.getCharacterEncoding()); System.out.println(servletRequest.getServletContext().getContextPath()); System.out.println(servletRequest.getServletContext()); System.out.println(servletResponse); } @Override public void destroy() { } } |
2:在配置器类中添加扫描过虑器注解
package com.ruoyi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.web.servlet.ServletComponentScan; /** * 启动程序 * * @author ruoyi */ @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @ServletComponentScan //为扫描注解Filter 过虑器 public class RuoYiApplication { public static void main(String[] args) { // System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(RuoYiApplication.class, args); System.out.println("(♥◠‿◠)ノ゙ 若依启动成功 ლ(´ڡ`ლ)゙ \n" + " ''-' `'-' `-..-' "); } }
做产品的程序,才是好的程序员!
分类:
spring boot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!