package cn.reghao.oss.console.config.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author reghao
 * @date 2021-12-30 12:34:26
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    private final ConsoleAuthInterceptor consoleAuthInterceptor;

    public WebConfig(ConsoleAuthInterceptor consoleAuthInterceptor) {
        this.consoleAuthInterceptor = consoleAuthInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(consoleAuthInterceptor);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/imgs/**")
                .addResourceLocations("classpath:/static/imgs/");
        registry.addResourceHandler("/js/**")
                .addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/lib/**")
                .addResourceLocations("classpath:/static/lib/");
    }

    /**
     * TODO 解决 RequestContextHolder.getRequestAttributes() 空指针问题
     *
     * @param
     * @return
     * @date 2024-02-27 14:34:56
     */
    @Bean
	public RequestContextListener requestContextListener(){
		return new RequestContextListener();
	}
}
