1. 程式人生 > >SpringBoot註冊Fiter(過濾器)和Listener(監聽器)

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<PayFilter> 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種類監聽器

此處以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<PayContextListener>  payListenerBean=new ServletListenerRegistrationBean<>();
        payListenerBean.setListener(payListener);
        System.out.println("PayContextListener被注入到spring容器");
        return  payListenerBean;
    }

}