feat: remove deprecated module property-guard-spring-boot-starter
This commit is contained in:
@@ -1,37 +0,0 @@
|
|||||||
# Property Guard
|
|
||||||
|
|
||||||
`property-guard-spring-boot-starter` is a utility that can help you protect secret values in Spring Boot configurations.
|
|
||||||
|
|
||||||
## Example usage
|
|
||||||
|
|
||||||
### 1. Implementation this module
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
dependencies {
|
|
||||||
implementation(platform("com.onixbyte:devkit-bom:$devKitVersion"))
|
|
||||||
implementation("com.onixbyte:devkit-utils")
|
|
||||||
implementation("com.onixbyte:property-guard-spring-boot-starter")
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Generate a secret
|
|
||||||
|
|
||||||
Use the following codes to get a random secret.
|
|
||||||
|
|
||||||
```java
|
|
||||||
@SpringBootTest
|
|
||||||
class SpringBootApplicationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
System.out.println(AesUtil.generateRandomSecret()); // Output: a 16-char long secret
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Or you can write a 16-char long secret by yourself.
|
|
||||||
|
|
||||||
### 3. Encrypt your secret properties and place them into your configuration file
|
|
||||||
|
|
||||||
### 4. Run application with parameter `--pg.key=$your_secret`
|
|
||||||
|
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2025 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
import java.net.URI
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
java
|
|
||||||
id("java-library")
|
|
||||||
id("maven-publish")
|
|
||||||
id("signing")
|
|
||||||
}
|
|
||||||
|
|
||||||
val artefactVersion: String by project
|
|
||||||
val projectUrl: String by project
|
|
||||||
val projectGithubUrl: String by project
|
|
||||||
val licenseName: String by project
|
|
||||||
val licenseUrl: String by project
|
|
||||||
|
|
||||||
group = "com.onixbyte"
|
|
||||||
version = artefactVersion
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
java {
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
|
||||||
withSourcesJar()
|
|
||||||
withJavadocJar()
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<JavaCompile> {
|
|
||||||
options.encoding = "UTF-8"
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<Jar> {
|
|
||||||
exclude("logback.xml")
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
compileOnly(libs.slf4j)
|
|
||||||
implementation(libs.logback)
|
|
||||||
api(project(":devkit-core"))
|
|
||||||
api(project(":devkit-utils"))
|
|
||||||
implementation(libs.springBoot.autoconfigure)
|
|
||||||
implementation(libs.springBoot.starter.logging)
|
|
||||||
implementation(libs.springBoot.configurationProcessor)
|
|
||||||
|
|
||||||
testImplementation(platform(libs.junit.bom))
|
|
||||||
testImplementation(libs.junit.jupiter)
|
|
||||||
testImplementation(libs.springBoot.starter.test)
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.test {
|
|
||||||
useJUnitPlatform()
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
|
||||||
publications {
|
|
||||||
create<MavenPublication>("propertyGuardSpringBootStarter") {
|
|
||||||
groupId = group.toString()
|
|
||||||
artifactId = "property-guard-spring-boot-starter"
|
|
||||||
version = artefactVersion
|
|
||||||
|
|
||||||
pom {
|
|
||||||
name = "Property Guard Spring Boot Starter"
|
|
||||||
description = "This module is made for protecting your secret properties in your spring application properties."
|
|
||||||
url = projectUrl
|
|
||||||
|
|
||||||
licenses {
|
|
||||||
license {
|
|
||||||
name = licenseName
|
|
||||||
url = licenseUrl
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
scm {
|
|
||||||
connection = "scm:git:git://github.com:OnixByte/JDevKit.git"
|
|
||||||
developerConnection = "scm:git:git://github.com:OnixByte/JDevKit.git"
|
|
||||||
url = projectGithubUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
developers {
|
|
||||||
developer {
|
|
||||||
id = "zihluwang"
|
|
||||||
name = "Zihlu Wang"
|
|
||||||
email = "really@zihlu.wang"
|
|
||||||
timezone = "Asia/Hong_Kong"
|
|
||||||
}
|
|
||||||
|
|
||||||
developer {
|
|
||||||
id = "siujamo"
|
|
||||||
name = "Siu Jam'o"
|
|
||||||
email = "jamo.siu@outlook.com"
|
|
||||||
timezone = "Asia/Shanghai"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
from(components["java"])
|
|
||||||
|
|
||||||
signing {
|
|
||||||
sign(publishing.publications["propertyGuardSpringBootStarter"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
name = "sonatypeNexus"
|
|
||||||
url = URI(providers.gradleProperty("repo.maven-central.host").get())
|
|
||||||
credentials {
|
|
||||||
username = providers.gradleProperty("repo.maven-central.username").get()
|
|
||||||
password = providers.gradleProperty("repo.maven-central.password").get()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-122
@@ -1,122 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2025 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.onixbyte.propertyguard.autoconfiguration;
|
|
||||||
|
|
||||||
import com.onixbyte.devkit.utils.AesUtil;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
|
||||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
|
||||||
import org.springframework.core.env.MapPropertySource;
|
|
||||||
import org.springframework.core.env.SimpleCommandLinePropertySource;
|
|
||||||
|
|
||||||
import java.security.GeneralSecurityException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@code PropertyGuard} is a utility class designed for encrypting configuration properties in
|
|
||||||
* Spring Boot applications.
|
|
||||||
* <p>
|
|
||||||
* Spring Boot applications often need to store sensitive configuration details such as database
|
|
||||||
* passwords, API keys, etc. To ensure that these sensitive pieces of information are not exposed
|
|
||||||
* to the public, developers can utilize the {@code PropertyGuard} class to encrypt and store them
|
|
||||||
* within configuration files.
|
|
||||||
* <p>
|
|
||||||
* <b>Usage</b>
|
|
||||||
* You need a 16-char long secret for encrypting a configuration property. You can get this secret
|
|
||||||
* on your own, or use the helper utility class by the following code:
|
|
||||||
* <pre>{@code
|
|
||||||
* var secret = AesUtil.generateRandomSecret(); // Let's presume the result is "3856faef0d2d4f33"
|
|
||||||
* }</pre>
|
|
||||||
* <p>
|
|
||||||
* Then, in {@code application.yml} or {@code application.properties}, change the original value
|
|
||||||
* from plain text to encrypted value with the prefix "<code>pg:</code>".
|
|
||||||
* <pre>{@code
|
|
||||||
* # original
|
|
||||||
* app.example-properties=Sample Value
|
|
||||||
*
|
|
||||||
* # encrypted with key 3856faef0d2d4f33
|
|
||||||
* app.example-properties=pg:t4YBfv8M9ZmTzWgTi2gJqg==
|
|
||||||
* }</pre>
|
|
||||||
* After that, before running, you need to add the command line arguments "pg.key" as the following
|
|
||||||
* codes: {@code --pg.key=<the secret>}.
|
|
||||||
* <p>
|
|
||||||
* This class is extracted from <a href="https://baomidou.com/pages/e0a5ce/">MyBatis-Plus</a>.
|
|
||||||
*
|
|
||||||
* @author hubin@baomidou
|
|
||||||
* @version 1.1.0
|
|
||||||
* @see EnvironmentPostProcessor
|
|
||||||
* @since 1.1.0 (3.3.2 of MyBatis-Plus)
|
|
||||||
*/
|
|
||||||
@Deprecated(forRemoval = true)
|
|
||||||
public class PropertyGuard implements EnvironmentPostProcessor {
|
|
||||||
|
|
||||||
private final static Logger log = LoggerFactory.getLogger(PropertyGuard.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a {@link PropertyGuard} instance.
|
|
||||||
*/
|
|
||||||
public PropertyGuard() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Process the encryption environment variables.
|
|
||||||
*
|
|
||||||
* @param environment the environment to post-process
|
|
||||||
* @param application the application to which the environment belongs
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
|
||||||
// Get the key for encryption from command line.
|
|
||||||
var encryptionKey = "";
|
|
||||||
for (var ps : environment.getPropertySources()) {
|
|
||||||
if (ps instanceof SimpleCommandLinePropertySource source) {
|
|
||||||
encryptionKey = source.getProperty("%s.key".formatted(PREFIX));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Optional.ofNullable(encryptionKey).map((key) -> !key.isEmpty()).orElse(false)) {
|
|
||||||
var map = new HashMap<String, Object>();
|
|
||||||
for (var propertySources : environment.getPropertySources()) {
|
|
||||||
if (propertySources instanceof OriginTrackedMapPropertySource source) {
|
|
||||||
for (var name : source.getPropertyNames()) {
|
|
||||||
if (source.getProperty(name) instanceof String str) {
|
|
||||||
if (str.startsWith("%s:".formatted(PREFIX))) {
|
|
||||||
try {
|
|
||||||
map.put(name, AesUtil.decrypt(str.substring(3), encryptionKey));
|
|
||||||
} catch (GeneralSecurityException e) {
|
|
||||||
log.error("Unable to decrypt param {}", name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Put the decrypted data into environment variables, and made them at top-level.
|
|
||||||
if (!map.isEmpty()) {
|
|
||||||
environment.getPropertySources().addFirst(new MapPropertySource("custom-encrypt", map));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String PREFIX = "pg";
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
org.springframework.boot.env.EnvironmentPostProcessor=com.onixbyte.propertyguard.autoconfiguration.PropertyGuard
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!--
|
|
||||||
~ Copyright (C) 2023-2024 OnixByte.
|
|
||||||
~
|
|
||||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
~ you may not use this file except in compliance with the License.
|
|
||||||
~ You may obtain a copy of the License at
|
|
||||||
~
|
|
||||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
~
|
|
||||||
~ Unless required by applicable law or agreed to in writing, software
|
|
||||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
~
|
|
||||||
~ See the License for the specific language governing permissions and
|
|
||||||
~ limitations under the License.
|
|
||||||
-->
|
|
||||||
|
|
||||||
<configuration>
|
|
||||||
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
|
||||||
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
|
||||||
|
|
||||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
|
||||||
<pattern>${COLOURFUL_OUTPUT}</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
<root level="INFO">
|
|
||||||
<appender-ref ref="STDOUT"/>
|
|
||||||
</root>
|
|
||||||
</configuration>
|
|
||||||
@@ -29,6 +29,5 @@ include(
|
|||||||
"simple-jwt-facade",
|
"simple-jwt-facade",
|
||||||
"simple-jwt-authzero",
|
"simple-jwt-authzero",
|
||||||
"simple-jwt-spring-boot-starter",
|
"simple-jwt-spring-boot-starter",
|
||||||
"property-guard-spring-boot-starter",
|
|
||||||
"simple-serial-spring-boot-starter"
|
"simple-serial-spring-boot-starter"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user