jasypt結合spring對數據庫密碼加密

1.jasypt介紹java

            官網:http://www.jasypt.org/,裏面有介紹jasypt是幹嗎的,官網記得介紹:Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.大體意思是:Jasypt是一個Java庫,容許開發者基本的加密功能添加到你所開發的項目以最少的投入,而不須要有密碼技術是如何工做的很深的造詣。因此只要使用就行。算法

 2.項目實際開發spring

    2.1 添加jar包sql

        通常項目是使用maven管理,因此咱們首先應該到,maven repository庫裏找到版本信息:maven

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>

    把上面的信息添加到pom.xml文件中進行。ide

    2.2 配置相關文件xmlthis

        第一種方式:配置xml文件加密

<bean id="environmentVariablesConfiguration"  
        class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">  
        <property name="algorithm" value="PBEWithMD5AndDES" />  
        <property name="password" value="root" />  
    </bean>  
    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">  
        <property name="config" ref="environmentVariablesConfiguration" />  
    </bean>  
    <bean id="propertyConfigurer"  
        class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">  
        <constructor-arg ref="configurationEncryptor" />  
        <property name="locations">  
            <list>  
                <value>/WEB-INF/config.properties</value>  
            </list>  
        </property>  
        <property name="fileEncoding" value="utf-8" />  
    </bean>

    最後,修改.properties配置中的明文密碼爲密文,這個須要本身寫一個main方法,對其進行加密,而後把加密的結果,填寫到.properties中:spa

public static void main(String[] args) {  
        //PBEWithMD5AndDES  
        BasicTextEncryptor encryptor = new BasicTextEncryptor();  
        encryptor.setPassword("root");  
        String encrypted = encryptor.encrypt("xxxx");  
        System.out.println(encrypted);  
    }

    最後更改.properties的信息:code

jdbc.password=ENC(jHv0WdiTLJFmOO08RQtUpg==)

    2.2第二種方式:修改配置文件

<bean id="encryptPropertyPlaceholderConfigurer"  
        class="com.util.EncryptPropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:config.properties</value>  
            </list>  
        </property>  
        <!--對配置文件中的指定屬性進行解密 -->  
        <property name="encryptedProps">  
            <set>  
                <value>orcl.password</value>  
                <value>sql.password</value>  
            </set>  
        </property>

    建立一個類:

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {  
  
    private Set<String> encryptedProps = Collections.emptySet();  
  
    public void setEncryptedProps(Set<String> encryptedProps) {  
        this.encryptedProps = encryptedProps;  
    }  
      
    @Override  
    protected String convertProperty(String propertyName, String propertyValue) {  
        if (encryptedProps.contains(propertyName)) {  
            return EncryptUtil.decode(propertyValue);  
        }  
        return super.convertProperty(propertyName, propertyValue);  
    }  
  
}

建立一個util加密算法:

    網上好多,這裏不列舉,只要能進行加密和進行解密的就行,不建議使用MD5,由於MD5是單向加密算法。

相關文章
相關標籤/搜索