1. 程式人生 > >攜程 Apollo 配置中心 | 學習筆記(七) | 如何將配置檔案敏感資訊加密?

攜程 Apollo 配置中心 | 學習筆記(七) | 如何將配置檔案敏感資訊加密?

以上為相關係列文章

通過上述文章,相信我們的環境基本搭建完成!

下面,我們將瞭解如何對存放在apollo配置中心的檔案進行脫敏處理。

一、需求

   當我們把我們專案中的所有配置資訊,都放入到apollo配置中心時,可能存在一些敏感配置資訊,不方便讓其他人檢視到。這時候我們就需要對我們的敏感資訊進行脫敏處理!

二、正文

    以下內容需要搭建好相關環境,這裡就不做過多介紹了,在專欄中都有相關介紹。如果有遇到問題,可以在下方留言。

這裡,我選用了jasypt 對相關內容進行加密處理。

2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>apollo-jasypt</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>apollo-demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
                <dependency>
                    <groupId>com.github.ulisesbocchio</groupId>
                    <artifactId>jasypt-spring-boot-starter</artifactId>
                    <version>1.18-SNAPSHOT</version>
                </dependency>
                <dependency>
                     <groupId>com.ctrip.framework.apollo</groupId>
                     <artifactId>apollo-client</artifactId>
                     <version>0.10.2</version>
                </dependency>
         </dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2.1 JasyptController

獲取加解密內容

package com.example.demo.controller;

import org.jasypt.encryption.StringEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class JasyptController {

    private static Logger logger = LoggerFactory.getLogger(JasyptController.class);

    @Autowired
    private StringEncryptor stringEncryptor;

    private static final String ENCRYPTED_VALUE_PREFIX = "ENC(";
    private static final String ENCRYPTED_VALUE_SUFFIX = ")";


    public static boolean isEncryptedValue(final String value) {
        if (value == null) {
            return false;
        }
        final String trimmedValue = value.trim();
        return (trimmedValue.startsWith(ENCRYPTED_VALUE_PREFIX) &&
                trimmedValue.endsWith(ENCRYPTED_VALUE_SUFFIX));
    }

    private static String getInnerEncryptedValue(final String value) {
        return value.substring(
            ENCRYPTED_VALUE_PREFIX.length(),
            (value.length() - ENCRYPTED_VALUE_SUFFIX.length()));
    }

    @RequestMapping(value = "/encrypt", method = RequestMethod.POST)
    public
    @ResponseBody
    String encrypt(
        @RequestParam("text") String text) {
        String encrypted = stringEncryptor.encrypt(text.trim());
        logger.info("ORIGINAL: " + text);
        logger.info("ENCRYPTED: " + encrypted);
        logger.info("DECRYPTED: " + stringEncryptor.decrypt(encrypted));
        return String.format("ENC(%s)", encrypted);
    }

    @RequestMapping(value = "/decrypt", method = RequestMethod.POST)
    public
    @ResponseBody
    String decrypt(
        @RequestParam("text") String text) {
        String decrypted = stringEncryptor.decrypt(isEncryptedValue(text) ? getInnerEncryptedValue(text) : text);
        logger.info("ORIGINAL: " + text);
        logger.info("DECRYPTED: " + decrypted);
        logger.info("ENCRYPTED: " + String.format("ENC(%s)", stringEncryptor.encrypt(decrypted)));
        return decrypted;
    }
}

2.2 ApolloDemoApplication

@SpringBootApplication
public class ApolloDemoApplication {

	public static void main(String[] args) {
		System.setProperty("jasypt.encryptor.password", "password");
		SpringApplication.run(ApolloDemoApplication.class, args);
	}
}

2.3SampleController

@RestController
@EnableApolloConfig
public class SampleController {

    @Value("${secret}")
    private String secret;
    
    @Value("${app.name}")
    private String name;

    @RequestMapping("/")
    @ResponseBody
    public String helloWorld() {
        return "Hello, Secret : " + secret + ",  app name: "+name;
    }

}

2.4 敏感資訊加密

對value merlinsecret 加密為輸出值為:ENC(ymHgopf/IWypH5zu8kX9JdwyuiZcZvme)

釋出到apollo配置中心

2.5 結果

通過訪問localhost:8082 可以看到資料已經解密了。

原始碼釋出到GitHub 和 碼雲 中:

碼雲:https://gitee.com/573059382/Apollo-Learning