package com.sptg.common.security.cfgbeans;

import com.sptg.common.security.interceptor.SecurityInterceptor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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;

/**
 * title: SecurityMvcConfigBean
 * package: com.sptg.common.security.cfgbeans
 * description: WEB相关配置
 *
 *                  
 *                 
 */
@Configuration
public class SecurityMvcConfigBean implements ApplicationContextAware, WebMvcConfigurer {

    private ApplicationContext applicationContext;

    /**
     * 添加拦截器
     * @param registry 拦截器注册表
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //  添加权限拦截器
        registry.addInterceptor(this.getSecurityInterceptor()).addPathPatterns("/**");
    }

    /**
     * 功能描述: 从Spring容器中获取Bean
     * @return 权限拦截器
     *                  
     *                       
     */
    private SecurityInterceptor getSecurityInterceptor(){
        return this.applicationContext.getBean(SecurityInterceptor.class);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
