spring3.1整合jasypt:

废话少说,直接上代码:

jasypt pom依赖:

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

spring的xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><context:component-scan base-package="com.study" /><!--jasypt的核心配置,就这3个bean--><bean id="environmentVariablesConfiguration"class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"><property name="algorithm" value="PBEWithMD5AndDES" /><!--这个属性是密码加密时候用到的,一般建议配置在机器的环境变量里,通过passwordEnvName这个属性设置,设置好之后就可以到系统环境变量里拿到这个值,当然也可以直接通过下面的password属性直接配置在这里,只不过安全性没有配置在环境变量里那么高,可参考相关环境变量配置方法的实现,这里就直接写了--><property name="password" value="mysqlPwd" /></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><!--这个是具体的配置文件路径,就是让jasypt扫描到这个配置文件,就可以对配置文件里加密数据进行解密--><value>classpath:test.properties</value></list></property></bean><bean id="test" class="com.example.xma.Test"><!--这里就可以拿到具体的加密后的密码值了--><property name="pwd" value="${datasource.username}"/><property name="user" value="${datasource.password}"/></bean>
</beans>

properties配置文件:

datasource.driver_class=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/mydatabase
datasource.username=ENC(aXZJTKwF6Vt147qUJOrMuT3NDV4y0NzG)
datasource.password=ENC(LWzlg7fvAhO8RMIDDxifEORimjA91ibn)

路径:

参考:https://blog.51cto.com/aiilive/1420903

jasypt既然是以简单的方式来解决java开发中的加密问题,自然使用起来难度不是很大。加密是从系统安全性方面考虑的,因此jasypt更像是面向方面的解决办法,不管你的系统中配置文件,敏感信息是否已经加密或者没有加密,jasypt都能够轻松的嵌入其中,开发人员就不用专门考虑加密算法和代码的编写。

要想深入了解jasypt是如何将加密解密和摘要算法组织起来,轻松的解决开发中加密问题以及和第三方组件集成,查看它的源代码是不错的选择。

下面主要说说如何在Spring框架中如何轻松使用jasypt。(下面的加密机是对jasypt中的加密解密,摘要算法的统称)

第一种方式:以bean的形式将加密机(即:加密类的实例对象)交给Spring托管

第二种方式:以配置XML的形式将加密机与Spring集成。

第一种方式:

1.托管一个StandardPBEStringEncryptor加密机

<!-- 加密机 -->
    <bean id="strongEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="algorithm">
            <value>PBEWithMD5AndTripleDES</value>
        </property>
        <property name="password">
            <value>${user.home}</value>
        </property>
    </bean>

这里的属性"password"的值为系统属性的值,实际开发中在对某一个数据进行加密的时候这个password是要进行记录的,如果password在这里设置之后将默认提供了一个password的取值。

其它的属性设置可以参见: http://aiilive.blog.51cto.com/1925756/1420837 这篇文章中关于jasypt命令行工具的介绍。

在程序中使用strongEncrypt加密机对象:

@Test
    public void test1() {
        StandardPBEStringEncryptor spe = (StandardPBEStringEncryptor) context
                .getBean("strongEncryptor");
        String src = "admin@123";
        String encrypt = spe.encrypt(src);
        System.out.println("src:" + src);
        System.out.println("encrypt:" + encrypt);
        String decrypt = spe.decrypt(encrypt);
        System.out.println("decrypt:" + decrypt);
        //加密解密
        Assert.assertEquals(decrypt, src);
    }

2.托管一个StandardStringDigester加密机

<!-- 摘要算法 -->
    <bean id="digestEncryptor" class="org.jasypt.digest.StandardStringDigester">
        <property name="algorithm">
            <value>MD5</value>
        </property>
    </bean>

在程序中使用digestEncryptor加密机对象

@Test
    public void test7() {
        StandardStringDigester ssd = (StandardStringDigester) context
                .getBean("digestEncryptor");
        String rs1 = ssd.digest("admin");
        String rs2 = ssd.digest("admin");
        System.out.println(rs1 + "  [vs]  " + rs2);
        //判断是否匹配
        Assert.assertTrue(ssd.matches("admin", rs1));
    }

StrandardStringDigester类提供了matches方法用来检测原始数据和进行摘要计算后的数据是否匹配。

1,2介绍了数据的处理,下面3讲介绍使用jasypt对配置文件进行处理.

