Spring Boot使用过滤器Filter

Spring Boot使用过滤器Filter

出处:使用步骤
具体使用案例可参考:对路径中含有api字样且userName为xm的请求则放过进行,否则其他情况进行拦截

1、过滤器含义

过滤器是对数据进行过滤,预处理过程,当我们访问网站时,有时候会发布一些敏感信息,发完以后有的会用*替代,还有就是登陆权限控制等,一个资源,没有经过授权,肯定是不能让用户随便访问的,这个时候,也可以用到过滤器。过滤器的功能还有很多,例如实现URL级别的权限控制、压缩响应信息、编码格式等等。
过滤器依赖servlet容器。在实现上基于函数回调,可以对几乎所有请求进行过滤。

2、使用步骤(实践)

(1)pom.xml

在pom.xml 中引入spring-boot-starter-web包。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

(2)新建filter 类

@Order(1)  //表示过滤器的顺序
//WebFilter:表示这个class是过滤器
@WebFilter(filterName = "DemoFilter", urlPatterns = "/*" , initParams = {
        @WebInitParam(name = "URL", value = "http://localhost:8080")})
public class DemoFilter implements Filter {

    private String url;
    /**
     * 可以初始化Filter在web.xml里面配置的初始化参数
     * filter对象只会创建一次,init方法也只会执行一次。
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.url = filterConfig.getInitParameter("URL");
        System.out.println("4我是过滤器的初始化方法!URL=" + this.url +  ",生活开始.........");
    }

    /**
     * 主要的业务代码编写方法
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("1我是过滤器的执行方法,客户端向Servlet发送的请求被我拦截到了");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("3我是过滤器的执行方法,Servlet向客户端发送的响应被我拦截到了");
    }

    /**
     * 在销毁Filter时自动调用。
     */
    @Override
    public void destroy() {
        System.out.println("我是过滤器的被销毁时调用的方法!,活不下去了................" );
    }
}

@Order(1):表示过滤器的顺序,假设我们有多个过滤器,你如何确定过滤器的执行顺序?这个注解就是规定过滤器的顺序。
@WebFilter:表示这个class是过滤器。
里面的参数,filterName 为过滤器名字,urlPatterns 为过滤器的范围,initParams 为过滤器初始化参数。
过滤器里面的三个方法
init : filter对象只会创建一次,init方法也只会执行一次。
doFilter : 主要的业务代码编写方法,可以多次重复调用
destroy : 在销毁Filter时自动调用(程序关闭或者主动销毁Filter)。

(3)建立controller类

@RestController
@RequestMapping("/shop")
@Api(value = "商品信息管理")
public class ShopGoodsController {
    @Autowired
    private ShopGoodsMapper shopGoodsMapper;

    //测试filter
    @GetMapping("/testFilter")
    public String testFilter(){
        System.out.println("2控制类里面的方法,我正在思考。。。");
        return"hhh";
    }
    }

(4)启动类中增加注解,自动注册Filter

@ServletComponentScan :在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。

@SpringBootApplication
@ServletComponentScan
public class SpringBootFiFilterApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootFiFilterApplication.class, args);
    }

}

(5)测试

浏览器中输入:http://localhost:8999/shop/testFilter
查看控制台:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值