日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

SpringBoot中的敏感信息的配置進(jìn)行加密處理,這種方式你知道嗎?

來源: 責(zé)編: 時間:2023-08-20 23:17:13 762觀看
導(dǎo)讀環(huán)境:Springboot2.4.12 + Spring Cloud Context 3.0.5概述SpringBoot配置文件中的內(nèi)容通常情況下是明文顯示,安全性就比較低一些。在application.properties或application.yml,比如數(shù)據(jù)庫配置信息的密碼,Redis配置的密碼

環(huán)境:Springboot2.4.12 + Spring Cloud Context 3.0.56xx28資訊網(wǎng)——每日最新資訊28at.com

概述

SpringBoot配置文件中的內(nèi)容通常情況下是明文顯示,安全性就比較低一些。在application.properties或application.yml,比如數(shù)據(jù)庫配置信息的密碼,Redis配置的密碼等都是通過明文配置的,為了提供系統(tǒng)整體的安全性,我們需要對這些敏感的信息進(jìn)行加密處理,這樣即便你難道了我的配置信息你也獲取不到有價值的信息。6xx28資訊網(wǎng)——每日最新資訊28at.com

在Springboot下我們可以通過如下兩種方式簡單的實現(xiàn)敏感信息的加密處理:6xx28資訊網(wǎng)——每日最新資訊28at.com

  1. Jasypt這是國外的一個開源加密工具包,功能強(qiáng)大。
  2. 基于EnvironmentPostProcessor實現(xiàn)我們可以通過實現(xiàn)這么一個接口來實現(xiàn)自己的加解密處理,我們也可以通過引入spring-cloud-context包,在該包中提供了一個DecryptEnvironmentPostProcessor處理器進(jìn)行對加密信息進(jìn)行解密。

關(guān)于Jasypt網(wǎng)上介紹的很多,這里不做介紹。這里我們主要只講Spring Cloud Context中提供的這個EnvironmentPostprocessor。6xx28資訊網(wǎng)——每日最新資訊28at.com

環(huán)境配置

<dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-context</artifactId>  <version>3.0.5</version></dependency>

應(yīng)用配置6xx28資訊網(wǎng)——每日最新資訊28at.com

encrypt:  key: 123456789 #密鑰  salt: abcdef #加密的內(nèi)容使用了加鹽處理---spring:  cloud:    decrypt-environment-post-processor:      enabled: true #開啟解密功能

程序代碼

現(xiàn)在需要對custom.password這樣的一個key值進(jìn)行加密處理6xx28資訊網(wǎng)——每日最新資訊28at.com

custom:  password:  123456

首先需要對這明文生成加密的內(nèi)容,如下方式:6xx28資訊網(wǎng)——每日最新資訊28at.com

public static void main(String[] args) throws Exception {  String key = "123456789" ;  String salt = "abcdef" ;  String text = "123123" ;  KeyProperties keyProperties = new KeyProperties() ;  keyProperties.setKey(key) ;  keyProperties.setSalt(salt) ;  String result = TextEncryptorUtils.createTextEncryptor(keyProperties, null).encrypt(text) ;  System.out.println(result) ;}

通過上面的代碼就可以生成加密信息,接下來就是將加密后的內(nèi)容配置到配置文件中6xx28資訊網(wǎng)——每日最新資訊28at.com

custom:  password:  "{cipher}2a483a44681006be6f0730b1acb45325c6bd20abe37369ef4fdb52c2e194a365"

注意:這里的內(nèi)容是有格式要求的必須是:{cipher}開頭。這里還有一點(diǎn)需要注意有可能你運(yùn)行上面的代碼會報錯,報錯信息應(yīng)該是 “Illegal key size” (非法的密鑰大小),比如:AES加密算法在默認(rèn)JDK(Sun的JDK)配置情況下支持的密鑰大小是128位,一旦超過就會報錯,這時候你需要安裝Java加密擴(kuò)展(JCE)策略文件。下面提供了3個版本的JCE策略文件。6xx28資訊網(wǎng)——每日最新資訊28at.com

Java 6 JCE:6xx28資訊網(wǎng)——每日最新資訊28at.com

https://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html6xx28資訊網(wǎng)——每日最新資訊28at.com

Java 7 JCE6xx28資訊網(wǎng)——每日最新資訊28at.com

https://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html6xx28資訊網(wǎng)——每日最新資訊28at.com

Java 8 JCE6xx28資訊網(wǎng)——每日最新資訊28at.com

https://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html6xx28資訊網(wǎng)——每日最新資訊28at.com

下載對應(yīng)的版本后將其解壓如下位置:6xx28資訊網(wǎng)——每日最新資訊28at.com

JCE策略文件安裝目錄JCE策略文件安裝目錄6xx28資訊網(wǎng)——每日最新資訊28at.com

以上操作完成以后在運(yùn)行上面的程序就能輸出結(jié)果了,然后將結(jié)果替換配置文件內(nèi)容即可。6xx28資訊網(wǎng)——每日最新資訊28at.com

接下來測試:6xx28資訊網(wǎng)——每日最新資訊28at.com

@Value("${custom.password}")public String pwd ;  @GetMapping("/pwd")public String pwd() {  return pwd ;}

圖片6xx28資訊網(wǎng)——每日最新資訊28at.com

正確的輸出了我們的內(nèi)容6xx28資訊網(wǎng)——每日最新資訊28at.com

原理

關(guān)于EnvironmentPostProcessor接口編寫完成以后都會進(jìn)行配置,我們上面引入的context包就對DecryptEnvironmentPostProcessor進(jìn)行了配置:6xx28資訊網(wǎng)——每日最新資訊28at.com

