jasypt结合spring加密

1.maven2引用

jasypt坐标


      org.jasypt
      jasypt
      {version}
      compile
 

jasypt with spring坐标


      org.jasypt
      jasypt-spring31
      {version}
      compile

如要结合spring ,需要将jasypt-spring31加入依赖

 

简单轻量的引用

    
      org.jasypt
      jasypt
      {version}
      lite
      compile
    

 

2.在spring中声明一个Encryptor的引用

例如:


    
        PBEWithMD5AndTripleDES
    
    
        jasypt
    
  

 

algorithm=算法

password=密钥

 

3.使用spring的app应用配置文件加密

Jasypt 提供的可体会spring configuration管理类的classes:

  • org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer, as a totally compatible replacement for Spring'sPropertyPlaceholderConfigurer.
  • org.jasypt.spring3.properties.EncryptablePropertyOverrideConfigurer, as a totally compatible replacement for Spring'sPropertyOverrideConfigurer.
  • org.jasypt.spring3.properties.EncryptableServletContextPropertyPlaceholderConfigurer: as a totally compatible replacement for Spring'sServletContextPropertyPlaceholderConfigurer.
  • org.jasypt.spring3.properties.EncryptablePreferencesPlaceholderConfigurer: as a totally compatible replacement for Spring'sPreferencesPlaceholderConfigurer.

例子:

配置文件如下:

 datasource.driver=com.mysql.jdbc.Driver
 datasource.url=jdbc:mysql://localhost/reportsdb
 datasource.username=reportsUser
 datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)

 

其中datasource.password是加密了的字符串,value的值必须要使用ENC()加密字符串括起来

 

Spring context configuration:

 
 
 
 
 
  
 
 
 
   
   
 
密钥passwordEnvName使用环境变量APP_ENCRYPTION_PASSWORD
  
  
 
 
 
 
   
 


 
 
 
 
  
 
 
 
   
   
     
       /WEB-INF/classes/application.properties
     
      
 
替换spring PropertyPlaceholderConfigurer的EncryptablePropertyPlaceholderConfigurer
 
 
 
 
 
 
 
   
     ${datasource.driver}
   
   
     ${datasource.url}
   
   
     ${datasource.username}
   
   
     ${datasource.password}
   
 
        
配置文件中加密了的datasource.password会被解密出来。
 
还有几个说明,懒得翻译了,自己看吧(英文水平不行的请止步,后面的可看可不看)
 
   

Encryptable ServletContextPropertyPlaceholderConfigurer implementation for Spring

Jasypt includes org.jasypt.spring3.properties.EncryptableServletContextPropertyPlaceholderConfigurer, a subclass of org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer which allows the transparent decryption of servlet context parameters in web applications (for example, parameters in WEB-INF/web.xml).

These encrypted parameters can be specified in a way equivalent to that of encrypted parameters in .properties files:

    ...
    
        someParameter
        ENC(...)
    
    ...
 
     

Encryptable PreferencesPlaceholderConfigurer implementation for Spring

Jasypt includes org.jasypt.spring3.properties.EncryptablePreferencesPlaceholderConfigurer, a subclass of org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer which allows the transparent decryption preferences set with JDK 1.4's Preferences API.

 
 

The jasypt-spring3 library includes a namespace you can use in your Spring XML files in order to make the declaration of your jasypt entities much easier.

This namespace can be included in your XML like this:


Once declared, you will be able to use tags for:

  • Creating encryptors and digesters.
  • Creating configuration beans, both for encryptors and for digesters.
  • Creating instances of EncryptableProperties (extending java.util.Properties) that automatically decrypt entries in .properties files.
  • Registering an EncryptablePropertyPlaceHolderConfigurer.

Creating encryptors and digesters

Creating encryptor and digester artifacts with the encryption namespace is easy. There's a tag for each type of encryptor/digester (including some util classes), and each tags is able to specify all of the artifact's properties as tag attributes.

Let's see some encryptor declaration examples:

  
  
  
  
  
  
  
  
  
  
  

Note how the pool-size parameter will affect the specific implementation of encryptor being created: a PooledPBE*Encryptor if this parameter is specified, and a StandardPBE*Encryptor if not.

Now for some digesters:

  
  
  
  
  

Again, the pool-size attribute will determine whether the digesters will be Standard or Pooled.

Some util artifacts can also be instantiated this way:

  
  
  
  
  
  
  
  

Creating configuration beans for encryptors and digesters

Configuration beans implement the DigesterConfig interface for digesters and PBEConfig for encryptors, and Jasypt offers several implementations of these interfaces out-of-the-box depending on whether the digester to be created is meant for bytes or Strings, and also whether some configuration parameters can come from environment variables and/or system properties.

The encryption namespace will automatically choose the correct config bean implementation to be instantiated depending on the specified configuration attributes, so that you do not have to worry about the specific implementation class you need.

Let's see some examples:

  
  
   
   
   
   
   

Using these beans in our encryptors/digesters is easy:

  
  
   
  
   

Creating EncryptableProperties instances

Usually, in Spring you can create a java.util.Properties bean in your XML using the util namespace, like this:

  

Jasypt allows you to register an org.jasypt.properties.EncryptableProperties object in an equivalent manner, simply by adding an encryptor bean reference:

  

This tag works in exactly the same way and with exactly the same features as , and as the object it registers is a subclass of java.util.Properties, you can autowire it inside your application with your code not even noticing these properties are originally encrypted.

Registering an EncryptablePropertyPlaceholder/Override

Spring allows you to easily register a PropertyPlaceholderConfigurer that takes care of the resolution of your ${...} property expressions:

  

But if you want to register an EncryptablePropertyPlaceholder instead because your property files might be encrypted, you can do:

  

And that's it! A property override implementation is also provided:

  

 

For details on how to integrate jasypt with Spring Security 3.x, please have a look at this guide.

 

 

引用:http://www.jasypt.org/spring3.html。

 

你可能感兴趣的:(java,spring)