Jayspt加密外部属性

在看spring3的时候就有提到过jasypt,用来加密Jayspt不过这个项目目前没有再更新了。最近一次更新也是 25 Feb 2014,很可惜,所以我们看到在spring4 他不再支持了。

不过来看下他曾经的作用吧。


可以在官网看下和spring3的整合,连接如下

http://www.jasypt.org/springsecurity.html


本文我们以数据库的用户名和密码为例子,来讲解在APDPlat中我们如何使用开源项目Jasypt来实现加解密。

src:http://my.oschina.net/apdplat/blog/405306

首先,我们引入依赖库,使用Maven方式如下:

?
1
2
3
4
5
< dependency >
     < groupId >org.jasypt</ groupId >
     < artifactId >jasypt</ artifactId >
     < version >1.5</ version >
</ dependency >

其次,接下来我们看看如何加密:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import  org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import  org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
 
/**
  *把密文放到配置文件中的时候要注意:
  * ENC(密文)
  * @author 杨尚川
  */
public  class  ConfigEncryptUtils {
     public  static  void  main(String[] args){
         //加密工具
         StandardPBEStringEncryptor encryptor =  new  StandardPBEStringEncryptor();
         //加密配置
         EnvironmentStringPBEConfig config =  new  EnvironmentStringPBEConfig();
         config.setAlgorithm( "PBEWithMD5AndDES" );
         //自己在用的时候更改此密码
         config.setPassword( "apdplat" );
         //应用配置
         encryptor.setConfig(config);
         String plaintext= "root" ;
         //加密
         String ciphertext=encryptor.encrypt(plaintext);
         System.out.println(plaintext +  " : "  + ciphertext);
     }
}

运行输出结果如下:

    root : azL9Cyp9H62r3eUgZ+TESw==

再次,接下来我们看看如何解密:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import  org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import  org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
 
/**
  *把密文放到配置文件中的时候要注意:
  * ENC(密文)
  * @author 杨尚川
  */
public  class  ConfigEncryptUtils {
     public  static  void  main(String[] args){
         //加密工具
         StandardPBEStringEncryptor encryptor =  new  StandardPBEStringEncryptor();
         //加密配置
         EnvironmentStringPBEConfig config =  new  EnvironmentStringPBEConfig();
         config.setAlgorithm( "PBEWithMD5AndDES" );
         //自己在用的时候更改此密码
         config.setPassword( "apdplat" );
         //应用配置
         encryptor.setConfig(config);
         String ciphertext= "azL9Cyp9H62r3eUgZ+TESw==" ;
         //解密
         String plaintext=encryptor.decrypt(ciphertext);
         System.out.println(ciphertext +  " : "  + plaintext);
     }
}

运行输出结果如下:

   azL9Cyp9H62r3eUgZ+TESw== : root

从上面我们可以看到,加密和解密的代码的唯一差别是encrypt和decrypt。

最后我们来看看如何和spring集成,在spring配置文件中加入如下配置,这样当spring读取到的值是加过密的值就会自动解密,那么spring是如何判断一个值是否加密过了呢?是根据特定的前缀ENC(和后缀)来判断的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- Spring属性文件解密组件  -->
< bean  id = "propertyConfigurer"  class = "org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer" >
     < constructor-arg  ref = "configurationEncryptor"  />
     < property  name = "systemPropertiesModeName"  value = "SYSTEM_PROPERTIES_MODE_OVERRIDE"  />
     < property  name = "ignoreResourceNotFound"  value = "true"  />
     < property  name = "locations" >
         < list >
             < value >classpath:/org/apdplat/config.properties</ value >
             < value >classpath:config.local.properties</ value >
             < value >classpath:/org/apdplat/db.properties</ value >
             < value >classpath:db.local.properties</ value >
         </ list >
     </ property >
</ bean >
 
< bean  id = "configurationEncryptor"  class = "org.jasypt.encryption.pbe.StandardPBEStringEncryptor" >
     < property  name = "config"  ref = "environmentVariablesConfiguration"  />
</ bean >
 
< bean  id = "environmentVariablesConfiguration"  class = "org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig" >
     < property  name = "algorithm"  value = "PBEWithMD5AndDES"  />
     < property  name = "password"  value = "apdplat"  />
</ bean >

配置好spring后,我们就可以在指定的配置文件classpath:config.local.properties中使用加密过后的数据库连接使用的用户名和密码了,别忘了特定的前缀ENC(和后缀)哦

?
1
2
3
4
5
6
7
8
9
10
#数据库配置文件
 
#mysql
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/${module.short.name}?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true&autoReconnect=true
db.username=ENC(qPWWR8YEmQE63EYywEBKaQ==)
db.password=ENC(qPWWR8YEmQE63EYywEBKaQ==)
jpa.database=MYSQL
db.backup.command=mysqldump  -u${db.username} -p${db.password} ${module.short.name}
db.restore.command=mysql -u${db.username} -p${db.password} ${module.short.name}



-------------------------

-------------------------

http://www.jasypt.org/springsecurity.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值