Apollo 加密示例

This commit is contained in:
YunaiV
2020-01-27 17:41:51 +08:00
parent 7938fdc60a
commit 934655f1f3
6 changed files with 133 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-45-apollo-demo-jasypt</artifactId>
<dependencies>
<!-- 实现对 SpringMVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.5.1</version>
</dependency>
<!-- 实现对 Jasypt 实现自动化配置 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
<!-- <version>2.1.2</version>-->
<!-- <scope>test</scope>-->
</dependency>
<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab45.apollodemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,20 @@
package cn.iocoder.springboot.lab45.apollodemo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Value("${spring.application.name}")
private String applicationName;
@GetMapping("/test")
public String test() {
return applicationName;
}
}

View File

@@ -0,0 +1,12 @@
server:
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
app:
id: demo-application-jasypt # 使用的 Apollo 的项目(应用)编号
apollo:
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
bootstrap:
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
eagerLoad:
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。

View File

@@ -0,0 +1,41 @@
package cn.iocoder.springboot.lab45.apollodemo;
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptTest {
@Autowired
private StringEncryptor encryptor;
@Test
public void encode() {
// 第一个加密
// String applicationName = "demo-application";
// System.out.println(encryptor.encrypt(applicationName));
// // 第二个加密
applicationName = "demo-app";
System.out.println(encryptor.encrypt(applicationName));
}
@Value("${spring.application.name}")
// @NacosValue("${spring.application.name}")
private String applicationName;
@Test
public void print() {
System.out.println(applicationName);
}
@Value("${jasypt.encryptor.password}")
private String password;
}

View File

@@ -15,6 +15,7 @@
<module>lab-45-apollo-demo</module>
<module>lab-45-apollo-demo-profiles</module>
<module>lab-45-apollo-demo-auto-refresh</module>
<module>lab-45-apollo-demo-jasypt</module>
</modules>