springboot jasypt加密_java打包加密

大家好,又见面了,我是你们的朋友全栈君。

1.maven2引用

jasypt坐标

<dependency>
      <groupId>org.jasypt</groupId>
      <artifactId>jasypt</artifactId>
      <version>{version}</version>
      <scope>compile</scope>
</dependency>

jasypt with spring坐标

<dependency>
      <groupId>org.jasypt</groupId>
      <artifactId>jasypt-spring31</artifactId>
      <version>{version}</version>
      <scope>compile</scope>
</dependency>

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

简单轻量的引用

    <dependency>
      <groupId>org.jasypt</groupId>
      <artifactId>jasypt</artifactId>
      <version>{version}</version>
      <classifier>lite</classifier>
      <scope>compile</scope>
    </dependency>

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

例如:

<bean id="strongEncryptor"
    class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
    <property name="algorithm">
        <value>PBEWithMD5AndTripleDES</value>
    </property>
    <property name="password">
        <value>jasypt</value>
    </property>
  </bean>

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:

 <!--                                                                      -->
 <!-- Configuration for encryptor, based on environment variables.         -->
 <!--                                                                      -->
 <!-- In this example, the encryption password will be read from an        -->
 <!-- environment variable called "APP_ENCRYPTION_PASSWORD" which, once    --> 
 <!-- the application has been started, could be safely unset.             -->
 <!--                                                                      -->
 <bean id="environmentVariablesConfiguration"
     class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
   <property name="algorithm" value="PBEWithMD5AndDES" />
   <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
 </bean>
密钥passwordEnvName使用环境变量APP_ENCRYPTION_PASSWORD
 <!--                                                                      -->
 <!-- The will be the encryptor used for decrypting configuration values.  -->
 <!--                                                                      -->
 <bean id="configurationEncryptor"
     class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
   <property name="config" ref="environmentVariablesConfiguration" />
 </bean>


 <!--                                                                      -->
 <!-- The EncryptablePropertyPlaceholderConfigurer will read the           -->
 <!-- .properties files and make their values accessible as ${var}         -->
 <!--                                                                      -->
 <!-- Our "configurationEncryptor" bean (which implements                  --> 
 <!-- org.jasypt.encryption.StringEncryptor) is set as a constructor arg.  -->
 <!--                                                                      -->
 <bean id="propertyConfigurer"
     class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer">
   <constructor-arg ref="configurationEncryptor" />
   <property name="locations">
     <list>
       <value>/WEB-INF/classes/application.properties</value>
     </list>
   </property>   
 </bean>
替换spring PropertyPlaceholderConfigurer的EncryptablePropertyPlaceholderConfigurer
 <!--                                                                      -->
 <!-- Our datasource is configured here, in the usual way. Jasypt's        -->
 <!-- EncryptedPropertyPlaceholderConfigurer will make sure that the       -->
 <!-- ${datasource.password} file gets decrypted and the DBCP DataSource   -->
 <!-- will be correctly initialised.                                       -->
 <!--                                                                      -->
 <bean id="dataSource"
     class="org.apache.commons.dbcp.BasicDataSource"
     destroy-method="close">
   <property name="driverClassName">
     <value>${datasource.driver}</value>
   </property>
   <property name="url">
     <value>${datasource.url}</value>
   </property>
   <property name="username">
     <value>${datasource.username}</value>
   </property>
   <property name="password">
     <value>${datasource.password}</value>
   </property>
 </bean>
        
配置文件中加密了的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:
   
   
    
        ...
    <context-param>
        <param-name>someParameter</param-name>
        <param-value>ENC(...)</param-value>
    </context-param>
    ...

     
     
     
      
      
      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: 
        
        <beans xmlns="http://www.springframework.org/schema/beans"
       ...
       xmlns:encryption="http://www.jasypt.org/schema/encryption"
       ...
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           ...
                           http://www.jasypt.org/schema/encryption
                           http://www.jasypt.org/schema/encryption/jasypt-spring3-encryption-1.xsd
                           ...">
 Once declared, you will be able to use <encryption:*> 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:
   <!-- Registers an org.jasypt.encryption.ByteEncryptor-->
  <encryption:byte-encryptor id="myEncryptor" algorithm="PBEWithMD5AndTripleDES" password="jasypt"/>
  
  <!-- Registers an org.jasypt.encryption.StringEncryptor-->
  <encryption:string-encryptor id="myEncryptor" password="jasypt" pool-size="5"/>
  
  <!-- Registers an org.jasypt.encryption.BigDecimalEncryptor-->
  <encryption:big-decimal-encryptor password="jasypt" key-obtention-iterations="15000"/>
  
  <!-- Registers an org.jasypt.encryption.BigIntegerEncryptor-->
  <encryption:big-integer-encryptor id="myEncryptor" password="jasypt" provider-name="BC"/>
 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:
   <!-- Registers an org.jasypt.digest.ByteDigester-->
  <encryption:byte-digester algorithm="SHA-1" salt-size-bytes="16" iterations="50000"/>
  
  <!-- Registers an org.jasypt.digest.StringDigester-->
  <encryption:string-digester pool-size="10"/>
 Again, the pool-size attribute will determine whether the digesters will be Standard or Pooled.
