package com.bohuikeji.frame.module.admin.config;

import com.bohuikeji.frame.module.admin.interceptor.LoginInterceptor;
import com.bohuikeji.frame.module.admin.interceptor.MenuInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
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 10552
 */
@Configuration
public class AdapterConfig implements WebMvcConfigurer {

    /**
     * 自定义静态资源映射(接口文档)
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    /**
     * 登录拦截器
     */
    @Primary
	@Bean
    LoginInterceptor localInterceptor() {return new LoginInterceptor();}
    /**
     * 菜单页面拦截器
     * @return
     */
    @Bean
    MenuInterceptor menuLnterceptor(){return new MenuInterceptor();}



    /**
     * 注册所以拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 拦截所有请求
         */
    	InterceptorRegistration loginRegistry=registry.addInterceptor(localInterceptor()).addPathPatterns("/**");
        /**
         * 排除
         */
        loginRegistry.excludePathPatterns(
                "/login/**",
                "/static/**",
                "/api/**",
                "/error",
                "/doc.html",
                "/webjars/**",
                "/upload/**",
                "/swagger-resources/**",
                "/v2/api-docs-ext",
                "/views/login/**",
                "/views/print",
                "/views/treaty/**",
                "/app/index",
                "/app/new/download/page/*",
                "/sys/application/shutdown",
                "/test/**",
                "/views/open/**",
                "/su/**",
                "/views/authorized",
                "/grant/authorized/login",
                "/grant/get/malt/code",
                "/third/**"
        );

        /**
         * 拦截页面
         */
        registry.addInterceptor(menuLnterceptor()).addPathPatterns("/views/**")
                .excludePathPatterns(
                        "/login/**",
                        "/static/**",
                        "/api/**",
                        "/error",
                        "/doc.html",
                        "/webjars/**",
                        "/upload/**",
                        "/swagger-resources/**",
                        "/v2/api-docs-ext",
                        "/views/login/**",
                        "/views/print",
                        "/views/treaty/**",
                        "/app/index",
                        "/app/new/download/page/*",
                        "/sys/application/shutdown",
                        "/test/**",
                        "/views/open/**",
                        "/views/authorized/**",
                        "/third/**"
                );


    }
}
