怎么查看网站是哪个公司建的,wordpress开发手册下载,晨光文具店网站建设,怎样设置 自己的网站目录 前言 一、Jasypt简介 二、运用场景 三、整合Jasypt 2.1.环境配置 2.2.添加依赖 2.3.添加Jasypt配置 2.4.编写加/解密工具类 2.5.自定义加密属性前缀和后缀 2.6.防止密码泄露措施 2.61.自定义加密器 2.6.2通过环境变量指定加密盐值 总结 前言 在以往的多数项目中#xff0… 目录 前言 一、Jasypt简介 二、运用场景 三、整合Jasypt 2.1.环境配置 2.2.添加依赖 2.3.添加Jasypt配置 2.4.编写加/解密工具类 2.5.自定义加密属性前缀和后缀 2.6.防止密码泄露措施 2.61.自定义加密器 2.6.2通过环境变量指定加密盐值 总结 前言 在以往的多数项目中配置文件中的数据库密码、redis密码、nacos密码等敏感性信息一般是以明文形式存在存在泄露的风险因此对敏感信息加固是很有必要加固的一个重要环节就是对重要信息做加密处理。
这里简单的介绍一下Jasypt加密
一、Jasypt简介
Jasypt 是一个 java 库可以使开发者不需要太多操作来给 Java 项目添加基本加密功能而且不需要知道加密原理。Jasypt 为开发人员提供了一种简单易用加密功能包括密码认证、字符串加密等。
二、运用场景
一般来说项目配置文件里所有涉及信息安全的配置项或字段都应该做处理典型的比如
数据库密码如mysql、oracle缓存中间件的密码如 redis、mongodb其他中间件如消息中间件、zk、nacos等第三方服务的如appid、 Access_Key…
三、整合Jasypt
官方示例GitHub - ulisesbocchio/jasypt-spring-boot-samples: Sample apps using jasypt-spring-boot 2.1.环境配置
SpringBoot 2.0以上Jasypt 3.0.5jdk1.8
2.2.添加依赖
在项目 pom.xml 添加 Jasypt 相关依赖。
!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter --
dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversion3.0.5/version
/dependency 2.3.添加Jasypt配置
jasypt:encryptor:password: 123456algorithm: PBEWITHHMACSHA512ANDAES_256iv-generator-classname: org.jasypt.iv.RandomIvGeneratorsalt-generator-classname: org.jasypt.salt.RandomSaltGeneratorstring-output-type: base64provider-name: SunJCEpool-size: 1key-obtention-iterations: 1000property:# 标识为加密属性的前缀prefix: ENC(# 标识为加密属性的后缀suffix: ) 2.4.编写加/解密工具类
package com.bexk.util;import java.util.Base64;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;/*** Description: 加解密密算法* Author developer* Date 2024/10/11 9:48 上午*/
public class JasyptUtil {/*** PBE 算法*/public static final String PBE_ALGORITHMS_MD5_DES PBEWITHMD5ANDDES;public static final String PBE_ALGORITHMS_SHA512_AES_256 PBEWithHMACSHA512ANDAES_256;private JasyptUtil() {}/*** 加密** param encryptedStr 加密字符串* param password 盐值* return*/public static String encrypt(String encryptedStr, String password) {return encrypt(encryptedStr, PBE_ALGORITHMS_MD5_DES, password);}/*** 加密** param encryptedStr 加密字符串* param algorithm 加密算法* param password 盐值* return*/public static String encrypt(String encryptedStr, String algorithm, String password) { StandardPBEStringEncryptor encryptor new StandardPBEStringEncryptor();EnvironmentPBEConfig config new EnvironmentPBEConfig();// 指定加密算法config.setAlgorithm(algorithm);// 加密盐值config.setPassword(password);config.setIvGeneratorClassName(org.jasypt.iv.NoIvGenerator);encryptor.setConfig(config);// 加密return encryptor.encrypt(encryptedStr);}/*** 解密** param decryptStr 解密字符串* param password 盐值* return*/public static String decrypt(String decryptStr, String password) {return decrypt(decryptStr, PBE_ALGORITHMS_MD5_DES, password);}/*** 解密** param decryptStr 解密字符串* param algorithm 指定解密算法* param password 盐值* return*/public static String decrypt(String decryptStr, String algorithm, String password) {StandardPBEStringEncryptor encryptor new StandardPBEStringEncryptor();EnvironmentPBEConfig config new EnvironmentPBEConfig();// 指定解密算法解密算法要与加密算法一一对应config.setAlgorithm(algorithm);// 加密秘钥config.setPassword(password);config.setIvGeneratorClassName(org.jasypt.iv.NoIvGenerator);encryptor.setConfig(config);// 解密return encryptor.decrypt(decryptStr);}}
修改配置文件
如图通过编写加/解密工具类得到对应的加密结果然后将配置文件的原始明文密码替换成上一步对应的结果并通过 ENC(加密结果) 包裹起来。
加密前 加密后 2.5.自定义加密属性前缀和后缀
如果您只想为加密属性使用不同的前缀/后缀则可以继续使用所有默认实现只需覆盖 application.yml 或 application.properties中的以下属性(property)
jasypt:encryptor:property:prefix: ENC[suffix: ]2.6.防止密码泄露措施
若使用的是默认的加密规则会让当自定义加密盐值(jasypt.encryptor.password) 泄漏可能变得不安全。那么如何进一步防止密码泄露呢 2.61.自定义加密器
自定义加密规则非常简单只需要提供自定义的加密器配置类然后通过jasypt.encryptor.bean配置指定加密配置类即可。
Bean(name encryptorBean)static public StringEncryptor stringEncryptor() {PooledPBEStringEncryptor encryptor new PooledPBEStringEncryptor();SimpleStringPBEConfig config new SimpleStringPBEConfig();config.setPassword(password);config.setAlgorithm(PBEWithHMACSHA512ANDAES_256);config.setKeyObtentionIterations(1000);config.setPoolSize(1);config.setProviderName(SunJCE);config.setSaltGeneratorClassName(org.jasypt.salt.RandomSaltGenerator);config.setIvGeneratorClassName(org.jasypt.iv.NoIvGenerator);config.setStringOutputType(base64);encryptor.setConfig(config);return encryptor;}
也将自定义加密器添加到 Spring IoC 容器中。
Configuration
public class JasyptConfig {/*** 加解密盐值*/Value(${jasypt.encryptor.password})private String password;// Bean(jasyptStringEncryptor)Bean(encryptorBean)public StringEncryptor myStringEncryptor() {PooledPBEStringEncryptor encryptor new PooledPBEStringEncryptor();SimpleStringPBEConfig config new SimpleStringPBEConfig();config.setPassword(password);config.setAlgorithm(PBEWithHMACSHA512ANDAES_256);config.setKeyObtentionIterations(1000);config.setPoolSize(1);config.setProviderName(SunJCE);config.setSaltGeneratorClassName(org.jasypt.salt.RandomSaltGenerator);config.setIvGeneratorClassName(org.jasypt.iv.NoIvGenerator);config.setStringOutputType(base64);encryptor.setConfig(config);return encryptor;}
}修改配置文件
jasypt:encryptor:# 指定加解密bean的名称默认jasyptStringEncryptorbean: encryptorBean# 盐值password: 123456#省略其它
注意事项Jasypt默认加解密器bean的Name为 jasyptStringEncryptor若不想在配置文件中指定自定义加密器名称需将自定义加密器bean的Name设置为jasyptStringEncryptor否则将不生效。
Springcloud项目最好不要采用自定义容易出现找不到bean的问题。
2.6.2通过环境变量指定加密盐值
方式一直接作为程序启动时的命令行参数
java -jar test.jar --jasypt.encryptor.password盐值
方式二直接作为程序启动时的应用环境变量
java -Djasypt.encryptor.password盐值 -jar test.jar
如果通过Docker部署请在ENTRYPOINT加上对应参数比如
ENTRYPOINT [java,-Djasypt.encryptor.passwordtest,-jar,test.jar] 方式三直接作为系统环境变量 1. 设置系统环境变量 JASYPT_PWD 在windows系统设置 在eclipse设置如图 在idea中设置需要通过VM options设置如图 在linux系统设置
#打开全局配置文件
sudo vim /etc/profile
#编辑全局配置文件
export JASYPT_PWDnrmZtkF7T0kjG
#重载profile配置文件
source /etc/profile
2. Spring Boot的项目配置文件指定系统环境变量
jasypt:encryptor:password: ${JASYPT_PWD:123456} 总结
本文介绍了如何在Springboot项目中使用Jasypt对配置文件中的敏感信息进行加密包括环境配置、依赖添加、配置设置、自定义加密器和使用环境变量管理盐值以提升项目的安全性。 文章转载自: http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.rdymd.cn.gov.cn.rdymd.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.rykx.cn.gov.cn.rykx.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn