package com.usoft.credit.config;

import com.usoft.credit.support.Des;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;

/**
 * 将加密的 userId password 进行解密
 * @author liuam
 * @since 2018/6/25 0025 下午 17:26
 */
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static final String key = "10101010";

    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {
        try {
            Des des = new Des();
            String userId = props.getProperty("pyconfig.userId");
            if (userId != null) {
                props.setProperty("pyconfig.userId", des.decrypt(userId, key));
            }

            String password = props.getProperty("pyconfig.password");
            if (password != null) {
                props.setProperty("pyconfig.password", des.decrypt(password, key));
            }
            // 自定义完成后调用父类方法
            super.processProperties(beanFactory, props);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BeanInitializationException(e.getMessage());
        }
    }

}
