JAVA过滤器首先字面意思可以大概知道具体的功能是做什么。
1、定义:
过滤器Filter,是Servlet的的一个实用技术了。可通过过滤器,对请求进行预处理,例如判断用户是否登录、判断访问的重定向访问url,访问参数是否为敏感词等等等等。
2、过滤器的实现方式(以springboot为例)
过滤器的实现方式有两种,第一种是利用WebFilter注解进行配置,第二种是使用FilterRegistrationBean方式。
2.1、WebFilter是Servlet3.0新增的注解,通过此注解,启动启动时会自动扫描自动注册,注意在启动类上还需使用@ServletComponentScan进行注解标注才可生效。如果注册了多个过滤器,要实现先后顺序执行,则需要配合@Order注解使用,其中通过@Order指定执行顺序,值越小,越先执行;也可以通过通过过滤器的名字先后顺序,进行顺序的约定。
//主启动类
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
//过滤器
@javax.servlet.annotation.WebFilter(filterName = "webFilter",urlPatterns = {"/*"})
@Order(1)
public class WebFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(WebFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
logger.info("doFilter 请求处理");
//处理操作
//链路 直接传给下一个过滤器
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
logger.info("destory");
}
}
2.2、FilterRegistrationBean是springboot提供的一个方法,不用@WebFilter注解和@Order注解。以一个例子,调用/hello请求,判断request中session为name的值是否存在,如果存在则返回“hello123!”,反之则返回“请登录!”。
//controller
@Controller
public class HelloController {
@RequestMapping("/index")
@ResponseBody
public String index(){
return "请登录!";
}
@RequestMapping("/hellos")
@ResponseBody
public String hellos(HttpServletRequest request){
request.getSession().setAttribute("name","hello");
return "hellos!";
}
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello123!";
}
}
//过滤器类
public class MyFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(MyFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info("过滤器init!");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
logger.info("过滤器执行!");
HttpServletRequest request = (HttpServletRequest) servletRequest;
String path=request.getRequestURI();
logger.info("request.getRequestURI():"+path);
if(request.getSession().getAttribute("name")==null || "".equals(request.getSession().getAttribute("name"))){
path = "/index";
request.getRequestDispatcher(path).forward(servletRequest,servletResponse);
}
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
logger.info("过滤器destroy!");
}
}
//config类
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.addUrlPatterns("/hello");
filterRegistrationBean.setName("myFilter");
filterRegistrationBean.setFilter(getMyFilter());
return filterRegistrationBean;
}
@Bean(name = "myFilter")
public Filter getMyFilter(){
return new MyFilter();
}
}
第一次直接执行/hello请求,此时session为空,则会显示“请登录!”
第二次试验,先执行/hellos请求,在执行/hello请求,此时应该显示“hello123!”
以上,过滤器配置基本完毕,小白一枚,还在不断学习...