/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.authcore.config;

import com.codvision.authcore.interceptor.AuthInterceptor;
import com.codvision.authcore.service.IAuthService;
import com.codvision.authcore.service.impl.AuthService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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 javax.annotation.Resource;

/**
 * 认证配置类
 *
 * @author lingee
 * @date 2023/7/19
 */
@Configuration
@ConditionalOnProperty(value = "cvbp.auth.enabled", havingValue = "true", matchIfMissing = true)
public class AuthConfig implements WebMvcConfigurer {

    @Resource
    private AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor())
                .excludePathPatterns("/v1/sysUser/login",
                        "/v1/sysUser/verifyCode",
                        "/v1/sysUser/loginByMobile",
                        "/v1/sysUser/loginSignature",
                        "/v1/manager/sysConfig/getListByKeys",
                        "/v1/manager/sysFile/preview/**",
                        "/v1/manager/sysFile/download/**",
                        "/v1/manager/website/addRecord",
                        "/customCaptcha/get",
                        "/captcha/get",
                        "/captcha/check",
                        "/backend/**",
                        "/**/error",
                        "/**/doc.*",
                        "/**/swagger-ui.*",
                        "/swagger-resources/**",
                        "/**/webjars/**",
                        "/favicon.ico",
                        "/**/v2/api-docs/**")
                .addPathPatterns(authProperties.getIncludes());
    }

    @Bean
    @ConditionalOnMissingBean(name = "authCenterConfig")
    public IAuthService authService() {
        return new AuthService();
    }

    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

}