3.使用jasypt对配置文件进行处理

比如数据库连接的属性值一般要进行加密处理,然后在程序运行时对其进行解密连接数据库,这样就保证了在程序代码已经配置中数据库的连接相关敏感数据不至于明文暴露。

jasypt是如何处理这一过程的呢?

首先,配置环境变量(这里指用来加解密的环境),

然后,通过环境变量来装载加密机,

最后,使用jasypt对Spring的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类的子类来配置属性文件替换配置。

下面是具体的配置信息:

<!-- 基于环境变量,配置加密机 -->
    <bean id="environmentVariablesConfiguration"
        class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES" />
        <!-- <property name="passwordEnvName" value=""/> -->
        <!-- <property name="passwordSysPropertyName" value=""></property> -->
        <property name="password" value="sa" />
    </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>classpath:db.properties</value>
            </list>
        </property>
    </bean>

<!--数据源配置, jasypt的EncryptedPropertyPlaceholderConfigurer将确保${dataSource.password}是解密 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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>

说明:

EnvironmentStringPBEConfig 中的属性

passwordEnvName,  passwordSysPropertyName,password

三者的区别是:

passwordEnvName的值直接设置为环境变量,比如value="APP_PASSWORD", APP_PASSWORD则是系统环境变量,在实际生产环境中建议使用这个属性,具体使用步骤如:配置环境变量APP_PASSWORD  --> 启动应用程序 --> 应用程序启动完成  --> 删除环境变量APP_PASSWORD。

passwordSysPropertyName的值就是用 System.getProperties() 获取的属性值,比如:value="${user.home}"

password和使用jasypt命令行工具时的password参数用法一致。

属性配置文件加密

dataSource.driver=org.postgresql.Driver
dataSource.url=jdbc:postgresql://localhost:5432/dbname
dataSource.username=postgres
#dataSource.password=postgres
dataSource.password=ENC(lzRg3F8s4sfJPQ96m5YqDz50VeY85YGX)

这里将password的加密结果放置在ENC(机密结果)中,注意这样的写法是jasypt的规定,可以查看源代码,在解密过程中会根据这个标志对属性配置文件中的加密数据进行解密。

