• Jasypt加解密、信息脱敏


    一、介绍

    Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
    Jasypt是一个Java库,它允许开发人员以最小的努力为他/她的项目添加基本的加密功能,而无需深入了解密码学的工作原理。

    • High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries…
      基于标准的高安全性加密技术,适用于单向和双向加密。加密密码,文本,数字,二进制文件…
    • Transparent integration with Hibernate.
      完美地与 Hibernate 集成。
    • Suitable for integration into Spring-based applications and also transparently integrable with Spring Security.
      适合集成到 Spring项目中,也可以完美地与 Spring Security集成。
    • Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
      用于加密应用程序(即数据源)配置的集成功能。
    • Specific features for high-performance encryption in multi-processor/multi-core systems.
      多处理器/多核系统中高性能加密的特定功能。
    • Open API for use with any JCE provider.
      开放 API 以与任何 JCE 提供程序一起使用。
    • …and much more
      …等等

    二、Spring集成

    1、 Maven依赖

    <dependency>
        <groupId>org.jasyptgroupId>
        <artifactId>jasypt-spring31artifactId>
        <version>1.9.3version>
    dependency>
    <dependency>
        <groupId>org.jasyptgroupId>
        <artifactId>jasyptartifactId>
        <version>1.9.3version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、application.xml的配置

    <bean id="environmentVariablesConfiguration"
    	  class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
    	
    	<property name="algorithm" value="PBEWithMD5AndDES" />
    	
    	<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>
    			<value>classpath:myconfig.propertiesvalue>
    			<value>classpath:application.propertiesvalue>
    		list>
    	property>
    bean>
    
    <bean id="myBean" class="com.example.MyBean">
    	<property name="pwd" value="${datasource.username}"/>
    	<property name="user" value="${datasource.password}"/>
    bean>
    
    • 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
    • 26
    • 27
    • 28

    3、配置文件使用

    使用 ENC(密文)

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

    4、方法加密

    BasicTextEncryptor encryptor = new BasicTextEncryptor();
    encryptor.setPassword("myPassword");
    String encrypted = encryptor.encrypt("敏感信息");//密码加密
    System.out.println(encrypted);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、SpringBoot集成

    1、 Maven依赖

    
    <dependency>
        <groupId>com.github.ulisesbocchiogroupId>
        <artifactId>jasypt-spring-boot-starterartifactId>
        <version>3.0.5version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、 Java Bean配置jasyptStringEncryptor

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
      PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
      SimpleStringPBEConfig config = new SimpleStringPBEConfig();
      config.setPassword("mypassword");
      config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
      config.setKeyObtentionIterations("1000");
      config.setPoolSize("1");
      config.setProviderName("SunJCE");
      config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
      config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
      config.setStringOutputType("base64");
      encryptor.setConfig(config);
      return encryptor;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3、配置文件使用

    使用 ENC(密文)

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

    4、Bean使用加密字段自动解密

    直接用@Value(“${配置文件的Key}”)

    @RestController
    @RequestMapping("/test")
    public class TestController {
        @Value("${username}")
        private String username;
        
    	@Value("${password}")
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    参考:https://blog.csdn.net/weixin_42962634/article/details/122455123

  • 相关阅读:
    300元左右的耳机哪个性价比最好、好用的开放式耳机推荐
    9.7 小结
    opencv 双目立体视觉
    17. Go并发编程
    Qt自定义菜单
    代码随想录算法训练营第25天|216.组合总和III● 17.电话号码的字母组合天|
    m基于OFDM数字电视地面广播系统中频域同步技术研究(word版说明文档,程序操作视频)
    MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
    .NET周刊【6月第1期 2024-06-02】
    NJ 时钟自动调整功能(SNTP)
  • 原文地址:https://blog.csdn.net/malu_record/article/details/134002384