本文主要介紹springboot數(shù)據(jù)庫密碼加密和解密(springboot數(shù)據(jù)庫連接密碼加密),下面一起看看springboot數(shù)據(jù)庫密碼加密和解密(springboot數(shù)據(jù)庫連接密碼加密)相關(guān)資訊。
前言在我們的日常開發(fā)中,我們可能會不經(jīng)意地直接在配置文件中暴露數(shù)據(jù)庫密碼,這在開發(fā)環(huán)境中是可以做到的,但在生產(chǎn)環(huán)境中是相當(dāng)不建議的。畢竟是安全的,誰也不知道密碼什么時(shí)候會莫名其妙的泄露。今天,讓我們 下面我們來談?wù)勅绾卧趕pringboot項(xiàng)目中加密數(shù)據(jù)庫密碼。
文本方案1。使用druid數(shù)據(jù)庫連接池1加密數(shù)據(jù)庫密碼。pom.xml引入了druid包。
為了方便其他操作,德魯伊 這里直接介紹s啟動器。
依賴groupidcom.alibaba/groupid工件id druid-spring-boot-starter/工件id版本$ {druid。版本}/版本/依賴關(guān)系2。使用com生成公鑰和私鑰。阿里巴巴。druid.filter.config.config工具。
ps:有兩種生成,一種是使用命令行,一種是直接寫工具類。本文中的示例由工具類直接生成。
工具類代碼如下
/** *阿里巴巴druid加解密規(guī)則:*明文密碼privatekey加密=加密密碼*加密密碼publickey解密=明文密碼*/public final class druid cryptography { private static string私鑰;私有靜態(tài)字符串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公共靜態(tài)字符串編碼(字符串明文){ system . out . println( 明文字符串: 明文);string密文= config tools . encrypt(private key,plaintext);system . out . println( 加密字符串: 密文);返回密文;}/* * *解密* @ param cipher * @ return */@ sneaky拋出公共靜態(tài)字符串解碼(字符串密文){system。out . println( 加密字符串: 密文);string plaintext = config tools . decrypt(public key,密文);system . out . println( 解密字符串: 明文 );返回明文;}3.修改數(shù)據(jù)庫的配置文件內(nèi)容信息。
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 .驅(qū)動程序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 #配置檢測需要關(guān)閉的空閑連接的頻率。單位是毫秒time between connectivity runsmillisec。ho0-@.com 60000 #配置連接在池中的最短存活時(shí)間(毫秒), minevictabl: 300000 #配置連接在池中的最長存活時(shí)間。單位為毫秒maxevictabl: 900000 #配置檢查連接是否有效validationqu:從對偶中選擇1 t:真t:假testonreturnecho 0-@ .c. om假w: :真statvi: :真#設(shè)置白名單,留空則允許訪問allow : url-pattern:/druid/* #控制臺管理用戶名和密碼登錄-us:登錄-password : filterconfig . decrypt . key = $ { spring . data source . public key }方案二:用jaypt 1加密數(shù)據(jù)庫密碼。pom.xml引入了jaypt包。依賴項(xiàng)groupidcom.github.ulisesbocchio/groupid工件id jas ypt-spring-boot-starter/工件id版本$ {jasypt。威瑞森}/版本/依賴關(guān)系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靜態(tài)字符串解碼(字符串密文){ system . out . println( 加密字符串: 密文);密文= enc( 密文 ) if(propertyvalueencryptionutils . isencryptedvalue(密文)){ string plaintext = propertyvalueencryptionutils . decrypt(密文,basictextencryptor);system . out . println( 解密字符串: 明文 );返回明文;} system . out . println( 解密失敗 );返回 }}3.修改數(shù)據(jù)庫的配置文件內(nèi)容信息。
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 .驅(qū)動程序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: #初始連接數(shù)initialsiz: 5 #最小連接池?cái)?shù)minidl: 10 #最大連接池?cái)?shù)maxactiv: 20 #配置獲取連接的超時(shí)期限maxwait: 60000 #配置檢測需要關(guān)閉的空閑連接的時(shí)間。單位是毫秒。timebetweenevictionrunsmillis: 60000 #配置一個(gè)連接在池中的最短存活時(shí)間,單位是毫秒。minevictabl: 300000 #配置連接在池中的最長存活時(shí)間。單位為毫秒maxevictabl: 900000 #配置檢查連接是否有效validationqu:從對偶中選擇1 t:真t:假testonreturnecho 0-@ .c. om假w: :真statvi: :真#設(shè)置白名單,留空則允許訪問管理用戶名和密碼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方案三:自定義實(shí)現(xiàn)原理:利用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公共靜態(tài)字符串編碼(字符串明文){ system . out . println( 明文字符串: 明文);byte[] key = hex.decodehex(秒retkey . tochararray);string密文= secureutil.aes(key)。encrypthex(明文);system . out . println( 加密字符串: 密文);返回密文;}/* * *解密* @ param cipher * @ return */@ sneaky拋出公共靜態(tài)字符串解碼(字符串密文){system。out . println( 加密字符串: 密文);byte[]key = hex . decode hex(secret key . tochararray);string plaintext = secure util . aes(key)。decryptstr(密文);system . out . println( 解密字符串: 明文 );返回明文;}/* * *明文加密* @ param明文* @ return */@潛行拋出公共靜態(tài)字符串編碼(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公共靜態(tài)字符串解碼(字符串密鑰,字符串密文){system。out . println( 加密字符串: 密文);byte[] key = hex . decode hex(secret key . tochararray);string plaintext = secure util . aes(key)。decryptstr(密文);system . out . println( 解密字符串: 明文 );返回明文;}}2.寫一個(gè)后處理器。
公共類druiddatasourceencyptbeanpostprocessor實(shí)現(xiàn)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: :真實(shí)秘密-k: datasourc: typ: com . alibaba . druid . pool . druid data source driverclassnam: com . mysql . cj . jdbc .驅(qū)動程序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: :真#設(shè)置白名單,留空則允許訪問allow : url-pattern:/druid/* #控制臺管理用戶名和密碼登錄-us:登錄-password : filter個(gè)人推薦jasypt,因?yàn)樗粌H可以加密密碼,還可以加密其他內(nèi)容。德魯伊只能加密數(shù)據(jù)庫密碼。至于自定義方案,屬于慣例,畢竟開源已經(jīng)如果你有什么,不要再自己做輪子了。最后一點(diǎn),如果jasypt高于2版,低于3.0.3版,那么配置中心的動態(tài)刷新配置,比如apollo或者nacos,都會失效(最新版本3.0.3官方表示已經(jīng)修復(fù)了這個(gè)問題)。
如果使用配置中心,jasypt建議使用版本3或者使用版本3.0.3的演示鏈接。
關(guān)于如何加密springboot項(xiàng)目的數(shù)據(jù)庫密碼,本文就講到這里。關(guān)于springboot數(shù)據(jù)庫密碼加密內(nèi)容的更多信息,請搜索我們之前的文章或者繼續(xù)瀏覽以下相關(guān)文章。希望大家以后支持我們!
標(biāo)簽:
字符串密碼
了解更多springboot數(shù)據(jù)庫密碼加密和解密(springboot數(shù)據(jù)庫連接密碼加密)相關(guān)內(nèi)容請關(guān)注本站點(diǎn)。