Merge remote-tracking branch 'origin/release/v2.0.0' into release/v2.0.0

This commit is contained in:
zihluwang
2025-02-18 22:04:35 +08:00
8 changed files with 73 additions and 193 deletions
+6
View File
@@ -1,5 +1,11 @@
# JDevKit # JDevKit
![Static Badge](https://img.shields.io/badge/tag-v2.0.0-orange)
![Static Badge](https://img.shields.io/badge/maven_central-v2.0.0-orange)
![Static Badge](https://img.shields.io/badge/licence-Apache_2.0-green)
![Static Badge](https://img.shields.io/badge/JDK-%E2%89%A517-blue)
JDevKit is a Java Development Kit that offers a set of convenient tools for writing code efficiently. JDevKit is a Java Development Kit that offers a set of convenient tools for writing code efficiently.
## Installation and Usage ## Installation and Usage
+1 -9
View File
@@ -2,12 +2,4 @@
## Introduction ## Introduction
This module is the main part of `JDevKit`, an open-source Java class library that provides a set of convenient tools to streamline code development and enhance productivity. This module serves as the basement of other modules. This module serves as the basement of `JDevKit`. It provides some base exceptions that `JDevKit` might use.
## Prerequisites
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience.
## Installation
You don't have to install this module at all, any module which is from `JDevKit` contains this `devkit-core` module.
+9 -47
View File
@@ -2,53 +2,15 @@
## Introduction ## Introduction
This module is part of `JDevKit`, an open-source Java Development Kit that provides a set of This module provides a set of utilities to streamline Java codes.
convenient tools to streamline code development and enhance productivity. This module serves as the
useful toolkit for the library, contains a collection of utility classes commonly used in all Java
Application development.
## Prerequisites ## Features
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for - AES encryption and decryption;
better experience. - Base64 encode and decode;
- Boolean calculation;
- Reduce `if...else...` with **lambdas**;
- Hash calculation for strings;
- Convert Java beans to map and map to Java beans;
- Simplified range generator.
## Installation
### If you are using `Maven`
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:
```xml
<dependency>
<groupId>com.onixbyte</groupId>
<artifactId>devkit-utils</artifactId>
<version>${devkit-utils.version}</version>
</dependency>
```
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`.
If you are restricted using the Internet, and have to make `Maven` offline, you could follow the
following steps.
1. Download the `jar` file from any place you can get and transfer the `jar` files to your
work computer.
2. Move the `jar` files to your local `Maven` Repository as the path of
`/path/to/maven_local_repo/com/onixbyte/devkit-utils/`.
### If you are using `Gradle`
Add this module to your project with `Gradle` is much easier than doing so with `Maven`.
Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure
in the build script:
```groovy
implementation 'com.onixbyte:devkit-utils:${devkit-utils.version}'
```
+20 -51
View File
@@ -2,68 +2,37 @@
## Introduction ## Introduction
Module `guid` is a library that provides utilities for generating and working with globally unique Module `guid` serves as a guid creator for other `JDevKit` modules. You can also use this module as a guid creator standards.
identifiers (GUIDs). GUIDs are globally unique across all devices and systems, making them ideal
for various use cases like database record keys, distributed systems, and tracking objects.
The `guid` module offers a reliable and efficient GUID generator, allowing developers to create We have already implemented `SnowflakeGuidCreator`, you can also implement a custom guid creations by implementing `com.onixbyte.guid.GuidCreator`.
unique identifiers with low collision probability. It also provides features to customize the
format of generated GUIDs, making it flexible and suitable for different applications.
With `guid`, developers can easily integrate globally unique identifiers into their projects, ## Example usage
ensuring data integrity, avoiding duplicates, and simplifying the identification of objects across
various systems. The module is designed to be simple to use, highly performant, and compatible with
different programming languages and frameworks.
## Prerequisites ### A UUID creator
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for ```java
better experience. GuidCreator<UUID> uuidCreator = (GuidCreator<UUID>) UUID::randomUUID;
## Installation
### If you are using `Maven`
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:
```xml
<dependency>
<groupId>com.onixbyte</groupId>
<artifactId>devkit-utils</artifactId>
<version>${devkit-utils.version}</version>
</dependency>
``` ```
And run `mvn dependency:get` in your project root folder(i.e., if your `pom.xml` is located at ### A custom guid creator
`/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`.
If you are restricted using the Internet, and have to make `Maven` offline, you could follow the Assume that you need serial guid creator.
following steps.
1. Download the `jar` file from any place you can get and transfer the `jar` files to your work ```java
computer. @Component
2. Move the `jar` files to your local `Maven` Repository as the path of public class CustomGuidCreator implementes GuidCreator<String> {
`/path/to/maven_local_repo/com/onixbyte/devkit-utils/`.
### If you are using `Gradle` public final RedisTemplate<String, Long> serialRedisTemplate;
Add this module to your project with `Gradle` is much easier than doing so with `Maven`. @Autowired
public CustomGuidCreator(RedisTemplate<String, Long> serialRedisTemplate) {
this.serialRedisTemplate = serialRedisTemplate;
}
Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure @Override public String nextId() {
in the build script: return "SOME_PREFIX" + serialRedisTemplate.opsForValue().get("some_serial_key");
}
```groovy }
implementation 'com.onixbyte:guid:${guid.version}'
``` ```
### If you are not using `Maven` or `Gradle`
1. Download the `jar` file from the Internet.
2. Create a folder in your project and name it as a name you like(i.e., for me, I prefer `vendor`).
3. Put the `jar` file to the folder you just created in Step 2.
4. Add this folder to your project `classpath`.
@@ -57,12 +57,15 @@ public class EcKeyLoader implements KeyLoader {
private final KeyFactory keyFactory; private final KeyFactory keyFactory;
private final Base64.Decoder decoder;
/** /**
* Initialise a key loader for EC-based algorithms. * Initialise a key loader for EC-based algorithms.
*/ */
public EcKeyLoader() { public EcKeyLoader() {
try { try {
this.keyFactory = KeyFactory.getInstance("EC"); this.keyFactory = KeyFactory.getInstance("EC");
this.decoder = Base64.getDecoder();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new KeyLoadingException(e); throw new KeyLoadingException(e);
} }
@@ -84,7 +87,7 @@ public class EcKeyLoader implements KeyLoader {
.replaceAll("-----BEGIN (EC )?PRIVATE KEY-----", "") .replaceAll("-----BEGIN (EC )?PRIVATE KEY-----", "")
.replaceAll("-----END (EC )?PRIVATE KEY-----", "") .replaceAll("-----END (EC )?PRIVATE KEY-----", "")
.replaceAll("\n", ""); .replaceAll("\n", "");
var decodedKeyString = Base64.getDecoder().decode(pemKeyText); var decodedKeyString = decoder.decode(pemKeyText);
var keySpec = new PKCS8EncodedKeySpec(decodedKeyString); var keySpec = new PKCS8EncodedKeySpec(decodedKeyString);
var _key = keyFactory.generatePrivate(keySpec); var _key = keyFactory.generatePrivate(keySpec);
@@ -114,7 +117,7 @@ public class EcKeyLoader implements KeyLoader {
.replaceAll("-----BEGIN (EC )?PUBLIC KEY-----", "") .replaceAll("-----BEGIN (EC )?PUBLIC KEY-----", "")
.replaceAll("-----END (EC )?PUBLIC KEY-----", "") .replaceAll("-----END (EC )?PUBLIC KEY-----", "")
.replaceAll("\n", ""); .replaceAll("\n", "");
var keyBytes = Base64.getDecoder().decode(pemKeyText); var keyBytes = decoder.decode(pemKeyText);
var spec = new X509EncodedKeySpec(keyBytes); var spec = new X509EncodedKeySpec(keyBytes);
var key = keyFactory.generatePublic(spec); var key = keyFactory.generatePublic(spec);
if (key instanceof ECPublicKey publicKey) { if (key instanceof ECPublicKey publicKey) {
+5
View File
@@ -0,0 +1,5 @@
# Map Util Unsafe
`map-util-unsafe` provides a set of more convenient utilities for converting Java bean to Map or Map to Java bean, but which are less safe than the `MapUtil` provided in `devkit-utils`.
This `MapUtil` is implemented with Reflect API, which might be removed in later JDKs.
+3
View File
@@ -0,0 +1,3 @@
# Num4j
`num4j` provides some mathematical algorithms and utilities such as chained high-precision mathematical calculator and percentile statistic algorithm.
+19 -79
View File
@@ -1,97 +1,37 @@
# Property Guard # Property Guard
## Introduction `property-guard-spring-boot-starter` is a utility that can help you protect secret values in Spring Boot configurations.
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. ## Example usage
## Prerequisites ### 1. Implementation this module
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. ```kotlin
dependencies {
## Installation implementation(platform("com.onixbyte:devkit-bom:$devKitVersion"))
implementation("com.onixbyte:devkit-utils")
### If you are using `Maven` implementation("com.onixbyte:property-guard-spring-boot-starter")
}
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:
```xml
<dependency>
<groupId>cn.org.codecrafters</groupId>
<artifactId>property-guard-spring-boot-starter</artifactId>
<version>${property-guard-spring-boot-starter.version}</version>
</dependency>
``` ```
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`. ### 2. Generate a secret
If you are restricted using the Internet, and have to make `Maven` offline, you could follow the following steps. Use the following codes to get a random secret.
1. Download the `jar` file from any place you can get and transfer the `jar` files to your work computer.
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/`.
### If you are using `Gradle`
Add this module to your project with `Gradle` is much easier than doing so with `Maven`.
Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure in the build script:
```groovy
implementation 'cn.org.codecrafters:property-guard-spring-boot-starter:${property-guard-spring-boot-starter.version}'
```
### If you are not using `Maven` or `Gradle`
1. Download the `jar` file from the Internet.
2. Create a folder in your project and name it as a name you like(i.e., for me, I prefer `vendor`).
3. Put the `jar` file to the folder you just created in Step 2.
4. Add this folder to your project `classpath`.
## Usage
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.
For example:
```java ```java
import utils.com.onixbyte.devkit.AesUtil; @SpringBootTest
import com.onixbyte.simplejwt.SecretCreator; class SpringBootApplicationTest {
class GenerateRandomKeySample { @Test
public static void main(String[] args) { void contextLoads() {
var secret1 = AesUtil.generateRandomSecret(); System.out.println(AesUtil.generateRandomSecret()); // Output: a 16-char long secret
var secret2 = SecretCreator.createSecret(16, true, true, true);
} }
} }
``` ```
Then, remember this secret and encrypt the configuration properties that are required high security. For example: Or you can write a 16-char long secret by yourself.
```java ### 3. Encrypt your secret properties and place them into your configuration file
import utils.com.onixbyte.devkit.AesUtil;
class EncryptSample { ### 4. Run application with parameter `--pg.key=$your_secret`
public static void main(String[] args) {
var dataNeedEncryption = "Sample Value";
var key = "3856faef0d2d4f33";
var encryptedData = AesUtil.encrypt(dataNeedEncryption, key);
}
}
```
After that, copy the encrypted data to `application.properties` or `application.yml`.
For `yml`:
```yaml
app:
sample-configuration: pe:t4YBfv8M9ZmTzWgTi2gJqg== # "pe:" is the prefix that declare that this property is encrypted.
```
For `properties`:
```properties
app.sample-configuration=pe:t4YBfv8M9ZmTzWgTi2gJqg==
```
## Contact
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).
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).