属性配置文件中的机密结果产生则需要用jasypt的命令行工具(具体使用可以参见: http://aiilive.blog.51cto.com/1925756/1420837  ),这里要注意的是加密过程中的算法,password参数值需要和Spring配置文件中的environmentVariablesConfiguration(bean)的属性取值保持一致。

数据源中使用属性配置信息中的值

以前Spring中怎么使用,现在就怎么使用。

jasypt和Spring集成的依赖

jasypt.jar+jasypt-springx-x.jar , x表示一些版本信息。

第二种方式

第一种方式将jasypt中的类作为bean的形式在Spring中应用,第二种方式则更加强大,有独立的XML配置命名空间,更像是Spring的一部分。

首先需要在Spring的配置文件中添加jasypt的命名空间。

配置完成jasypt的命名空间就可以在Spring的配置文件中直接进行加密机,加密机参数配置,下面是一个示例:

<!-- 基本的密码加密机 -->
    <encryption:basic-password-encryptor id="bpe" scope="singleton" />
    
    <!-- 摘要配置  -->
    <encryption:digester-config id="digester-config" algorithm="SHA-256" algorithm-env-name=""/>
    <!-- 字符串摘要机 -->
    <encryption:string-digester id="sd" algorithm="MD5" config-bean="digester-config"/>
    
    <!-- 加密机配置  -->
    <encryption:encryptor-config id="encryptor-config" algorithm="PBEWITHMD5ANDTRIPLEDES"/>
    <!-- 字符串加密机  -->
    <encryption:string-encryptor id="se" algorithm="PBEWITHMD5ANDDES" config-bean="encryptor-config"/>
    
    <!-- 加密的属性占位符  -->
    <encryption:encryptable-property-placeholder encryptor="se" location="classpath:db.properties"/>

第二种方式同样可以实现方式一中的功能。

通过介绍了jasypt和Spring集成的两种方式可以看出使用jasypt能够比较轻松自定义加密的参数,配置文件的加解密,整个过程对于应用程序的代码侵入性是很小的,可以在程序中使用jasypt提供的加密算法和方法来实现对需要加密的数据进行处理。

此外jasypt与Hibernate集成则以一个完全对程序逻辑透明的方式可以在ORM映射中对数据进行加解密。

最后jasypt也是开放的,它开放了JCE Provider API,允许开发者使用任何存在的JCE Provider在jasypt中进行消息摘要和加密处理。
-----------------------------------
©著作权归作者所有:来自51CTO博客作者secondriver的原创作品,谢绝转载,否则将追究法律责任
jasypt与Spring结合使用说明
https://blog.51cto.com/aiilive/1420903

Spring整合jasypt使用说明相关推荐

  1. Strutsw2与Spring整合流程-简述

    1.      新建WEB工程: 2.      导入struts2开发包,和资源配置文件 ① globalMessages.properties ② struts.properties 3.     ...

  2. 最新Spring整合MyBatis详解教程

    目录 1.导入相关jar包 1. junit 2. mybatis 3. mysql 4. spring相关 5. aop织入 6. mybatis-spring 7. lombok(选用) 2.回顾 ...

  3. Spring整合Struts2

    ①导入Struts2 jar包 ②在web.xml文件中创建过滤器 <?xml version="1.0" encoding="UTF-8"?> & ...

  4. shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决

    shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决 from :http://blog.csdn.net/babys/article/ ...

  5. Spring整合CXF,发布RSETful 风格WebService

    这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了.如果你对Spring整合CXF ...

  6. springMvc+mybatis+spring 整合 包涵整合activiti 基于maven

    2019独角兽企业重金招聘Python工程师标准>>> 最近自己独立弄一个activiti项目,写一下整合过程: 环境:jdk1.7 tomcat7.0 maven3.5  ecli ...

  7. activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建         (2)创建流程模型:activiti自己定义流程之Spr ...

  8. Spring 整合 Junit

    Spring 整合 Junit 问题 在测试类中,每个测试方法都有以下两行代码: ApplicationContext ac = new ClassPathXmlApplicationContext( ...

  9. spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试

    引子:spring整合mybatis.因为,我们看完(我就是这样的)spring和mybatis之后,本想自己写一个小小的项目,以便加深理解,但是我发现在spring中整合mybatis并不是一件容易 ...

  10. Hibernate Validation与Spring整合各注解的用法Demo

    转自:https://www.aliyun.com/jiaocheng/1315650.html <dependency> <groupId>org.hibernate< ...

最新文章

  1. 毛永胜计算机教师,中国文化中心笛子教师与毛国立音乐学院师生交流
  2. LeetCode: 150:逆波兰表示法求值。
  3. Linux distributions —— 可安全安装套件,Linux发布商套件
  4. 学校做计算机教室锐捷,锐捷云课堂:让学生爱上每一节课
  5. 荣耀智慧屏功能曝光 首发华为鸿蒙OS,荣耀智慧屏功能曝光:首发华为鸿蒙OS,全场景智慧体验...
  6. 聊聊RPC之Provider
  7. 解析word文件的简单实现
  8. 89c51汇编语言波形发生器,51单片机汇编语言实现波形发生器.docx
  9. AWS中IGW,NAT GW以及Egress-only IGW的概念和区别
  10. php zend optimizer,【原创】ZendOptimizer 的安装
  11. Testin云测平台操作步骤
  12. CPU后面字母究竟是啥?
  13. VSCode删除多余空行快捷方法
  14. 和ZLTT一起学pwn 2.ret2text
  15. idea项目总是自动重启_IDEA 下 SpringBoot 自动重启
  16. 安卓Android开发快速入门
  17. DotAsterisk(点星PBX)呼叫中心系统在阿里云ECS服务器上的安装部署
  18. 乐优商场项目day02——总结
  19. storm显微镜成像原理_STORM和STED显微成像技术特点的比较.PDF
  20. SpringSecurity-笔记

热门文章

  1. [浪风分享]推荐一些不错的计算机书籍
  2. python内置函数print输出到文件,实现日志记录的功能
  3. $.ajax()在IE9下的兼容性问题
  4. Linq-查询上一条下一条
  5. Python学习第五天
  6. 【Android进阶】SlidingMenu实现侧滑栏效果的实现
  7. 一个C#控制台小游戏(源码解析)
  8. asp.net中DataList和Repeater的使用
  9. vue中Component错误
  10. Servlet请求转发RequestDispatcher接口