Some util artifacts can also be instantiated this way:
   <!-- Password encryptors -->
  <encryption:basic-password-encryptor/>
  <encryption:strong-password-encryptor/>
  <encryption:configurable-password-encryptor/>
  
  <!-- Text encryptors -->
  <encryption:basic-text-encryptor password="jasypt"/>
  <encryption:strong-text-encryptor password="jasypt"/>
 
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:
   <encryption:digester-config id="dConf1" iterations="1400" salt-size-bytes="32"/>
  <encryption:digester-config id="dConf2" iterations="10000" string-output-type="hexa"/>
  <encryption:digester-config id="dConf3" string-output-type="hexa" algorithm-env-name="VAR_ALGORITHM"/> 
   
  <encryption:encryptor-config id="eConf1" key-obtention-iterations="500" password-env-name="VAR_PASSWD"/> 
  <encryption:encryptor-config id="eConf2" password-env-name="VAR_PASSWD" algorithm="PBEWithMD5AndTripleDES"/> 
  <encryption:encryptor-config id="eConf3" password="jasypt" algorithm-sys-property-name="jasypt.enc.algorithm"/> 
 Using these beans in our encryptors/digesters is easy:
   <encryption:digester-config id="dConf" string-output-type="hexa" algorithm-env-name="VAR_ALGORITHM"/>
  <encryption:string-digester config-bean="dConf"/>
   
  <encryption:encryptor-config id="eConf" password-env-name="VAR_PASSWD" algorithm="PBEWithMD5AndTripleDES"/>
  <encryption:string-encryptor id="stringEnc" config-bean="eConf"/> 
 
Creating EncryptableProperties instances
Usually, in Spring you can create a java.util.Properties bean in your XML using the util namespace, like this:
   <util:properties location="classpath:application.properties"/>
 Jasypt allows you to register an org.jasypt.properties.EncryptableProperties object in an equivalent manner, simply by adding an encryptor bean reference: 
   <encryption:encryptable-properties encryptor="stringEnc" location="classpath:application.properties"/>
 This <encryption:encryptable-properties> tag works in exactly the same way and with exactly the same features as <util:properties>, 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:
   <context:property-placeholder location="classpath:application.properties"/>
 But if you want to register an EncryptablePropertyPlaceholder instead because your property files might be encrypted, you can do: 
   <encryption:encryptable-property-placeholder encryptor="stringEnc" location="classpath:application.properties"/>
 And that's it! A property override implementation is also provided:
   <encryption:encryptable-property-override encryptor="stringEnc" location="classpath:application.properties"/>
    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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/189387.html原文链接:https://javaforall.cn

原文地址:https://cloud.tencent.com/developer/article/2156831

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原理介绍,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。传统事务中回滚点的使...
今天小编给大家分享的是一文解析spring中事务的传播机制,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区别,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。Spring Cloud Netfli...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。第一步:整合pom文件,在S...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。List 坑列表 = new ArrayList(2);...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓存的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇...
本篇内容主要讲解“Spring中的@Autowired和@Resource注解怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学...
今天小编给大家分享一下SpringSecurity怎么定义多个过滤器链的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家
这篇文章主要介绍“Spring的@Conditional注解怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring的@Con...
这篇文章主要介绍了SpringCloudGateway的熔断限流怎么配置的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringCloud&nb...
今天小编给大家分享一下怎么使用Spring解决循环依赖问题的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考
这篇文章主要介绍“Spring事务及传播机制的原理及应用方法是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Sp...
这篇“SpringCloudAlibaba框架实例应用分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价
本篇内容主要讲解“SpringBoot中怎么使用SpringMVC”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习...
这篇文章主要介绍“SpringMVC适配器模式作用范围是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringMVC
这篇“导入SpringCloud依赖失败如何解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家...
这篇文章主要讲解了“SpringMVC核心DispatcherServlet处理流程是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来
今天小编给大家分享一下SpringMVCHttpMessageConverter消息转换器怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以...
这篇文章主要介绍“Spring框架实现依赖注入的原理是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring框架...
本篇内容介绍了“Spring单元测试控制Bean注入的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下