1. 程式人生 > 實用技巧 >Spring MVC 與 Spring Boot 應用程式接入統一密碼

Spring MVC 與 Spring Boot 應用程式接入統一密碼

Spring MVC 與 Spring Boot 應用程式接入統一密碼

在集中化環境中接入統一密碼的應用程式大部分均為springboot或springmvc應用程式,並且大部分的接入統一密碼都是應用啟動時所依賴的資料來源,若未使用封裝好的框架注入應用啟動的資料來源需要在自己的模組中進行下改造工作,大部分的改造都是通過下面的樣例:

1. Spring mvc

spring mvc 中一般的資料庫配置資訊均放在了classpath路徑下的jdbc.properties檔案中如下:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql.....
jdbc.username=DBUSERNAME
jdbc.password=DBPASSWORD

之後在spring的xml配置檔案中注入資料來源資訊大致如下

<context:property-placeholder location="classpath:jdbc.properties" file-encoding="utf-8"/>

......

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">  
    <!-- 資料庫基本資訊配置 -->
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="driverClassName" value="${jbdc.driverClassName}" />
    ......
</bean>

原始的流程大致如上所示,為了接入統一密碼,採用讀取配置中心拉取到指定路經的密碼檔案方式,如檔案路徑:

/opt/athena/passwd/csd-comp-passwd.properties

新增類繼承PropertyPlaceholderConfigurerPropertySourcesPlaceholderConfigurer 類,重寫方法convertProperty(String, String)

// 原始方法僅僅將value值原樣返回
protected String convertProperty(String propertyName, String propertyValue) {
    return convertPropertyValue(propertyValue);
}
// 樣例程式碼:此處為讀取密碼檔案解密形式,其他形式類似
public class SelfPropertyConvertConfigurer extends PropertySourcesPlaceholderConfigurer {

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        // 當屬性為密碼屬性時,讀取密碼檔案,呼叫密碼解密程式解密
        if ("jdbc.password".equals(propertyName)) {
            // 密碼標籤
			String resTag = "DBNAME_DBUSERNAME";
            // 密碼檔案路徑
            String pwdFilePath = "/opt/athena/passwd/csd-comp-passwd.properties";
            // 呼叫passwdauth-app.jar 與 passwdauth-jni.jar 中的密碼解密程式
            ResourceInfo resourceInfo = EncryptInterface.desDecryptDataByTag(resTag, pwdFilePath);
            Stirng passwd = resourceInfo.getDbPasswd();
            return passwd;
        } else {
            return propertyValue;
        }
    }
    
    /**
    @Override
    protected void processProperties(final ConfigurableListableBeanFactory beanFactoryToProcess, Properties 			paramProperties) throws BeansException{
    		......
    		// EncryptInterface.desDecryptDataByTag(String... params);
    		
    		......
    		super.processProperties(beanFactoryToProcess, paramProperties);
	}
	*/
    
}

將自定義擴充套件類加入spring管理

<bean class="com.sitech.comp.passwd.demo.SelfPropertyConvertConfigurer">
    <property name="locations">
        <list>
            <!-- 非密碼資訊仍從原來的jdbc.properteis檔案中讀取 -->
			<value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

2. Spring Boot

繼承org.springframework.boot.autoconfigure.jdbc.DataSourceProperties, 重寫determinePassword()方法

@Configuration
@Primary
public class DataSourceConfig extends DataSourceProperties {

    public static final Logger logger = Logger.getLogger(DataSourceConfig.class.getName());

    @Value("${spring.datasource.password:'not find password config'}")
    private String password;

    @Override
    public String determinePassword() {
        logger.info("encrypt password is " + this.password);
        this.password= EncryptInterface.decrypt(password);
        return password;
    }
    
}