SpringBoot环境添加过滤器

Java过滤器能够对目标资源的请求和响应进行截取,SpringBoot环境中定义过滤器按照如下步骤:

一、定义过滤器

Order注解是定义过滤器的执行顺序,值越小优先执行。

package com.test.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.springframework.core.annotation.Order;

@Order(1)
@WebFilter(filterName = "myFilter", urlPatterns = "/*")
public class MyFilter implements Filter{
	  @Override
	  public void init(FilterConfig filterConfig) throws ServletException {
	 
	  }
	 
	  @Override
	  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
			  FilterChain filterChain)
	      throws IOException, ServletException {
	    System.out.println("MyFilter...");
	    filterChain.doFilter(servletRequest,servletResponse);
	  }
	 
	  @Override
	  public void destroy() {
	 
	  }
}

二、SpringBoot启动类上添加ServletComponentScan注解

package com.test;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;


@SpringBootApplication
//@ServletComponentScan("com.test.util")
@EnableDubboConfiguration
//@ComponentScan("com.test.ctrl,com.test.util")
@ServletComponentScan
public class Starter extends SpringBootServletInitializer
{
    public static void main( String[] args )
    {
        SpringApplication.run(Starter.class,args);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值