使用jasypt-1.5加密Spring的db属性文件 (如db.properties)
工具包准备:
/index.html
/maven2/org/jasypt/jasypt/1.5/
/encrypting-configuration.html
下载工具包后,会用到几个jar:
jasypt-1.5.jar
icu4j-3.8.jar 可另外下载
commons-lang-2.1.jar
commons-codec-1.1.jar
jasypt-cli-bundle.jar 
配置环境变量:
JASYPT_HOME=C:\Java\jasypt-1.5
2.JASYPT_CLASSPATH=.;%JASYPT_HOME%\bin\jasypt-cli-bundle.jar;%JASYPT_HOME%\lib\commons-codec-1.1.jar;%JASYPT_HOME%\lib\commons-lang-2.1.jar;%JASYPT_HOME%\lib\icu4j-3.8.jar;%JASYPT_HOME%\lib\jasypt-1.5.jar
.在 path下添加JASYPT_HOME\bin
加密公式:明文(可以是要加密的密码) + 密钥 =Jasypt默认算法=> 密文
使用命令生成密文:
可以写成bat文件
加密:
  @ECHO OFF
%JASYPT_HOME%\bin\encrypt.bat input="E8iptsi855" password="PTSPASSWORD" verbose=false > .\
生成的密文
 wASjSlTjsgYFLyVswElJ4S7yjOcGnABF
解密:
$ ./decrypt.sh input="k1AwOd5XuW4VfPQtEXEdVlMnaNn19hivMbn1G4JQgq/jArjtKqryXksYX4Hl6A0e" password=MYPAS_WORD
具体可参见:
Encrypting from the command line: Jasypt CLI Tools
/cli.html
二个文件:
db.properties
datasource.sql.jdbc.Driver 
datasource.url=jdbc:mysql://localhost/reportsdb 
datasource.username=reportsUser 
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm) 
db.xml----- Xml代码
<property name="connection.provider_class"> 
tionprovider.EncryptedPasswordDriverManagerConnectionProvider 
</property> 
<property name="connection.password">ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)</property> 
l
    <bean id="environmentVariablesConfiguration"
        class="fig.EnvironmentStringPBEConfig">
        <!――指定加密算法: PBEWithMD5AndDES ――>
            <property name="algorithm" value="PBEWithMD5AndDES" />
        <!――指定密钥:PTSPASSWORD――>
        <property name="password"
            value="PTSPASSWORD" />
    </bean>
    <!――指定加密类: StandardPBEStringEncryptor ――>
      <bean id="configurationEncryptor"
        class="ption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
    </bean>
     
      <!――指定要已被加密的属性文件db.properties Jasypt集成了对spring的属性文件解密――>
    <bean id="propertyConfigurer"
        class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>
            <!――原Spring读取属性文件db.properties――>
 
    <!-- <bean id="propertyConfigurer"
        class="org.springframework.fig.PropertyPlaceholderConfigurer">
        <property name="locations">
        <list>
        <value>classpath:db.properties</value>               
        </list>
        </property>
        </bean>
    -->
    <!――配置数据数――>
    <bean id="dataSource"
        class="org.apachemons.dbcp.BasicDataSource"
        destroy-method="close">
如何配置maven环境变量
        <property name="driverClassName"
            value="${pts4.jdbc.driverClassName}" />
        <property name="url" value="${pts4.jdbc.url}" />
        <property name="username" value="${pts4.jdbc.username}" />
        <property name="password" value="${pts4.jdbc.password}" />
        <property name="maxActive" value="${pts4.jdbc.maxActive}"/>
        <property name="maxIdle" value="${pts4.jdbc.Idle}" />
        <property name="maxWait" value="${pts4.jdbc.maxWait}" />
        <property name="defaultAutoCommit"
            value="${pts4.jdbc.defaultAutoCommit}" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout"
            value="${veAbandonedTimeout}" />
    </bean>
    <bean id="jdbcTemplate"
        class="org.JdbcTemplate">
        <constructor-arg>
            <ref bean="dataSource" />
        </constructor-arg>
    </bean>
    <bean id="CatagorySearchDao"
        class="ample.booksearch.server.BookSearchDaoImpl">
        <property name="jdbcTemplate">
            <ref bean="jdbcTemplate" />
        </property>
    </bean>
学习相关网址:
www.javaeye/news/456
sklst.javaeye/blog/284689