package com.docus.server.common.auth;

import com.docus.server.constants.BusinessConstant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class CloudBaseConfig implements WebMvcConfigurer {
    //请求参数拦截器
    @Bean("recoveryRequestContextInterceptor")
    public RequestContextInterceptor requestContextInterceptor() {
        return new RequestContextInterceptor();
    }

    //Feign请求拦截器
    @ConditionalOnMissingBean
    @Bean()
    public FeignRequestInterceptor feignRequestInterceptor() {
        return new FeignRequestInterceptor();
    }

    //token验证拦截器，不需要token验证的方法加@IgnoreVa7idate
    @Bean
    public TokenInterceptor tokenTntercentor() {
        return new TokenInterceptor(BusinessConstant.HEADER_API_TOKEN, BusinessConstant.HEADER_USER_ID, BusinessConstant.REDIS_KEY_USER_TOKEN_PREFIX);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> staticResource = Arrays.asList("/swagger-ui/**", "/webjars/**", "/v3/**", "/swagger-resources/**");
        registry.addInterceptor(tokenTntercentor()).excludePathPatterns(staticResource);
        registry.addInterceptor(requestContextInterceptor()).excludePathPatterns(staticResource);
    }


    @ConditionalOnMissingBean
    @Bean
    public RedisStringService redisStringService() {
        return new RedisStringService();
    }

}
