原创: SpringBoot中filter的使用详解及原理

SpringBoot中filter的使用详解及原理

正文链接参考

https://blog.csdn.net/u014627099/article/details/84565603

 

配置成功,务必在启动配置类前加如下

最后是springboot的主方法入口,注意,由于我们使用注解注入的Filter,所以要在下边这个Application类中加入@ServletComponentScan注解:

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);
	}
}

 自已书代三个步骤

1:创建过滤器类并注到Spring 容器

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" +
             
                " ''-'   `'-'    `-..-'              ");
    }
}

 

posted @ 2021-06-29 15:49  码哥之旅  阅读(482)  评论(0编辑  收藏  举报