SpringBoot过滤器编写(简单登录过滤器为例)
首先,大家都知道在有web.xml文件的项目中,我们直接在web.xml文件中配置过滤器就可以了,然而springboot框架中一般没有web.xml文件,那么我们在springboot中仍然有两种方式配置过滤器。
方案一:通过注解的方式配置过滤器(两个注解)
@WebFilter(注解一)
我们需要在过滤器实现类上添加@WebFilter注解,此注解可以设置url匹配模式,过滤器名称等。@WebFilter这个注解是Servlet3.0的规范,并不是Spring boot提供的。
@WebFilter 用于将一个类声明为==过滤器==,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为过滤器。该注解具有下表给出的一些常用属性 ( 以下所有属性均为可选属性,但是 value、urlPatterns、servletNames 三者必需至少包含一个,且 value 和 urlPatterns 不能共存,如果同时指定,通常忽略 value 的取值 )
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName = "myFilter",urlPatterns = "/*")
public class MyFilter implements Filter {
String NO_LOGIN = "您还没有登录";
String[] includeUrls = new String[]{"/login","/login_in","/home"};
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
HttpServletResponse httpServletResponse=(HttpServletResponse)servletResponse;
HttpSession httpSession=httpServletRequest.getSession();
String url=httpServletRequest.getRequestURI();
boolean needFilter = isNeedFilter(url);
//放行静态资源
if(url.endsWith(".css")||url.endsWith(".js")||url.endsWith(".jpg")
||url.endsWith(".gif")||url.endsWith(".png")){
filterChain.doFilter(servletRequest,servletResponse);
return;
}
if(!needFilter){ //需要放行的请求
filterChain.doFilter(servletRequest,servletResponse);
}else{ //进行拦截返回到登录页
String requestType = httpServletRequest.getHeader("X-Requested-With");
//判断请求是否为ajax请求
if(requestType!=null && "XMLHttpRequest".equals(requestType)){
//是ajax请求返回提示信息
httpServletResponse.getWriter().write(this.NO_LOGIN);
}else{//不是ajax请求,重定向到登录页
httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login");
}
return;
}
}
@Override
public void destroy() {
}
public boolean isNeedFilter(String url){
for(String includeUrl:includeUrls){
if(includeUrl.equals(url)){
return false;
}
}
return true;
}
}
@ServletComponentScan(注解二)
需要在springboot启动类上添加@ServletComponentScan("过滤器所在包")
在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码,例如:
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.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.example"})
@MapperScan("com.example.demo.mapper")
@ServletComponentScan("com.example.demo.filter")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
方案二:FilterRegistrationBean来完成配置。
现在我们通过过滤器来实现记录请求执行时间的功能,其实现如下:
public class LogCostFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
long start = System.currentTimeMillis();
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("Execute cost="+(System.currentTimeMillis()-start));
}
@Override
public void destroy() {
}
}
在Spring boot中,我们需要FilterRegistrationBean来完成配置
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new LogCostFilter());
registration.addUrlPatterns("/*");
registration.setName("LogCostFilter");
registration.setOrder(1);
return registration;
}
}
上一篇: 两大注册中心比较(Zookeeper,Eureka)
下一篇: cmd中输javac,找不到文件