style(global): Added README for each module
This commit is contained in:
@@ -5,41 +5,40 @@ JDevKit is a Java Development Kit that offers a set of convenient tools for writ
|
|||||||
|
|
||||||
> For more information, please visit the README file of each module.
|
> For more information, please visit the README file of each module.
|
||||||
|
|
||||||
### `devkit-core` <span style="font-size: 14px;">_[Learn more](devkit-core/README.md)_</span>
|
### `devkit-core` <span style="font-size: 14px;">_[Learn more](./devkit-core/README.md)_</span>
|
||||||
The core module for `JDevKit`, by now, this module contains the commonly used classes of the whole `dev-kit`.
|
The core module for `JDevKit`, by now, this module contains the commonly used classes of the whole `dev-kit`.
|
||||||
|
|
||||||
### `guid` <span style="font-size: 14px;">_[Learn more](guid/README.md)_</span>
|
### `guid` <span style="font-size: 14px;">_[Learn more](./guid/README.md)_</span>
|
||||||
A module for generating globally unique IDs. It includes a facade interface and an implementation of GUID generation
|
A module for generating globally unique IDs. It includes a facade interface and an implementation of GUID generation
|
||||||
using the Snowflake algorithm. More globally unique ID generation modes will be added in the future.
|
using the Snowflake algorithm. More globally unique ID generation modes will be added in the future.
|
||||||
|
|
||||||
### `dev-utils` <span style="font-size: 14px;">_[Learn more](dev-utils/README.md)_</span>
|
### `devkit-utils` <span style="font-size: 14px;">_[Learn more](./devkit-utils/README.md)_</span>
|
||||||
A collection of common utility classes to simplify Java development. It includes tools for Base64 encoding/decoding of
|
A collection of common utility classes to simplify Java development. It includes tools for Base64 encoding/decoding of
|
||||||
strings, reducing if-else code blocks using Lambda expressions, converting between maps and arbitrary objects,
|
strings, reducing if-else code blocks using Lambda expressions, converting between maps and arbitrary objects,
|
||||||
high-precision chained mathematical calculations, and string hashing or message digest calculations.
|
high-precision chained mathematical calculations, and string hashing or message digest calculations.
|
||||||
|
|
||||||
### `simple-jwt-facade` <span style="font-size: 14px;">_[Learn more](simple-jwt-facade/README.md)_</span>
|
### `simple-jwt-facade` <span style="font-size: 14px;">_[Learn more](./simple-jwt-facade/README.md)_</span>
|
||||||
A facade for Simple JWT (JSON Web Token) implementations in Java. This module provides a unified interface to work with
|
A facade for Simple JWT (JSON Web Token) implementations in Java. This module provides a unified interface to work with
|
||||||
JWTs regardless of the underlying implementation.
|
JWTs regardless of the underlying implementation.
|
||||||
|
|
||||||
### `simple-jwt-auth0` <span style="font-size: 14px;">_[Learn more](simple-jwt-facade/README.md)_</span>
|
### `simple-jwt-auth0` <span style="font-size: 14px;">_[Learn more](./simple-jwt-facade/README.md)_</span>
|
||||||
A Simple JWT implementation using the com.auth0:java-jwt library.
|
A Simple JWT implementation using the com.auth0:java-jwt library.
|
||||||
|
|
||||||
### `simple-jwt-spring-boot-starter` <span style="font-size: 14px;">_[Learn more](simple-jwt-spring-boot-starter/README.md)_</span>
|
### `simple-jwt-spring-boot-starter` <span style="font-size: 14px;">_[Learn more](./simple-jwt-spring-boot-starter/README.md)_</span>
|
||||||
A Spring Boot auto-configuration wrapper for the simple-jwt module, making it easier to integrate JWT functionality into
|
A Spring Boot auto-configuration wrapper for the simple-jwt module, making it easier to integrate JWT functionality into
|
||||||
Spring Boot applications.
|
Spring Boot applications.
|
||||||
|
|
||||||
### `sms-sender-facade` <span style="font-size: 14px;">_[Learn more](sms-sender-facade/README.md)_</span>
|
### `sms-sender-facade` <span style="font-size: 14px;">_[Learn more](./sms-sender-facade/README.md)_</span>
|
||||||
A facade for sending SMS messages in Java. This module abstracts the interfaces of various SMS service providers.
|
A facade for sending SMS messages in Java. This module abstracts the interfaces of various SMS service providers.
|
||||||
|
|
||||||
### `sms-sender-aliyun` <span style="font-size: 14px;">_[Learn more](sms-sender-aliyun/README.md)_</span>
|
### `sms-sender-aliyun` <span style="font-size: 14px;">_[Learn more](./sms-sender-aliyun/README.md)_</span>
|
||||||
An implementation of SMS Sender using the Alibaba Cloud (Aliyun) service.
|
An implementation of SMS Sender using the Alibaba Cloud (Aliyun) service.
|
||||||
|
|
||||||
### `sms-sender-tencent` <span style="font-size: 14px;">_[Learn more](sms-sender-tencent/README.md)_</span>
|
### `sms-sender-tencent` <span style="font-size: 14px;">_[Learn more](./sms-sender-tencent/README.md)_</span>
|
||||||
An implementation of SMS Sender using the Tencent Cloud service.
|
An implementation of SMS Sender using the Tencent Cloud service.
|
||||||
|
|
||||||
### `sms-sender-spring-boot-starter` <span style="font-size: 14px;">_[Learn more](sms-sender-spring-boot-starter/README.md)_</span>
|
### `sms-sender-spring-boot-starter` <span style="font-size: 14px;">_[Learn more](./sms-sender-spring-boot-starter/README.md)_</span>
|
||||||
A Spring Boot auto-configuration wrapper for the sms-sender module, simplifying the integration of SMS sending
|
A Spring Boot auto-configuration wrapper for the `sms-sender-facade` module, simplifying the integration of SMS sending capabilities into Spring Boot applications.
|
||||||
capabilities into Spring Boot applications.
|
|
||||||
|
|
||||||
## Installation and Usage
|
## Installation and Usage
|
||||||
If you are using `maven`, please paste the following codes to `pom.xml` in your project.
|
If you are using `maven`, please paste the following codes to `pom.xml` in your project.
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# Module `devkit-core`
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
## 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).
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
# Module `devkit-utils`
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
This module is part of `JDevKit`, an open-source Java Development Kit that provides a set of 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
|
||||||
|
|
||||||
|
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience.
|
||||||
|
|
||||||
|
## 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>cn.org.codecrafters</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/cn/org/codecrafters/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 'cn.org.codecrafters:devkit-utils:${devkit-utils.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`.
|
||||||
|
|
||||||
|
### Base64 Encode and Decode
|
||||||
|
|
||||||
|
If you are trying to encode a string to Base64 string or decode a Base64 string to normal string, then you can try this:
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.org.codecrafters.devkit.utils.Base64Util;
|
||||||
|
|
||||||
|
// To reduce sample codes, let me use the simplified main method that is upcoming in Java 21
|
||||||
|
void main(String... args) {
|
||||||
|
var aString = "The string you need to encode.";
|
||||||
|
// Encode it from original text.
|
||||||
|
var encodedString = Base64Util.encode(aString);
|
||||||
|
// Decode the encoded text.
|
||||||
|
var decodedString = Base64Util.decode(encodedString);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Reduce `if...else...`
|
||||||
|
|
||||||
|
I believe those `if...else...` blocks make you headache, and Java imported lambda since Java 8, why not try to replace those `if...else` with lambda expressions?
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.org.codecrafters.devkit.utils.BranchUtil;
|
||||||
|
|
||||||
|
void main(String... args) {
|
||||||
|
var a = 1;
|
||||||
|
var b = 2;
|
||||||
|
|
||||||
|
if (a < b) {
|
||||||
|
// do something...
|
||||||
|
} else {
|
||||||
|
// do something different...
|
||||||
|
}
|
||||||
|
// The codes above is really annoying, have a try of the lambda version.
|
||||||
|
|
||||||
|
BranchUtil.or(a < b) // If multiple logical expressions are combined using the OR operator.
|
||||||
|
.handle(() -> {
|
||||||
|
// do something if a < b.
|
||||||
|
}, () -> {
|
||||||
|
// do something if a > b.
|
||||||
|
});
|
||||||
|
|
||||||
|
BranchUtil.and(a < b) // If multiple logical expressions are combined using the AND operator
|
||||||
|
.handle(() -> {
|
||||||
|
// do something if a < b.
|
||||||
|
}, () -> {
|
||||||
|
// do something if a > b.
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
What if you have to get some data from this branch? Don't worry, those **lambda supports Supplier**. Just add a return in those lambda is fine.
|
||||||
|
|
||||||
|
## CHAINED High-precision Calculation
|
||||||
|
|
||||||
|
If you have faced high-precision mathematical calculation in Java, you might know it is very troubled to do it in **ANY** programming languages. I'm certain you remember that `0.1 + 0.2 != 0.3` right?
|
||||||
|
|
||||||
|
In Java, we usually do high-precision mathematical calculation with `BigDecimal` which is quite tricky when using it.
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.org.codecrafters.devkit.utils.ChainedCalcUtil;
|
||||||
|
|
||||||
|
void main(String... args) {
|
||||||
|
// If you are trying to calculate the expression of 1 * 2 / 2 - 3 + 4
|
||||||
|
ChainedCalcUtil.startWith(1).multiply(2).divide(2).subtract(3).add(4).getInteger(); // you can also get a double by getDouble([int scale]), get a BigDecimal by getDecimal([int scale]) or get a long by getLong().
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If you are facing a divide calculation which has an infinite decimal expansion, then DON'T use `divide(dividend)`, use `divideWithScale(dividend, scale, [beforeOperateScale])`.
|
||||||
|
|
||||||
|
## Hash a `String`
|
||||||
|
|
||||||
|
As is well known, storing plain text passwords in a database is very insecure. Therefore, you need to encrypt the passwords stored in the database, or at least make it difficult for hackers to see the real password at a glance. As a result, hash calculation is often used in database password obfuscation due to its ease of use.
|
||||||
|
|
||||||
|
This `HashUtil` supports these following hash or message digest algorithms:
|
||||||
|
|
||||||
|
- MD2
|
||||||
|
- MD5
|
||||||
|
- SHA-1
|
||||||
|
- SHA-224
|
||||||
|
- SHA-256
|
||||||
|
- SHA-384
|
||||||
|
- SHA-512
|
||||||
|
|
||||||
|
If you want to run a hash calculation to a string, you can use the following codes:
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.org.codecrafters.devkit.utils.HashUtil;
|
||||||
|
|
||||||
|
void main(String... args) {
|
||||||
|
var plaintext = "This is a plain text";
|
||||||
|
var hashedText = HashUtil.md2(plaintext); // if you want to use other algorithm, just change the method name such as md5, sha1, sha224 and so on.
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Besides, if you want to use other character set to do the hash calculation, you can add the specified charset after the text to be calculated.
|
||||||
|
|
||||||
|
```java
|
||||||
|
HashUtil.$algorithm$(String textToCalculate, Charset charset);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Convert a Map to Any Object and Vice Versa
|
||||||
|
|
||||||
|
A Map is a data structure that allows you to store key-value pairs. The keys can be of any type and the values can be of any type. In this case, the key is the user's name and the value is the user's profile.
|
||||||
|
|
||||||
|
Imagine you are developing a website where users can register an account and store their profile. A profile can contain information like their name, age, address, and email address. You store the user's profile in a Map. When the user registers an account, you need to store their profile in a database. A database is a system for storing data. A database can store any type of data, including Maps.
|
||||||
|
|
||||||
|
In order to store the Map in a database, you need to convert the Map to an Object. An Object is a generic data type that can store any type of data.
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.org.codecrafters.devkit.utils.MapUtil;
|
||||||
|
|
||||||
|
class Data {
|
||||||
|
private String name;
|
||||||
|
private Long id;
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
// Setters and getters here.
|
||||||
|
|
||||||
|
// No-args constructor and all-args constructor here.
|
||||||
|
}
|
||||||
|
|
||||||
|
void main(String... args) {
|
||||||
|
// Create a map.
|
||||||
|
var dataMap = new HashMap<String, Object>();
|
||||||
|
dataMap.put("name", "Zihlu Wang");
|
||||||
|
dataMap.put("id", 1L);
|
||||||
|
dataMap.put("age", 18);
|
||||||
|
|
||||||
|
// Convert this map to an instance of class Data
|
||||||
|
var data = MapUtil.mapToObject(dataMap, Data.class); // Then you got an instance of Data("Zihlu Wang", 1L, 18);
|
||||||
|
|
||||||
|
// Then you can convert this object to a map.
|
||||||
|
var anotherDataMap = MapUtil.objectToMap(data)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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).
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
# Module `guid`
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Module `guid` is a library that provides utilities for generating and working with globally unique 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 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, 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
|
||||||
|
|
||||||
|
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience.
|
||||||
|
|
||||||
|
## 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>cn.org.codecrafters</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/cn/org/codecrafters/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 'cn.org.codecrafters:devkit-utils:${devkit-utils.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`.
|
||||||
|
|
||||||
|
## Implement a `GuidCreator`
|
||||||
|
|
||||||
|
You can build your own `GuidCreator` by implements the interface `GuidCreator`. This interface accepts a type parameter which determine the type of the GUID.
|
||||||
|
|
||||||
|
## Generate a `guid` with Snowflake Algorithm
|
||||||
|
|
||||||
|
If you are willing to use Snowflake algorithm to create a guid, just use `SnowflakeGuidCreator` is find. To create a guid in Snowflake algorithm, you'll need an instance of `SnowflakeGuidCreator` first.
|
||||||
|
|
||||||
|
To create an instance of `SnowflakeGuidCreator`, you should use its constructor.
|
||||||
|
|
||||||
|
```java
|
||||||
|
var guidCreator = new SnowflakeGuidCreator(long workerId, long dataCentreId[, long startEpoch]);
|
||||||
|
```
|
||||||
|
|
||||||
|
Then, use the instance to create a guid.
|
||||||
|
|
||||||
|
```java
|
||||||
|
long guid = guidCreator.nextId();
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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).
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
# Module `simple-jwt-authzero`
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
The `simple-jwt-authzero` module is an implementation of module `simple-jwt-facade` which uses the third-party library `com.auth0:java-jwt`. By using this module can help you use JWT to help you in your application.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
> This module has already imported `simple-jwt-facade`, there is no need to import it again.
|
||||||
|
|
||||||
|
### 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>cn.org.codecrafters</groupId>
|
||||||
|
<artifactId>simple-jwt-authzero</artifactId>
|
||||||
|
<version>${simple-jwt-authzero.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/cn/org/codecrafters/simple-jwt-facade/`.
|
||||||
|
|
||||||
|
### 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:simple-jwt-authzero:${simple-jwt-authzero.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`.
|
||||||
|
|
||||||
|
## Use the `AuthzeroTokenResolver`
|
||||||
|
|
||||||
|
We have implemented `TokenResolver` to make sure you can add JWT to your Java application as soon as possible. All you need to do is to create an instance of `cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver` and other operations to JWT could follow our instruction in [`simple-jwt-facade`](../simple-jwt-facade/README.md).
|
||||||
|
|
||||||
|
## 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).
|
||||||
|
|||||||
@@ -0,0 +1,127 @@
|
|||||||
|
# Module `simple-jwt-facade`
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
The `simple-jwt-facade` module is a lightweight and easy-to-use façade for working with JSON Web Tokens (JWT) in Java applications. It provides a simplified interface and utilities to handle the creation, validation, renewal, and processing of JWTs without the need for complex configurations or third-party dependencies.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience.
|
||||||
|
|
||||||
|
## 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>cn.org.codecrafters</groupId>
|
||||||
|
<artifactId>simple-jwt-facade</artifactId>
|
||||||
|
<version>${simple-jwt-facade.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/cn/org/codecrafters/simple-jwt-facade/`.
|
||||||
|
|
||||||
|
### 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:simple-jwt-facade:${simple-jwt-facade.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`.
|
||||||
|
|
||||||
|
## Implement a `TokenResolver`
|
||||||
|
|
||||||
|
If you want to use `TokenResolver` to handle with JWTs, you can choose to use any implementation such as `cn.org.codecrafters:simple-jwt-authzero` or any other `simple-jwt-$implementation_artifact`. But if you do choose to use other implementation, it is not the topic we are going to talk about here. We are here for helping you to implement one by yourself.
|
||||||
|
|
||||||
|
First, you are going to choose a third-party library or your own library which is aims to process JWTs(i.e., `com.auth0:java-jwt` and `io.jsonwebtoken:jjwt`). Then, you need to confirm which class they are going to use for a parsed JWT(i.e., `DecodedJWT` is being used in `com.auth0:java-jwt`). And let me use `com.auth0:java-jwt` for example in this tutor.
|
||||||
|
|
||||||
|
Then, you can add these following codes in your project:
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Auth0TokenResolver implements TokenResolver<DecodedJWT> {
|
||||||
|
// Dont forget to implement methods in interface TokenResolver.
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
After doing all these, an implemented `TokenResolver` is ready to process JWTs.
|
||||||
|
|
||||||
|
**Let's pretend that you already implemented a `TokenResolver` and named as `ImplementedTokenResolver`.**
|
||||||
|
|
||||||
|
You will need an instance of the implementation to process JWTs.
|
||||||
|
|
||||||
|
```java
|
||||||
|
var tokenResolver = new ImplementedTokenResolver(/* arguments you declared */)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create a JSON Web Token with `TokenResolver`
|
||||||
|
|
||||||
|
Then, there are 3 method to create a token:
|
||||||
|
|
||||||
|
```java
|
||||||
|
String createToken(java.time.Duration expireAfter, String audience, String subject);
|
||||||
|
|
||||||
|
String createToken(java.time.Duration expireAfter, String audience, String subject, Map<String, Object> payloads);
|
||||||
|
|
||||||
|
<T extends TokenPayload> String createToken(java.time.Duration expireAfter, String audience, String subject, T payload);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Read or Verify a JSON Web Token
|
||||||
|
|
||||||
|
There are many ways to read a JWT.
|
||||||
|
|
||||||
|
If you want to read as the data class which is designed by the third-party library, you can just use `resolve(String token)`. If you want know what is contained in this JWT, you can use `extract(String, Class<?>)`.
|
||||||
|
|
||||||
|
Meanwhile, the `resolve(token)` method also serves as a token validation mechanism. If any issues arise, such as token expiration, tampering suspicion, or mismatched token signatures, corresponding exceptions will be thrown.
|
||||||
|
|
||||||
|
## Renew a Token that is About to Expire
|
||||||
|
|
||||||
|
When a token is about to expire, its validity period can be extended using this method, allowing users to obtain a new valid token without the need for re-login.
|
||||||
|
|
||||||
|
**Renew a token** means that the content of the pre-defined properties by JWT, such as the audience (aud) representing the recipient of this token, will not be altered, except for the time-related fields.
|
||||||
|
|
||||||
|
```java
|
||||||
|
String renew(String oldToken, Map<String, Object> payload);
|
||||||
|
```
|
||||||
|
|
||||||
|
This method allows you to pass new custom payload into the updated token. The new token will be available for 30 minutes.
|
||||||
|
|
||||||
|
```java
|
||||||
|
String renew(String oldToken, Duration expireAfter, Map<String, Object> payload);
|
||||||
|
```
|
||||||
|
|
||||||
|
This method allows you to pass new custom payload into the updated token. The new token will be available for the specific duration.
|
||||||
|
|
||||||
|
```java
|
||||||
|
<T extends TokenPayload> String renew(String oldToken, Duration expireAfter, T payload);
|
||||||
|
```
|
||||||
|
|
||||||
|
This method allows you to pass new custom payload into the updated token. The new token will be available for 30 minutes.
|
||||||
|
|
||||||
|
```java
|
||||||
|
<T extends TokenPayload> String renew(String oldToken, Duration expireAfter, T payload);
|
||||||
|
```
|
||||||
|
|
||||||
|
This method allows you to pass new custom payload into the updated token. The new token will be available for the specific duration.
|
||||||
|
|
||||||
|
## 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).
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
# Module `simple-jwt-spring-boot-starter`
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Module `simple-jwt-spring-boot-starter` is a lightweight and easy-to-use Spring Boot starter module that provides seamless integration with JSON Web Token (JWT) authentication in Spring Boot applications. With this starter, developers can easily configure and enable JWT-based authentication for their APIs and web applications without the need for complex manual setup. It simplifies the process of generating and validating JWTs, making it effortless to secure endpoints and implement token-based authentication in Spring Boot projects. The module is designed to be flexible, allowing developers to customize various aspects of JWT authentication to suit their specific requirements. Overall, `simple-jwt-spring-boot-starter` is a convenient solution for adding secure and efficient JWT authentication to Spring Boot applications.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience.
|
||||||
|
- You also need an implementation of any `simple-jwt-$implementation` module to make sure you can create an instance of `TokenResolver`.
|
||||||
|
|
||||||
|
### Known Implementation
|
||||||
|
|
||||||
|
> If you implemented one on your own, please contact us to add your artifact to this list.
|
||||||
|
|
||||||
|
- [`cn.org.codecrafters:simple-jwt-authzero`](../simple-jwt-authzero/README.md)
|
||||||
|
|
||||||
|
## 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>${implementation-builder-group-id}</groupId>
|
||||||
|
<artifactId>simple-jwt-${any-implementation}</artifactId>
|
||||||
|
<version>${simple-jwt-${any-implementation}.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.org.codecrafters</groupId>
|
||||||
|
<artifactId>simple-jwt-spring-boot-starter</artifactId>
|
||||||
|
<version>${simple-jwt-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`.
|
||||||
|
|
||||||
|
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/cn/org/codecrafters/simple-jwt-spring-boot-starter/` and `/path/to/maven_local_repo/${implementation-builder-group-seperated-by-system-seperator}/${implementation_artifact_id}`.
|
||||||
|
|
||||||
|
### 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 '${implementation-builder-group-id}:simple-jwt-${any-implementation}:${simple-jwt-${any-implementation}.version}'
|
||||||
|
implementation 'cn.org.codecrafters:simple-jwt-spring-boot-starter:${simple-jwt-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`.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
### Configuration for a customized JWT ID Creator
|
||||||
|
|
||||||
|
We need a `GuidCreator` instance to create JWT ID, though we did implemented a simple `GuidCreator`, but you can still customize it.
|
||||||
|
|
||||||
|
First, please implement the `cn.org.codecrafters.guid.GuidCreator` interface based on your own rules for generating JWT IDs.
|
||||||
|
|
||||||
|
Then, add the instance of your own guid creator to spring container, whose name is `jtiCreator`.
|
||||||
|
|
||||||
|
Here is a simple example which uses class `Random` to create guid.
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Bean
|
||||||
|
public GuidCreator<?> jtiCreator() {
|
||||||
|
return new GuidCreator<Long>() {
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long nextId() {
|
||||||
|
return random.nextLong();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `TokenResolver` Configuration
|
||||||
|
|
||||||
|
| Property Name | Type | Description |
|
||||||
|
| ------------------------------------ | ---------------- | ------------------------------------------------------ |
|
||||||
|
| `code-crafters.simple-jwt.algorithm` | `TokenAlgorithm` | The algorithm used for JWT generation and validation. |
|
||||||
|
| `code-crafters.simple-jwt.issuer` | `String` | The issuer value to be included in the generated JWT. |
|
||||||
|
| `code-crafters.simple-jwt.secret` | `String` | The secret key used for JWT generation and validation. |
|
||||||
|
|
||||||
|
## 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).
|
||||||
-3
@@ -30,9 +30,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
Reference in New Issue
Block a user