一、定义一个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);
}
}