package org.springblade.common.config;

import org.springblade.common.interceptor.IPInterceptor;
import org.springblade.common.listener.OnlinePeopleListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 17:16 2021/11/16
 * @Modified By:
 */
@Configuration
public class BladeMvcConfiguration implements WebMvcConfigurer {
	@Bean
	public IPInterceptor ipInterceptor(){
		return new IPInterceptor();
	}

	/**
	 * 添加拦截器
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//添加拦截器
		InterceptorRegistration interceptorRegistration = registry.addInterceptor(ipInterceptor());
		interceptorRegistration.addPathPatterns("/**");
	}

	/**
	 * 添加在线人数监听
	 * @return
	 */
	@Bean
	public ServletListenerRegistrationBean servletListenerRegistrationBean(){
		ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
		servletListenerRegistrationBean.setListener(new OnlinePeopleListener());
		return servletListenerRegistrationBean;
	}
}
