|
1 | 1 | # Property Guard |
2 | 2 |
|
3 | | -## Introduction |
| 3 | +`property-guard-spring-boot-starter` is a utility that can help you protect secret values in Spring Boot configurations. |
4 | 4 |
|
5 | | -This feature is designed to protect the security of configurations and data, to a certain extent, to control the flow of developers leading to the leakage of sensitive information. |
| 5 | +## Example usage |
6 | 6 |
|
7 | | -## Prerequisites |
| 7 | +### 1. Implementation this module |
8 | 8 |
|
9 | | -This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience. Except this, this module is designed for Spring Boot framework, so you have to install Spring Boot (v3) in your application. |
10 | | - |
11 | | -## Installation |
12 | | - |
13 | | -### If you are using `Maven` |
14 | | - |
15 | | -It is quite simple to install this module by `Maven`. The only thing you need to do is find your `pom.xml` file in the project, then find the `<dependencies>` node in the `<project>` node, and add the following codes to `<dependencies>` node: |
16 | | - |
17 | | -```xml |
18 | | -<dependency> |
19 | | - <groupId>cn.org.codecrafters</groupId> |
20 | | - <artifactId>property-guard-spring-boot-starter</artifactId> |
21 | | - <version>${property-guard-spring-boot-starter.version}</version> |
22 | | -</dependency> |
23 | | -``` |
24 | | - |
25 | | -And run `mvn dependency:get` in your project root folder(i.e., if your `pom.xml` is located at `/path/to/your/project/pom.xml`, then your current work folder should be `/path/to/your/project`), then `Maven` will automatically download the `jar` archive from `Maven Central Repository`. This could be **MUCH EASIER** if you are using IDE(i.e., IntelliJ IDEA), the only thing you need to do is click the refresh button of `Maven`. |
26 | | - |
27 | | -If you are restricted using the Internet, and have to make `Maven` offline, you could follow the following steps. |
28 | | - |
29 | | -1. Download the `jar` file from any place you can get and transfer the `jar` files to your work computer. |
30 | | -2. Move the `jar` files to your local `Maven` Repository as the path of `/path/to/maven_local_repo/cn/org/codecrafters/property-guard-spring-boot-starter/`. |
31 | | - |
32 | | -### If you are using `Gradle` |
33 | | - |
34 | | -Add this module to your project with `Gradle` is much easier than doing so with `Maven`. |
35 | | - |
36 | | -Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure in the build script: |
37 | | - |
38 | | -```groovy |
39 | | -implementation 'cn.org.codecrafters:property-guard-spring-boot-starter:${property-guard-spring-boot-starter.version}' |
40 | | -``` |
41 | | - |
42 | | -### If you are not using `Maven` or `Gradle` |
43 | | - |
44 | | -1. Download the `jar` file from the Internet. |
45 | | -2. Create a folder in your project and name it as a name you like(i.e., for me, I prefer `vendor`). |
46 | | -3. Put the `jar` file to the folder you just created in Step 2. |
47 | | -4. Add this folder to your project `classpath`. |
48 | | - |
49 | | -## Usage |
50 | | - |
51 | | -First, you need a 16-bit-long secret. If you don't have a good way to get a secret, you could consider using our `utils.com.onixbyte.devkit.AesUtil` or `com.onixbyte.simplejwt.SecretCreator` to create a secret. |
52 | | - |
53 | | -For example: |
54 | | -```java |
55 | | -import utils.com.onixbyte.devkit.AesUtil; |
56 | | -import com.onixbyte.simplejwt.SecretCreator; |
57 | | - |
58 | | -class GenerateRandomKeySample { |
59 | | - public static void main(String[] args) { |
60 | | - var secret1 = AesUtil.generateRandomSecret(); |
61 | | - var secret2 = SecretCreator.createSecret(16, true, true, true); |
62 | | - } |
| 9 | +```kotlin |
| 10 | +dependencies { |
| 11 | + implementation(platform("com.onixbyte:devkit-bom:$devKitVersion")) |
| 12 | + implementation("com.onixbyte:devkit-utils") |
| 13 | + implementation("com.onixbyte:property-guard-spring-boot-starter") |
63 | 14 | } |
64 | 15 | ``` |
65 | 16 |
|
66 | | -Then, remember this secret and encrypt the configuration properties that are required high security. For example: |
| 17 | +### 2. Generate a secret |
67 | 18 |
|
68 | | -```java |
69 | | -import utils.com.onixbyte.devkit.AesUtil; |
| 19 | +Use the following codes to get a random secret. |
70 | 20 |
|
71 | | -class EncryptSample { |
72 | | - public static void main(String[] args) { |
73 | | - var dataNeedEncryption = "Sample Value"; |
74 | | - var key = "3856faef0d2d4f33"; |
75 | | - var encryptedData = AesUtil.encrypt(dataNeedEncryption, key); |
| 21 | +```java |
| 22 | +@SpringBootTest |
| 23 | +class SpringBootApplicationTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + void contextLoads() { |
| 27 | + System.out.println(AesUtil.generateRandomSecret()); // Output: a 16-char long secret |
76 | 28 | } |
77 | 29 | } |
78 | 30 | ``` |
79 | 31 |
|
80 | | -After that, copy the encrypted data to `application.properties` or `application.yml`. |
81 | | - |
82 | | -For `yml`: |
83 | | -```yaml |
84 | | -app: |
85 | | - sample-configuration: pe:t4YBfv8M9ZmTzWgTi2gJqg== # "pe:" is the prefix that declare that this property is encrypted. |
86 | | -``` |
87 | | -
|
88 | | -For `properties`: |
89 | | -```properties |
90 | | -app.sample-configuration=pe:t4YBfv8M9ZmTzWgTi2gJqg== |
91 | | -``` |
| 32 | +Or you can write a 16-char long secret by yourself. |
92 | 33 |
|
93 | | -## Contact |
| 34 | +### 3. Encrypt your secret properties and place them into your configuration file |
94 | 35 |
|
95 | | -If you have any suggestions, ideas, don't hesitate contacting us via [GitHub Issues](https://github.com/CodeCraftersCN/jdevkit/issues/new) or [Discord Community](https://discord.gg/NQK9tjcBB8). |
| 36 | +### 4. Run application with parameter `--pg.key=$your_secret` |
96 | 37 |
|
97 | | -If you face any bugs while using our library and you are able to fix any bugs in our library, we would be happy to accept pull requests from you on [GitHub](https://github.com/CodeCraftersCN/jdevkit/compare). |
0 commit comments