package com.rf.psychological.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lpf
 * @description:
 * @date 2021/12/2822:06
 */
@Configuration
@Profile("!public")
public class JWTInterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private UserCheckInterceptor userCheckInterceptor;

    @Autowired
    private JWTInterceptor jwtInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //定义排除swagger访问的路径配置
        String[] swaggerExcludes = new String[]{"/swagger-ui.html", "/swagger-resources/**", "/csrf", "/webjars/**"};
        String[] institutionLogin = new String[]{"/user/login", "/institution/loginV2", "/admin/login", "/superAdmin/login"};
        String[] webLogin = new String[]{"/user/authCode", "/user/saveUser", "/user/webLogin", "/user/webUpdatePassword", "/user/*/checkUser"};
        String[] automaticImport = new String[]{"/category/loadCategory", "/subjectInfo", "/subjectInfoList", "/importTest", "/subjectInfoUpload/importTest", "/subjectInfo/update", "/subjectInfoUpload/update"};
        String[] wxPay = new String[]{"/api/wx-pay/native/notify","/api/wx-pay/refunds/notify","/api/wx-pay/code2openid"};
        String[] systemApi = new String[]{"/system/login","/system/forgotPassword","/system/registerUser","/system/update/*","/v1/getToken","/v1/registerUser"};
        String[] temporarily = new String []{"/category/**","/api/wx-pay/**"};
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/index**", "/error")
                .excludePathPatterns(institutionLogin)
                .excludePathPatterns(automaticImport)
                .excludePathPatterns(wxPay)
                .excludePathPatterns(systemApi)
                .excludePathPatterns("/user/saveUserV2", "/institution/saveUser")
                .excludePathPatterns(webLogin)
                .excludePathPatterns("/user/LANSaveUserInfo", "/user/LANLogin", "/user/LANUpdatePassword")
                .excludePathPatterns("/file/**", "/result/download/**")
                .excludePathPatterns("/download/**")
                .excludePathPatterns("/apk/**")
                .excludePathPatterns("/device/**")
                .excludePathPatterns("/param/**")
                .excludePathPatterns("/userImport")
                .excludePathPatterns("/test/token")
                .excludePathPatterns(swaggerExcludes)
//                .excludePathPatterns(temporarily)
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/druid/**")
                .excludePathPatterns("/static/**");//排除静态资源
        registry.addInterceptor(userCheckInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/index.html", "/error")
                .excludePathPatterns(institutionLogin)
                .excludePathPatterns(automaticImport)
                .excludePathPatterns(wxPay)
                .excludePathPatterns(systemApi)
                .excludePathPatterns("/user/saveUserV2", "/institution/saveUser")
                .excludePathPatterns(webLogin)
                .excludePathPatterns("/user/LANSaveUserInfo", "/user/LANLogin", "/user/LANUpdatePassword")
                .excludePathPatterns("/file/**", "/result/download/**")
                .excludePathPatterns("/download/**")
                .excludePathPatterns("/apk/**")
                .excludePathPatterns("/device/**")
                .excludePathPatterns("/param/**")
                .excludePathPatterns("/userImport")
                .excludePathPatterns("/test/token")
                .excludePathPatterns(swaggerExcludes)
//                .excludePathPatterns(temporarily)
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/druid/**")
                .excludePathPatterns("/static/**");//排除静态资源

    }

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