栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合jasypt

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合jasypt

文章目录
  • jasypt
  • springboot集成jasypt
    • 1.引入maven依赖jasypt-spring-boot-starter
    • 2.启动类添加注解@EnableEncryptableProperties
    • 3.yaml文件配置
  • 总结
  • 问题


jasypt

保证项目中的账号密码不以明文的形式展示


springboot集成jasypt 1.引入maven依赖jasypt-spring-boot-starter

	com.github.ulisesbocchio
    jasypt-spring-boot-starter
    3.0.4

2.启动类添加注解@EnableEncryptableProperties
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEncryptableProperties
public class AdminMain {

    public static void main(String[] args) {
        SpringApplication.run(AdminMain.class, args);
    }
}
3.yaml文件配置

这里以配置数据库为例:

  • 配置jasypt
jasypt:
 encryptor:
   password: 02700083-9fd9-4b82-a4b4-9177e0560e92
   algorithm: PBEWithMD5AndDES
   iv-generator-classname: org.jasypt.iv.NoIvGenerator

生成账号:root,密码:mysql

import org.jasypt.util.text.BasicTextEncryptor;


public class Test {

    public static void main(String[] args) {
    	//该类的选择根据algorithm:PBEWithMD5AndDE选择的算法选择
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword("02700083-9fd9-4b82-a4b4-9177e0560e92");
        String encrypt = encryptor.encrypt("root");
        System.out.println(encrypt);
        String decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
        String encrypt = encryptor.encrypt("mysql");
        System.out.println(encrypt);
        String decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
    }
}
##生成的结果,每次生成的结果都不一致
Ii5Ux7xKBVTd7+SrCkX9TQ==
root
Eg/vK2yi3/p8boYVznsyVw==
mysql
  • 配置数据库
spring:
 datasource:
   type: com.alibaba.druid.pool.DruidDataSource
   driver-class-name: com.mysql.cj.jdbc.Driver
   url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
   username: ENC(Ii5Ux7xKBVTd7+SrCkX9TQ==)
   password: ENC(Eg/vK2yi3/p8boYVznsyVw==)
总结 问题

1.如果启动项目的时候报如下错误:

Failed to bind properties under 'spring.datasource.username' to java.lang.String:

    Reason: Failed to bind properties under 'spring.datasource.username' to java.lang.String

可以通过配置yaml文件

jasypt:
  encryptor:
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032619.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号