配置EnvironmentPostProcessor配置EnvironmentPostProcessor6xx28資訊網(wǎng)——每日最新資訊28at.com

public class DecryptEnvironmentPostProcessor extends AbstractEnvironmentDecrypt implements EnvironmentPostProcessor, Ordered {  @Override  public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {    // 判斷是否開啟了功能    if (bootstrapEnabled(environment) || useLegacyProcessing(environment) || !isEnabled(environment)) {      return;    }    // 如果環(huán)境中不存在TextEncryptor那么也不會開啟    if (!ClassUtils.isPresent("org.springframework.security.crypto.encrypt.TextEncryptor", null)) {      return;    }    // 獲取當(dāng)前環(huán)境下的所有配置屬性    MutablePropertySources propertySources = environment.getPropertySources();    environment.getPropertySources().remove(DECRYPTED_PROPERTY_SOURCE_NAME);    // 解密所有的配置屬性,這里也就是入口了    Map<String, Object> map = TextEncryptorUtils.decrypt(this, environment, propertySources);    if (!map.isEmpty()) {      // 將解密后的配置添加到環(huán)境中(addFirst 表示了添加到具有最高優(yōu)先級的給定屬性源對象)      propertySources.addFirst(new SystemEnvironmentPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, map));    }  }  protected Boolean isEnabled(ConfigurableEnvironment environment) {    // 獲取配置屬性值,是否開啟功能    return environment.getProperty("spring.cloud.decrypt-environment-post-processor.enabled", Boolean.class, true);  }}

解密配置6xx28資訊網(wǎng)——每日最新資訊28at.com

public abstract class TextEncryptorUtils {  static Map<String, Object> decrypt(AbstractEnvironmentDecrypt decryptor, ConfigurableEnvironment environment, MutablePropertySources propertySources) {    // 獲取加解密服務(wù)接口    TextEncryptor encryptor = getTextEncryptor(decryptor, environment);    return decryptor.decrypt(encryptor, propertySources);  }  static TextEncryptor getTextEncryptor(AbstractEnvironmentDecrypt decryptor, ConfigurableEnvironment environment) {    Binder binder = Binder.get(environment);    // 將屬性配置以 ‘encrypt’開頭的都綁定到KeyProperties對象中    KeyProperties keyProperties = binder.bind(KeyProperties.PREFIX, KeyProperties.class).orElseGet(KeyProperties::new);    // 檢查是否配置了encrypt.key 等核心的信息    if (TextEncryptorUtils.keysConfigured(keyProperties)) {      decryptor.setFailOnError(keyProperties.isFailOnError());      // 檢查是否當(dāng)前CLASSPATH中是否存在RsaSecretEncryptor      if (ClassUtils.isPresent("org.springframework.security.rsa.crypto.RsaSecretEncryptor", null)) {        RsaProperties rsaProperties = binder.bind(RsaProperties.PREFIX, RsaProperties.class).orElseGet(RsaProperties::new);        return TextEncryptorUtils.createTextEncryptor(keyProperties, rsaProperties);      }      // 如果你沒有使用及配置Rsa相關(guān)的,那么就會在這里創(chuàng)建加解密服務(wù)接口,這里跟蹤了下使用的是AES加密算法      return new EncryptorFactory(keyProperties.getSalt()).create(keyProperties.getKey());    }    // no keys configured    return new TextEncryptorUtils.FailsafeTextEncryptor();  }}

獲取到了加解密服務(wù)接口以后,接下來就是對配置屬性進(jìn)行解密操作。6xx28資訊網(wǎng)——每日最新資訊28at.com

public class AbstractEnvironmentDecrypt {  public static final String ENCRYPTED_PROPERTY_PREFIX = "{cipher}";  protected Map<String, Object> decrypt(TextEncryptor encryptor, PropertySources propertySources) {    Map<String, Object> properties = merge(propertySources);    // 解密處理    decrypt(encryptor, properties);    return properties;  }  protected void decrypt(TextEncryptor encryptor, Map<String, Object> properties) {    // 開始替換所有以{cipher}開頭的屬性值    properties.replaceAll((key, value) -> {      String valueString = value.toString();      if (!valueString.startsWith(ENCRYPTED_PROPERTY_PREFIX)) {        return value;      }      // 解密數(shù)據(jù), key 配置屬性的key, valueString要解密的數(shù)據(jù)      return decrypt(encryptor, key, valueString);    });  }  protected String decrypt(TextEncryptor encryptor, String key, String original) {    // 截取{cipher}之后的內(nèi)容    String value = original.substring(ENCRYPTED_PROPERTY_PREFIX.length());    try {       // 解密數(shù)據(jù)      value = encryptor.decrypt(value);      return value;    } catch (Exception e) {      // ...      return "";    }  }}

整個源碼處理還是非常簡單的。6xx28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-6190-0.htmlSpringBoot中的敏感信息的配置進(jìn)行加密處理,這種方式你知道嗎?

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: GitHub和碼云上,七個H5頁面制作工具推薦

下一篇: Go 語言中排序的三種方法

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 钟山县| 梁平县| 南京市| 泉州市| 靖边县| 那曲县| 安福县| 南阳市| 晋江市| 蕉岭县| 沽源县| 叙永县| 东乡| 临沂市| 修文县| 贺兰县| 即墨市| 弥渡县| 陵川县| 竹溪县| 三原县| 高要市| 天门市| 那坡县| 栾川县| 新泰市| 昌图县| 隆尧县| 滦平县| 临澧县| 尉氏县| 达日县| 九寨沟县| 昌邑市| 体育| 前郭尔| 台山市| 陈巴尔虎旗| 长治市| 呼伦贝尔市| 习水县|