package com.nokia.hb.config.web;

import com.nokia.hb.service.SessionService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    private final SessionService sessionService;

    public MyWebMvcConfigurer(SessionService sessionService) {
        this.sessionService = sessionService;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加请求日志拦截
        registry.addInterceptor(new RequestLogHandlerInterceptor())
                .addPathPatterns("/api/**");
        // 添加登录拦截
        registry.addInterceptor(new LoginHandlerInterceptor(sessionService))
                .addPathPatterns("/**")
                .excludePathPatterns("/login", "/error", "/api/userLogin", "/js/**", "/html/**", "/image/**", "/css/**",
                        "/swagger-ui/**", "/v3/**", "/webjars/**", "/doc**");
    }

    /**
     * 使用自定义DispatcherServlet
     */
    @Bean
    @Qualifier(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    public DispatcherServlet dispatcherServlet() {
        return new MyDispatcherServlet();
    }
}
