springboot数据库密码加密解密(springboot 数据库连接密码加密)

发布时间:2024-04-06 点击:66
本文主要介绍springboot数据库密码加密和解密(springboot数据库连接密码加密),下面一起看看springboot数据库密码加密和解密(springboot数据库连接密码加密)相关资讯。
前言在我们的日常开发中,我们可能会不经意地直接在配置文件中暴露数据库密码,这在开发环境中是可以做到的,但在生产环境中是相当不建议的。毕竟是安全的,谁也不知道密码什么时候会莫名其妙的泄露。今天,让我们 下面我们来谈谈如何在springboot项目中加密数据库密码。
文本方案1。使用druid数据库连接池1加密数据库密码。pom.xml引入了druid包。
为了方便其他操作,德鲁伊 这里直接介绍s启动器。
依赖groupidcom.alibaba/groupid工件id druid-spring-boot-starter/工件id版本$ {druid。版本}/版本/依赖关系2。使用com生成公钥和私钥。阿里巴巴。druid.filter.config.config工具。
ps:有两种生成,一种是使用命令行,一种是直接写工具类。本文中的示例由工具类直接生成。
工具类代码如下
/** *阿里巴巴druid加解密规则:*明文密码privatekey加密=加密密码*加密密码publickey解密=明文密码*/public final class druid cryptography { private static string私钥;私有静态字符串publickeystatic { try { string[]key pair = config tools . genkey pair(512);private key = key pair[0];system . out . println(string . format( 私有密钥-% s private key));public key = key pair[1];system . out . println(string . format( 公钥-% s ,public key));} catch(nosuch algorithm exception e){ e . printstacktrace;} catch (nosuchprovide exception e){ e . printstacktrace;}}/* *明文加密* @ param明文* @ return */@ sneakytows公共静态字符串编码(字符串明文){ system . out . println( 明文字符串: 明文);string密文= config tools . encrypt(private key,plaintext);system . out . println( 加密字符串: 密文);返回密文;}/* * *解密* @ param cipher * @ return */@ sneaky抛出公共静态字符串解码(字符串密文){system。out . println( 加密字符串: 密文);string plaintext = config tools . decrypt(public key,密文);system . out . println( 解密字符串: 明文 );返回明文;}3.修改数据库的配置文件内容信息。
a、修改密码,用druidencryptorutils生成的密码替换。
password: $ { data source _ pw: hb 5 fmueai 1 u 81 yjrt/t 6 awimfg 1/az 5 o 8 imy 765 wkvjououbc 2h 80 jqmzr 8 l9 zw kuz/8 agzuq 4 yysakhywna = } b、过滤器打开配置。
truec、:、config:、filt:,并配置connectionproperties属性。
connection-prop: config . decrypt = true;config . decrypt . key = $ { spring . data source。publickey} ps:由spring.datasource.publick: datasourc: typ: com . alibaba . druid . pool . druid data source driverclassnam: com . mysql . cj . jdbc .驱动程序url: $ { data source _ url : jdbc : mysql ://localhost: 3306/demo?use unicode = true character encoding = ut f8 us: $ { data source _ username : root } password: $ { data source _ pwd : hb 5 fmueai 1 u 81 yjrt/t 6 awimg 1/az5o 8 imy 765 kwjoouubc 2h 80 jqmzrr 8 l 9 zwkuzs/8 agzuq 4 yysakhywna = = } publick: mfwwdqyjkozihvcnaqu: 60000 #配置检测需要关闭的空闲连接的频率。单位是毫秒time between connectivity runsmillisec。ho0-@.com 60000 #配置连接在池中的最短存活时间(毫秒), minevictabl: 300000 #配置连接在池中的最长存活时间。单位为毫秒maxevictabl: 900000 #配置检查连接是否有效validationqu:从对偶中选择1 t:真t:假testonreturnecho 0-@ .c. om假w: :真statvi: :真#设置白名单,留空则允许访问allow : url-pattern:/druid/* #控制台管理用户名和密码登录-us:登录-password : filterconfig . decrypt . key = $ { spring . data source . public key }方案二:用jaypt 1加密数据库密码。pom.xml引入了jaypt包。依赖项groupidcom.github.ulisesbocchio/groupid工件id jas ypt-spring-boot-starter/工件id版本$ {jasypt。威瑞森}/版本/依赖关系2。使用jasypt提供的工具类加密明文密码。
加密工具类别如下
public final class jaspytenctorutils { private static final string salt = lybgeek private static basictextencryptor basictextencryptor = new basictextencryptor;static { basictextencryptor . set password(salt);} private jaspytencryptor utils{ }/* *明文加密* @ param明文* @ return */public static string encode(string plaintext){ system。out . println( 明文字符串: 明文);string密文= basictextencryptor.encrypt(明文);system . out . println( 加密字符串: 密文);返回密文;}/* * *解密* @ param密文* @ return */public静态字符串解码(字符串密文){ system . out . println( 加密字符串: 密文);密文= enc( 密文 ) if(propertyvalueencryptionutils . isencryptedvalue(密文)){ string plaintext = propertyvalueencryptionutils . decrypt(密文,basictextencryptor);system . out . println( 解密字符串: 明文 );返回明文;} system . out . println( 解密失败 );返回 }}3.修改数据库的配置文件内容信息。
a、用enc封装jasyptencryptorutils生成的加密字符串。
password: $ { data source _ pw: enc(p 8m 43 qmzqn 4c 07 ddctp: encryptor: password: lybg: pbewithmd 5 and des iv-generator-classnam: datasourc: typ: com . alibaba . druid . pool . druid data source driverclassnam: com . mysql . cj . jdbc .驱动程序url: $ { data source _ url : enc(kt/gwazwzafnep 7 ocbsgcqn 7 phrohatkjndgvglsw 2 ch 67 zqbveq 7 mn 0 btixae f 4/fvv4 l7 mlfx 0y 6 ap 4 umod 7 c2 vwgyru 5 uqtkmdwzqn 3 hxxktikrfpn 9dm 6 yahm 0 xp ppo 9 haw)} us: $ { data source _ username :enc(r: $ { data source _ pw: enc(p 8m 43 qmzqn 4c 07 ddctp: #初始连接数initialsiz: 5 #最小连接池数minidl: 10 #最大连接池数maxactiv: 20 #配置获取连接的超时期限maxwait: 60000 #配置检测需要关闭的空闲连接的时间。单位是毫秒。timebetweenevictionrunsmillis: 60000 #配置一个连接在池中的最短存活时间,单位是毫秒。minevictabl: 300000 #配置连接在池中的最长存活时间。单位为毫秒maxevictabl: 900000 #配置检查连接是否有效validationqu:从对偶中选择1 t:真t:假testonreturnecho 0-@ .c. om假w: :真statvi: :真#设置白名单,留空则允许访问管理用户名和密码allow : url-pattern:/druid/* # consol:,登录-password: :真#慢sql记录log-slow-sql:真slow-sql-millis: 1000 m:真wall: config:多stat: true jaspyt: encryptor: password : lybgeek algorithm : pbewithmd 5 and des iv-generator-class name : orgjasypt.iv.noivgenerator方案三:自定义实现原理:利用spring后处理器修改datasource。
1.自定义加密和解密工具类
/* * *以aes对称加密算法为例,通过使用hu tool */public final class encryptor utils封装的加解密工具{ private static string secret keystatic { secret key = hex . encodehexstring(secureutil . generate key(symmetricalgorithm。aes.getvalue)。get encoded);system . out . println( 秘钥- secret key);system . out . println( - );}/* * *明文加密* @ param明文* @ return */@ sneakytows公共静态字符串编码(字符串明文){ system . out . println( 明文字符串: 明文);byte[] key = hex.decodehex(秒retkey . tochararray);string密文= secureutil.aes(key)。encrypthex(明文);system . out . println( 加密字符串: 密文);返回密文;}/* * *解密* @ param cipher * @ return */@ sneaky抛出公共静态字符串解码(字符串密文){system。out . println( 加密字符串: 密文);byte[]key = hex . decode hex(secret key . tochararray);string plaintext = secure util . aes(key)。decryptstr(密文);system . out . println( 解密字符串: 明文 );返回明文;}/* * *明文加密* @ param明文* @ return */@潜行抛出公共静态字符串编码(string secret key,string plaintext){ system . out . println( 明文字符串: 明文);byte[]key = hex . decode hex(secret key . tochararray);string密文= secureutil.aes(key)。encrypthex(明文);system . out . println( 加密字符串: 密文);返回密文;}/* * *解密* @ param cipher * @ return */@ sneaky throws公共静态字符串解码(字符串密钥,字符串密文){system。out . println( 加密字符串: 密文);byte[] key = hex . decode hex(secret key . tochararray);string plaintext = secure util . aes(key)。decryptstr(密文);system . out . println( 解密字符串: 明文 );返回明文;}}2.写一个后处理器。
公共类druiddatasourceencyptbeanpostprocessor实现beanpostprocessor { private customencryptproperties customencryptproperties;私有data source properties data source properties;public druiddatasourceencyptbeanpostprocessor(customencryptproperties customencryptproperties,data source properties data source properties){ this . customencryptproperties = customencryptproperties;this . data source properties = data source properties;} @ override public object postprocessbefore initial ization(object bean,string beanname)抛出bean exception { if(bean instance of druid data source){ if(customencryptproperties . is enabled){ druid data source druid data source =(druid data source)bean;system . out . println( - );字符串username = datasourceproperties . get username;druid data source . set username(encrypturtils . decode(customencryptproperties . getsecretkey,username));system . out . println( - );string password = data source properties . get password;druid data source . set password(encryptorutils . decode(customencryptproperties . getsecretkey,password));system . out . println( - );string url = data source properties . geturl;druid data source . seturl(encryptorutils . decode(customencryptproperties . getsecr: $ { data source _ pw : fb 31 cd 78 a5 fa 2c 43 f 530 b 849 f 1135 e 7 } b,指定密钥,开启加密功能。
custom: encrypt: :真实秘密-k: datasourc: typ: com . alibaba . druid . pool . druid data source driverclassnam: com . mysql . cj . jdbc .驱动程序url: $ { data source _ url : dcb 268 cf 3a 2626381d 2 bc 5 c 96 f 94 fb 3d 7 f 99352 e0e 392362 cb 818 a 321 b0ca 61 f3 a 8 dad 3a eb 084242 b 745 c 1484 bf 745 c 858 c 44 e单位是毫秒timebetweenevictionrunsmillis: 60000 #配置。池中连接的最短生存期(以毫秒为单位)是minevictabl: 300000 #配置的池中连接的最长生存期。单位为毫秒maxevictabl: 900000 #配置检查连接是否有效validationqu:从对偶中选择1 t:真t:假testonreturnecho 0-@ .c. om假w: :真statvi: :真#设置白名单,留空则允许访问allow : url-pattern:/druid/* #控制台管理用户名和密码登录-us:登录-password : filter个人推荐jasypt,因为它不仅可以加密密码,还可以加密其他内容。德鲁伊只能加密数据库密码。至于自定义方案,属于惯例,毕竟开源已经如果你有什么,不要再自己做轮子了。最后一点,如果jasypt高于2版,低于3.0.3版,那么配置中心的动态刷新配置,比如apollo或者nacos,都会失效(最新版本3.0.3官方表示已经修复了这个问题)。
如果使用配置中心,jasypt建议使用版本3或者使用版本3.0.3的演示链接。
关于如何加密springboot项目的数据库密码,本文就讲到这里。关于springboot数据库密码加密内容的更多信息,请搜索我们之前的文章或者继续浏览以下相关文章。希望大家以后支持我们!
标签:
字符串密码
了解更多springboot数据库密码加密和解密(springboot数据库连接密码加密)相关内容请关注本站点。


塔防三国志新城(塔防三国志新城二十闯关教程)
为什么一打开手机就有热点资讯是什么原因(为什么一打开手机就有热点资讯vivo)
适合新手的重装win10教程步骤图(适合新手的重装win10教程步骤有哪些)
戴尔维修电话,戴尔服务网点维修
华为大尺寸平板(华为最大平板电脑多少钱)
怎么修改手机的dns,手机怎么修改DNS设置
wifi密码查看怎么看密码(如何查看wifi秘码)
戴尔中国百度百科,DELL是什么牌子的电脑
136 8686 3664
李经理
 发送短信