SpringBoot注册Fiter(过滤器)和Listener(监听器)

一、SpingBoot注册Filter

   1、创建filter类及其注册配置类,用于过滤某些需要登陆后才可以访问的接口

自定义过滤器
@Component
public class PayFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest  request= (HttpServletRequest) servletRequest;
        Boolean f= (Boolean) request.getSession().getAttribute("login");
        if (f!=null&&f){
           filterChain.doFilter(servletRequest,servletResponse);
        }else {
            System.out.println("***********您还未登陆,请登录后操作***********");
        }
    }

    @Override
    public void destroy() {}
}
向spring容器中注册PayFilter
@Configuration
public class PayFilterConfigure {
    private final PayFilter  payFilter;
    @Autowired
    public PayFilterConfigure(PayFilter payFilter) {
        this.payFilter = payFilter;
    }
    @Bean
    public FilterRegistrationBean  registrationBean(){
        FilterRegistrationBean bean=new FilterRegistrationBean<>();
        bean.setFilter(payFilter);
        bean.addUrlPatterns("/pay/*");  //过滤/pay相关的url
        return bean;
    }
}

    2、创建支付和登陆的controller类

//登陆接口
@Controller
public class UserCtl {
    @GetMapping("/login")
    @ResponseBody
    public  String  login(HttpServletRequest request){
        request.getSession().setAttribute("login",true);
        return "登陆成功";
    }
}

//支付接口
@RestController
@RequestMapping("/pay")
public class PayCtl {
    @GetMapping("/order")
    public String    pay(){
        return "恭喜您,订单支付成功";
    }
}

二、SpringBoot注册监听器

 javaweb共有8种类监听器

SpringBoot注册Fiter(过滤器)和Listener(监听器)_第1张图片此处以ServletContextListener为例

/**
 * 自定义监听器
 */
@Component
public class PayContextListener implements HttpSessionListener {

    private int count;

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        count++;
        ServletContext servletContext= httpSessionEvent.getSession().getServletContext();
        servletContext.setAttribute("count",count);
        System.out.println("PayContextListener监听在线人数count="+count+"人");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
       count--;
        ServletContext servletContext= httpSessionEvent.getSession().getServletContext();
        servletContext.setAttribute("count",count);
    }
}


/**
 * 配置监听器
 */
@Configuration
public class PayListenerConfigure {

   private final PayContextListener payListener;

    @Autowired
    public PayListenerConfigure(PayContextListener payListener) {
        this.payListener = payListener;
    }

    @Bean
    public ServletListenerRegistrationBean  payListener(){
        ServletListenerRegistrationBean  payListenerBean=new ServletListenerRegistrationBean<>();
        payListenerBean.setListener(payListener);
        System.out.println("PayContextListener被注入到spring容器");
        return  payListenerBean;
    }

}

 

你可能感兴趣的:(javaweb)