package com.lantone.qc.security.config;


import com.lantone.qc.security.exception.CommonErrorCode;
import com.lantone.qc.security.exception.CommonException;
import com.lantone.qc.security.util.SerialNumberUtil;
import com.lantone.qc.security.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.Map;

/**
 * @Description: 安全拦截启动配置
 * @author: gaodm
 * @time: 2019/12/24 10:41
 */
@Configuration
@Order(1)
@Slf4j
@ConditionalOnProperty(prefix = "mysecurity", value = { "enable" }, havingValue = "true")
public class SecurityConfigurer {

    @Bean
    public void SecurityCheck() {
        log.info("启动安全拦截");
        // 获取本地配置信息
        Map<String, String> localMap = SerialNumberUtil.getAllSn();
        // 读取配置文件到全局参数tokenMap
        Map<String, String> tokenMap = TokenUtil.getFileContent();

        System.out.println("本地配置信息：" + localMap);
        System.out.println("文件配置信息：" + tokenMap);

        // 只校验linux配置信息，windows打包如果配置不一致会报错，故屏蔽
        if ("LINUX".equals(localMap.get("server.os"))) {
            // 比对参数：cpu 是否一致
            String param = "server.cpu";
            if (!localMap.get(param).equals(tokenMap.get(param))) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "启动失败，配置信息不一致");
            }
            log.info("启动校验通过");
        } else {
            log.info("当前系统不校验配置信息");
        }

    }

}
