Spring boot下添加filter

一、定义一个Filter

 

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * Created by Administrator on 2018/4/6.
 */
@Order(1)
@WebFilter(filterName="Signature Filter")
public class SignatureFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        String administrator = servletRequest.getParameter("Administrator");
        if (checkSign(administrator)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        throw new ServletException(" The signature is invalid");
    }

    private boolean checkSign(String administrator) {
        //TODO
        return false;
    }

    @Override
    public void destroy() {

    }
}

 比较核心的代码是自定义类上面加上@WebFilter,其中@Order注解表示执行过滤顺序,值越小,越先执行

 

2、在Application中增加@ServletComponentScan

@SpringBootApplication
@ServletComponentScan
public class EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值