没有Jasypt Spring的加密和解密属性文件

时间:2018-03-01 22:46:51

标签: java spring encryption password-encryption

我想使用datasource.xml中调用的静态方法解密我的密码。我不想使用Jasypt。

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      <property name="driverClassName"> 
            <value> ${sqltc.db.driverClassName}</value>

      <property name="url"> 
            <value> ${sqltc.db.url}</value>

      <property name="username"> 
            <value> ${sqltc.db.username}</value>

      <property name="password"> 
            <value>${sqltc.db.password}</value>
            <--- tried this Static Method
                <value>#{com.jsa.bla.batch.util.DecryptUtil.decrypt('${sqltc.db.password}')}</value>
                --->

    </bean>

--------- properties -------------

  sqltc.db.driverClassName=com.oracle.12c.driver
    sqltc.db.url=jdbc:oracle12c://oralce.web.net
    sqltc.db.username=oracle2
    sqltc.db.password= ZPAxPsrQk172638908==

----解密课----

Static Method 

public static String decrypt(String encryptedString) {
    String decryptedText = null;
    try{
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptedText = Base64.decodeBase64(encryptedString);
        byte[] plainText = cipher.doFinal(encryptedText);
        decryptedText = new String (plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedText;
    }   

1 个答案:

答案 0 :(得分:0)

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

  <property name="driverClassName">
    <value> ${sqltc.db.driverClassName}</value>

    <property name="url">
      <value> ${sqltc.db.url}</value>

      <property name="username">
        <value> ${sqltc.db.username}</value>

        <property name="password">
          <bean id="Decrypt" class="org.springframework.beans.factory.config.MethodInvokingBean">
            <property name="targetClass" value="com.jsa.bla.batch.util.DecryptUtil" />
            <property name="staticMethod" value="com.jsa.bla.batch.util.DecryptUtil.decrypt" />
            <property name="arguments"/>
              <list>
                <value>${sqltc.db.password}</value>
              </list>
          </bean>
          </property>