docs: Fix all docs from one <p> represents for a paragraph

This commit is contained in:
Zihlu Wang
2023-07-31 22:53:09 +08:00
parent 715ae6db3a
commit 748e474a82
34 changed files with 220 additions and 184 deletions
@@ -22,19 +22,18 @@ import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
/** /**
* Utility class for Base64 encoding and decoding of strings.
* <p> * <p>
* The {@code Base64Util} class provides static methods to encode and decode * The {@code Base64Util} class provides static methods to encode and decode
* strings using Base64 encoding. It utilizes the {@link Base64} class from the * strings using Base64 encoding. It utilizes the {@link Base64} class from the
* Java standard library for performing the encoding and decoding operations. * Java standard library for performing the encoding and decoding operations.
* This utility class offers convenient methods to encode and decode strings * This utility class offers convenient methods to encode and decode strings
* with different character sets. * with different character sets.
* </p> *
* <p> * <p>
* This class is designed as a final class with a private constructor to * This class is designed as a final class with a private constructor to
* prevent instantiation. All methods in this class are static, allowing easy * prevent instantiation. All methods in this class are static, allowing easy
* access to the Base64 encoding and decoding functionality. * access to the Base64 encoding and decoding functionality.
* </p> *
* <p> * <p>
* Example usage: * Example usage:
* <pre> * <pre>
@@ -48,12 +47,12 @@ import java.util.Base64;
* String decoded = Base64Util.decode(encoded); * String decoded = Base64Util.decode(encoded);
* System.out.println("Decoded string: " + decoded); * System.out.println("Decoded string: " + decoded);
* </pre> * </pre>
* </p> *
* <p> * <p>
* <b>Note:</b> This utility class uses the default charset (UTF-8) if no * <b>Note:</b> This utility class uses the default charset (UTF-8) if no
* specific charset is provided. It is recommended to specify the charset * specific charset is provided. It is recommended to specify the charset
* explicitly to ensure consistent encoding and decoding. * explicitly to ensure consistent encoding and decoding.
* </p> *
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
@@ -23,18 +23,19 @@ import java.util.function.BooleanSupplier;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
* Utility class to simplify if...else logic using lambda expressions.
* <p> * <p>
* The BranchUtil class provides static methods to simplify conditional logic * The BranchUtil class provides static methods to simplify conditional logic
* in Java development by leveraging lambda expressions. It offers convenient * in Java development by leveraging lambda expressions. It offers convenient
* methods to replace verbose if...else statements with more concise and * methods to replace verbose if...else statements with more concise and
* expressive functional constructs. * expressive functional constructs.
* </p> *
*
* <p> * <p>
* Developers can use the methods in this utility class to streamline their * Developers can use the methods in this utility class to streamline their
* code, enhance readability, and promote a more functional style of * code, enhance readability, and promote a more functional style of
* programming when dealing with branching logic and conditional statements. * programming when dealing with branching logic and conditional statements.
* </p> *
*
* <p> * <p>
* <b>Example:</b> * <b>Example:</b>
* <pre> * <pre>
@@ -66,12 +67,11 @@ import java.util.function.Supplier;
* // do something * // do something
* }); * });
* </pre> * </pre>
* </p> *
* <p> * <p>
* <b>Note:</b> * <b>Note:</b>
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any * The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any
* number of boolean expressions. * number of boolean expressions.
* </p>
* *
* @param <T> the type of the result to be handled by the methods * @param <T> the type of the result to be handled by the methods
* @author Zihlu Wang * @author Zihlu Wang
@@ -167,15 +167,14 @@ public final class BranchUtil<T> {
* <p> * <p>
* Handles the result of the boolean expressions by executing the * Handles the result of the boolean expressions by executing the
* appropriate handler based on the result. * appropriate handler based on the result.
* </p> *
* <p> * <p>
* If the result is {@code true}, the {@code ifHandler} is executed. If the * If the result is {@code true}, the {@code ifHandler} is executed. If the
* result is {@code false} and an {@code elseHandler} is provided, it is * result is {@code false} and an {@code elseHandler} is provided, it is
* executed. * executed.
* </p> *
* <p> * <p>
* Returns the result of the executed handler. * Returns the result of the executed handler.
* </p>
* *
* @param ifHandler the handler to be executed if the result is * @param ifHandler the handler to be executed if the result is
* {@code true} * {@code true}
@@ -200,10 +199,9 @@ public final class BranchUtil<T> {
* <p> * <p>
* Handles the result of the boolean expressions by executing the provided * Handles the result of the boolean expressions by executing the provided
* handler if the result is {@code true}. * handler if the result is {@code true}.
* </p> *
* <p> * <p>
* Returns the result of the executed handler. * Returns the result of the executed handler.
* </p>
* *
* @param ifHandler the handler to be executed if the result is * @param ifHandler the handler to be executed if the result is
* {@code true} * {@code true}
@@ -217,12 +215,11 @@ public final class BranchUtil<T> {
* <p> * <p>
* Handles the result of the boolean expressions by executing the * Handles the result of the boolean expressions by executing the
* appropriate handler based on the result. * appropriate handler based on the result.
* </p> *
* <p> * <p>
* If the result is {@code true}, the {@code ifHandler} is executed. If the * If the result is {@code true}, the {@code ifHandler} is executed. If the
* result is {@code false} and an {@code elseHandler} is provided, it is * result is {@code false} and an {@code elseHandler} is provided, it is
* executed. * executed.
* </p>
* *
* @param ifHandler the handler to be executed if the result is * @param ifHandler the handler to be executed if the result is
* {@code true} * {@code true}
@@ -26,7 +26,8 @@ import java.util.function.Function;
/** /**
* <p> * <p>
* Utility class for chained high-precision calculations using BigDecimal. * Utility class for chained high-precision calculations using BigDecimal.
* </p> *
*
* <p> * <p>
* The ChainedCalcUtil class provides a convenient way to perform chained * The ChainedCalcUtil class provides a convenient way to perform chained
* high-precision calculations using BigDecimal. It allows users to perform * high-precision calculations using BigDecimal. It allows users to perform
@@ -34,7 +35,8 @@ import java.util.function.Function;
* and division with customizable precision and scale. By using this utility * and division with customizable precision and scale. By using this utility
* class, developers can achieve accurate results and avoid precision loss * class, developers can achieve accurate results and avoid precision loss
* in their calculations. * in their calculations.
* </p> *
*
* <p> * <p>
* <b>Usage:</b> * <b>Usage:</b>
* <pre> * <pre>
@@ -83,14 +85,13 @@ import java.util.function.Function;
* </pre> * </pre>
* The above expressions perform various mathematical calculations using the * The above expressions perform various mathematical calculations using the
* ChainedCalcUtil class. * ChainedCalcUtil class.
* </p> *
* <p> * <p>
* <b>Note:</b> * <b>Note:</b>
* The ChainedCalcUtil class internally uses BigDecimal to handle * The ChainedCalcUtil class internally uses BigDecimal to handle
* high-precision calculations. It is important to note that BigDecimal * high-precision calculations. It is important to note that BigDecimal
* operations can be memory-intensive and may have performance implications * operations can be memory-intensive and may have performance implications
* for extremely large numbers or complex calculations. * for extremely large numbers or complex calculations.
* </p>
* *
* @author sunzsh * @author sunzsh
* @version 1.0.0 * @version 1.0.0
@@ -27,13 +27,13 @@ import java.util.Optional;
/** /**
* <p> * <p>
* Utility class for performing hash operations on strings. * Utility class for performing hash operations on strings.
* </p> *
* <p> * <p>
* The HashUtil class provides convenient methods for calculating various hash * The HashUtil class provides convenient methods for calculating various hash
* functions on strings, including MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384, * functions on strings, including MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384,
* and SHA-512. It allows developers to easily obtain the hash value of a given * and SHA-512. It allows developers to easily obtain the hash value of a given
* string using different algorithms. * string using different algorithms.
* </p> *
* <p> * <p>
* Example usage: * Example usage:
* <pre> * <pre>
@@ -60,14 +60,13 @@ import java.util.Optional;
* </pre> * </pre>
* The above examples demonstrate how to use the HashUtil class to calculate * The above examples demonstrate how to use the HashUtil class to calculate
* hash values for a given string using different algorithms. * hash values for a given string using different algorithms.
* </p> *
* <p> * <p>
* <b>Note:</b> * <b>Note:</b>
* The hash functions provided by the HashUtil class are one-way hash * The hash functions provided by the HashUtil class are one-way hash
* functions, meaning the original data cannot be retrieved from the hash * functions, meaning the original data cannot be retrieved from the hash
* value. These hash functions are commonly used for data integrity checks and * value. These hash functions are commonly used for data integrity checks and
* password storage, but they should not be used for encryption purposes. * password storage, but they should not be used for encryption purposes.
* </p>
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
@@ -27,11 +27,10 @@ import java.util.Map;
* <p> * <p>
* MapUtil is a utility class that provides methods for converting objects to * MapUtil is a utility class that provides methods for converting objects to
* maps and maps to objects. * maps and maps to objects.
* </p> *
* <p> * <p>
* It also provides methods for getting and setting field values using * It also provides methods for getting and setting field values using
* reflection. * reflection.
* </p>
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
@@ -150,6 +149,7 @@ public final class MapUtil {
* field * field
* @throws InvocationTargetException if an error occurs while invoking the * @throws InvocationTargetException if an error occurs while invoking the
* field getter method * field getter method
* @throws NoSuchMethodException if the specified getter is not present
*/ */
public static <T> T getFieldValue(Object obj, String fieldName, Class<T> fieldType) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { public static <T> T getFieldValue(Object obj, String fieldName, Class<T> fieldType) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
var methodName = getMethodName("get", fieldName); var methodName = getMethodName("get", fieldName);
@@ -170,6 +170,7 @@ public final class MapUtil {
* field setter method * field setter method
* @throws IllegalAccessException if an error occurs while accessing the * @throws IllegalAccessException if an error occurs while accessing the
* field * field
* @throws NoSuchMethodException if the specific setter is not present
*/ */
public static void setFieldValue(Object obj, String fieldName, Object fieldValue) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { public static void setFieldValue(Object obj, String fieldName, Object fieldValue) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
var objectClass = obj.getClass(); var objectClass = obj.getClass();
@@ -16,14 +16,11 @@
*/ */
/** /**
* <h3>JDevKit Dev-Utils - Common Utility Classes</h3>
* <p>
* This package is part of JDevKit, an open-source Java Development Kit that * This package is part of JDevKit, an open-source Java Development Kit that
* provides a set of convenient tools to streamline code development and * provides a set of convenient tools to streamline code development and
* enhance productivity. This package serves as the root package for the module * enhance productivity. This package serves as the root package for the module
* dev-utils, which contains a collection of common utility classes commonly * dev-utils, which contains a collection of common utility classes commonly
* used in all Java Application development. * used in all Java Application development.
* </p>
* *
* @author Zihlu Wang * @author Zihlu Wang
* @since 1.0.0 * @since 1.0.0
@@ -24,15 +24,16 @@ package cn.org.codecrafters.devkit.core.exceptions;
* that represents a situation where a particular method or functionality is * that represents a situation where a particular method or functionality is
* not implemented or is currently unavailable in the codebase. It extends the * not implemented or is currently unavailable in the codebase. It extends the
* standard {@code RuntimeException} class, making it an unchecked exception. * standard {@code RuntimeException} class, making it an unchecked exception.
* </p> *
* <p> * <p>
* This exception is typically thrown when developers need to indicate that a * This exception is typically thrown when developers need to indicate that a
* specific part of the code is incomplete or requires further implementation. * specific part of the code is incomplete or requires further implementation.
* It serves as a placeholder to highlight unfinished sections of the * It serves as a placeholder to highlight unfinished sections of the
* application during development and testing phases. * application during development and testing phases.
* </p> *
* <p> * <p>
* Usage Example: * Usage Example:
*
* <pre> * <pre>
* public void someMethod() { * public void someMethod() {
* // Some code... * // Some code...
@@ -41,13 +42,8 @@ package cn.org.codecrafters.devkit.core.exceptions;
* """); * """);
* } * }
* </pre> * </pre>
* </p> *
* <p> * <b>Contact</b>
* For more information and the latest version of JDevKit, please visit our
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.
* </p>
* <p>
* <h4>Contact</h4>
* <ul> * <ul>
* <li> * <li>
* <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new" * <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new"
@@ -57,7 +53,6 @@ package cn.org.codecrafters.devkit.core.exceptions;
* <a href="https://discord.gg/">Discord Community</a> * <a href="https://discord.gg/">Discord Community</a>
* </li> * </li>
* </ul> * </ul>
* </p>
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
@@ -16,22 +16,21 @@
*/ */
/** /**
* <p>
* This package is a part of JDevKit, an open-source Java Development Kit that * This package is a part of JDevKit, an open-source Java Development Kit that
* provides a set of convenient tools to streamline code development and * provides a set of convenient tools to streamline code development and
* enhance productivity. This package serves as the core package containing * enhance productivity. This package serves as the core package containing
* common exceptions that are used throughout the entireJDevKit project. * common exceptions that are used throughout the entireJDevKit project.
* </p> *
* <p> * <p>
* JDevKit is designed to be modular, and other specific feature modules within * JDevKit is designed to be modular, and other specific feature modules within
* the library may rely on these exceptions from the core package. * the library may rely on these exceptions from the core package.
* </p> *
* <p> * <p>
* For more information and the latest version of JDevKit, please visit our * For more information and the latest version of JDevKit, please visit our
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>. * website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.
* </p> *
* <p> * <p>
* <h4>Contact</h4> * <b>Contact</b>
* <ul> * <ul>
* <li> * <li>
* <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new" * <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new"
@@ -41,7 +40,6 @@
* <a href="https://discord.gg/">Discord Community</a> * <a href="https://discord.gg/">Discord Community</a>
* </li> * </li>
* </ul> * </ul>
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -16,19 +16,17 @@
*/ */
/** /**
* <h3>JDevKit - Java Development Kit</h3>
* <p>
* This package is the main part of JDevKit, an open-source Java class library * This package is the main part of JDevKit, an open-source Java class library
* that provides a set of convenient tools to streamline code development and * that provides a set of convenient tools to streamline code development and
* enhance productivity. This package serves as the root package for several * enhance productivity. This package serves as the root package for several
* modules, containing devkit-core, guid and dev-utils module. * modules, containing devkit-core, guid and dev-utils module.
* </p> *
* <p> * <p>
* For more information and the latest version of JDevKit, please visit our * For more information and the latest version of JDevKit, please visit our
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>. * website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.
* </p> *
* <p> * <p>
* <h4>Contact</h4> * <b>Contact</b>
* <ul> * <ul>
* <li> * <li>
* <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new" * <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new"
@@ -38,7 +36,6 @@
* <a href="https://discord.gg/">Discord Community</a> * <a href="https://discord.gg/">Discord Community</a>
* </li> * </li>
* </ul> * </ul>
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -21,10 +21,10 @@ package cn.org.codecrafters.guid;
* <p> * <p>
* The {@code GuidCreator} is a generic interface for generating globally unique * The {@code GuidCreator} is a generic interface for generating globally unique
* identifiers (GUIDs) of a specific type. * identifiers (GUIDs) of a specific type.
* </p> *
* <p> * <p>
* The type of ID is determined by the class implementing this interface. * The type of ID is determined by the class implementing this interface.
* </p> *
* *
* @param <IdType> this represents the type of the Global Unique Identifier * @param <IdType> this represents the type of the Global Unique Identifier
* @author Zihlu Wang * @author Zihlu Wang
@@ -25,7 +25,7 @@ import java.time.ZoneOffset;
/** /**
* <p> * <p>
* SnowflakeGuidCreator - GUID generator based on the Snowflake algorithm. * SnowflakeGuidCreator - GUID generator based on the Snowflake algorithm.
* </p> *
* <p> * <p>
* The SnowflakeGuidCreator generates unique identifiers using the Snowflake * The SnowflakeGuidCreator generates unique identifiers using the Snowflake
* algorithm, which combines a timestamp, worker ID, and data center ID to * algorithm, which combines a timestamp, worker ID, and data center ID to
@@ -38,7 +38,6 @@ import java.time.ZoneOffset;
* <li>5 bits for worker ID</li> * <li>5 bits for worker ID</li>
* <li>12 bits for sequence number (per millisecond)</li> * <li>12 bits for sequence number (per millisecond)</li>
* </ul> * </ul>
* </p>
* *
* <p> * <p>
* When initializing the SnowflakeGuidCreator, you must provide the worker ID * When initializing the SnowflakeGuidCreator, you must provide the worker ID
@@ -47,7 +46,6 @@ import java.time.ZoneOffset;
* increments for IDs generated within the same millisecond. If the system * increments for IDs generated within the same millisecond. If the system
* clock moves backward, an exception is thrown to prevent generating IDs with * clock moves backward, an exception is thrown to prevent generating IDs with
* repeated timestamps. * repeated timestamps.
* </p>
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
@@ -21,24 +21,24 @@ package cn.org.codecrafters.guid.exceptions;
* <p> * <p>
* The TimingException class represents an exception that is thrown when there * The TimingException class represents an exception that is thrown when there
* is an error related to time sequence. * is an error related to time sequence.
* </p> *
* <p> * <p>
* This class extends the RuntimeException class, which means that instances of * This class extends the RuntimeException class, which means that instances of
* TimingException do not need to be declared in a method or constructor's * TimingException do not need to be declared in a method or constructor's
* throws clause. * throws clause.
* </p> *
* <p> * <p>
* Instances of TimingException can be created with or without a message and a * Instances of TimingException can be created with or without a message and a
* cause. The message provides a description of the exception, while the cause * cause. The message provides a description of the exception, while the cause
* represents the underlying cause of the exception and provides additional * represents the underlying cause of the exception and provides additional
* information about the error. * information about the error.
* </p> *
* <p> * <p>
* TimingException is typically used to handle exceptions related to timing, * TimingException is typically used to handle exceptions related to timing,
* such as timeouts or synchronization issues. It is a subclass of * such as timeouts or synchronization issues. It is a subclass of
* RuntimeException, which means it is an unchecked exception and does not need * RuntimeException, which means it is an unchecked exception and does not need
* to be caught or declared. * to be caught or declared.
* </p> *
* *
* @author Zihlu Wang * @author Zihlu Wang
* @since 1.0.0 * @since 1.0.0
@@ -20,14 +20,12 @@
* This package contains the custom exception classes related to GUID * This package contains the custom exception classes related to GUID
* generation. These exceptions are thrown when there are issues or errors * generation. These exceptions are thrown when there are issues or errors
* during the generation or processing of global unique identifiers (GUIDs). * during the generation or processing of global unique identifiers (GUIDs).
* </p>
* *
* <p> * <p>
* The main exception class in this package is {@link * The main exception class in this package is {@link
* cn.org.codecrafters.guid.exceptions.TimingException}, which is a runtime * cn.org.codecrafters.guid.exceptions.TimingException}, which is a runtime
* exception and serves as the base exception for all other custom exceptions * exception and serves as the base exception for all other custom exceptions
* related to GUID generation. * related to GUID generation.
* </p>
* *
* <p> * <p>
* Custom exceptions in this package provide specific information about the * Custom exceptions in this package provide specific information about the
@@ -36,13 +34,11 @@
* GUIDs. They are designed to enhance the robustness and reliability of the * GUIDs. They are designed to enhance the robustness and reliability of the
* GUID generation process by providing clear and meaningful error messages to * GUID generation process by providing clear and meaningful error messages to
* the developers. * the developers.
* </p>
* *
* <p> * <p>
* Developers using the GUID generation module should be aware of the possible * Developers using the GUID generation module should be aware of the possible
* exceptions that can be thrown and handle them appropriately to ensure smooth * exceptions that can be thrown and handle them appropriately to ensure smooth
* operation and error handling in their applications. * operation and error handling in their applications.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -19,12 +19,12 @@
* <p> * <p>
* The package provides a set of tools for generating globally unique * The package provides a set of tools for generating globally unique
* identifiers (GUIDs). * identifiers (GUIDs).
* </p> *
* <p> * <p>
* The goal of this library is to provide an efficient, reliable way to * The goal of this library is to provide an efficient, reliable way to
* generate globally unique identifiers without requiring any specific * generate globally unique identifiers without requiring any specific
* environment or configuration. * environment or configuration.
* </p> *
* <p> * <p>
* Key features include: * Key features include:
* <ul> * <ul>
@@ -32,7 +32,6 @@
* <li>High performance and quick response</li> * <li>High performance and quick response</li>
* <li>Easy to integrate</li> * <li>Easy to integrate</li>
* </ul> * </ul>
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -43,13 +43,13 @@ import java.util.*;
* com.auth0:java-jwt} library to handle JSON Web Token (JWT) resolution. This * com.auth0:java-jwt} library to handle JSON Web Token (JWT) resolution. This
* resolver provides functionality to create, extract, verify, and renew JWT * resolver provides functionality to create, extract, verify, and renew JWT
* tokens using various algorithms and custom payload data. * tokens using various algorithms and custom payload data.
* </p> *
* <p> * <p>
* <b>Dependencies:</b> * <b>Dependencies:</b>
* This implementation relies on the {@code com.auth0:java-jwt} library. Please * This implementation relies on the {@code com.auth0:java-jwt} library. Please
* ensure you have added this library as a dependency to your project before * ensure you have added this library as a dependency to your project before
* using this resolver. * using this resolver.
* </p> *
* <p> * <p>
* <b>Usage:</b> * <b>Usage:</b>
* To use the {@code AuthzeroTokenResolver}, first, create an instance of this * To use the {@code AuthzeroTokenResolver}, first, create an instance of this
@@ -61,7 +61,7 @@ import java.util.*;
* "Token Issuer", * "Token Issuer",
* "Token Secret"); * "Token Secret");
* }</pre> * }</pre>
* </p> *
* <p> * <p>
* Then, you can utilize the various methods provided by this resolver to * Then, you can utilize the various methods provided by this resolver to
* handle JWT tokens: * handle JWT tokens:
@@ -82,14 +82,13 @@ import java.util.*;
* String renewedToken = * String renewedToken =
* tokenResolver.renew(token, Duration.ofMinutes(30), customPayloads); * tokenResolver.renew(token, Duration.ofMinutes(30), customPayloads);
* }</pre> * }</pre>
* </p> *
* <p> * <p>
* <b>Note:</b> * <b>Note:</b>
* It is essential to configure the appropriate algorithms, secret, and issuer * It is essential to configure the appropriate algorithms, secret, and issuer
* according to your specific use case when using this resolver. * according to your specific use case when using this resolver.
* Additionally, ensure that the {@code com.auth0:java-jwt} library is * Additionally, ensure that the {@code com.auth0:java-jwt} library is
* correctly configured in your project's dependencies. * correctly configured in your project's dependencies.
* </p>
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
@@ -264,10 +263,9 @@ public class AuthzeroTokenResolver implements TokenResolver<DecodedJWT> {
/** /**
* <p> * <p>
* Finish creating a token. * Finish creating a token.
* </p> *
* <p> * <p>
* This is the final step of create a token, to sign this token. * This is the final step of create a token, to sign this token.
* </p>
* *
* @param builder the builder to build this JWT * @param builder the builder to build this JWT
* @return the generated token as a {@code String} * @return the generated token as a {@code String}
@@ -64,12 +64,11 @@ public final class AuthzeroTokenResolverConfig implements TokenResolverConfig<Fu
/** /**
* <p> * <p>
* Constructs a new instance of AuthzeroTokenResolverConfig. * Constructs a new instance of AuthzeroTokenResolverConfig.
* </p> *
* <p> * <p>
* The constructor is set as private to enforce the singleton pattern for * The constructor is set as private to enforce the singleton pattern for
* this configuration class. Instances of AuthzeroTokenResolverConfig * this configuration class. Instances of AuthzeroTokenResolverConfig
* should be obtained through the {@link #getInstance()} method. * should be obtained through the {@link #getInstance()} method.
* </p>
*/ */
private AuthzeroTokenResolverConfig() { private AuthzeroTokenResolverConfig() {
} }
@@ -77,20 +76,19 @@ public final class AuthzeroTokenResolverConfig implements TokenResolverConfig<Fu
/** /**
* <p> * <p>
* The singleton instance of AuthzeroTokenResolverConfig. * The singleton instance of AuthzeroTokenResolverConfig.
* </p> *
* <p> * <p>
* This instance is used to ensure that only one instance of * This instance is used to ensure that only one instance of
* AuthzeroTokenResolverConfig is created and shared throughout the * AuthzeroTokenResolverConfig is created and shared throughout the
* application. The singleton pattern is implemented to provide centralized * application. The singleton pattern is implemented to provide centralized
* configuration and avoid redundant object creation. * configuration and avoid redundant object creation.
* </p>
*/ */
private static AuthzeroTokenResolverConfig instance; private static AuthzeroTokenResolverConfig instance;
/** /**
* <p> * <p>
* The supported algorithms and their corresponding algorithm functions. * The supported algorithms and their corresponding algorithm functions.
* </p> *
* <p> * <p>
* This map stores the supported algorithms as keys and their corresponding * This map stores the supported algorithms as keys and their corresponding
* algorithm functions as values. The algorithm functions represent the * algorithm functions as values. The algorithm functions represent the
@@ -126,14 +124,13 @@ public final class AuthzeroTokenResolverConfig implements TokenResolverConfig<Fu
* <p> * <p>
* Gets the algorithm function corresponding to the specified * Gets the algorithm function corresponding to the specified
* TokenAlgorithm. * TokenAlgorithm.
* </p> *
* <p> * <p>
* This method returns the algorithm function associated with the given * This method returns the algorithm function associated with the given
* TokenAlgorithm. The provided TokenAlgorithm represents the specific * TokenAlgorithm. The provided TokenAlgorithm represents the specific
* algorithm for which the corresponding algorithm function is required. * algorithm for which the corresponding algorithm function is required.
* The returned AlgorithmFunction represents the function implementation * The returned AlgorithmFunction represents the function implementation
* that can be used by the TokenResolver to handle the specific algorithm. * that can be used by the TokenResolver to handle the specific algorithm.
* </p>
* *
* @param algorithm the TokenAlgorithm for which the algorithm function is * @param algorithm the TokenAlgorithm for which the algorithm function is
* required * required
@@ -21,13 +21,11 @@
* configuration classes related to the {@link * configuration classes related to the {@link
* cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver}
* implementation. * implementation.
* </p>
* *
* <p> * <p>
* The classes in this package provide configuration options and settings for * The classes in this package provide configuration options and settings for
* the {@link cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver}, * the {@link cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver},
* which is used for resolving JSON Web Tokens (JWT) using the Auth0 library. * which is used for resolving JSON Web Tokens (JWT) using the Auth0 library.
* </p>
* *
* <p> * <p>
* The {@link * The {@link
@@ -39,7 +37,6 @@
* JWT algorithms. It enables developers to specify and customize the * JWT algorithms. It enables developers to specify and customize the
* algorithm functions according to the chosen JWT algorithm and the library * algorithm functions according to the chosen JWT algorithm and the library
* being used. * being used.
* </p>
* *
* <p> * <p>
* The configuration options in this package help developers integrate and * The configuration options in this package help developers integrate and
@@ -48,7 +45,6 @@
* into their Spring Boot applications. Developers can fine-tune the token * into their Spring Boot applications. Developers can fine-tune the token
* resolution process and customize algorithm handling to align with their * resolution process and customize algorithm handling to align with their
* specific requirements and desired level of security. * specific requirements and desired level of security.
* </p>
* *
* <p> * <p>
* It is recommended to explore the classes in this package to understand how * It is recommended to explore the classes in this package to understand how
@@ -56,7 +52,6 @@
* cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} effectively * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} effectively
* in the Spring Boot environment to handle JWT authentication and * in the Spring Boot environment to handle JWT authentication and
* authorization securely and efficiently. * authorization securely and efficiently.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -16,20 +16,16 @@
*/ */
/** /**
* <h3>cn.org.codecrafters.simplejwt.authzero</h3>
*
* <p>
* This package contains classes related to the integration of the {@code * This package contains classes related to the integration of the {@code
* com.auth0:java-jwt} library in the Simple JWT project. {@code * com.auth0:java-jwt} library in the Simple JWT project. {@code
* com.auth0:java-jwt} is a powerful and widely-used Identity as a Service * com.auth0:java-jwt} is a powerful and widely-used Identity as a Service
* (IDaaS) platform that provides secure authentication and authorization * (IDaaS) platform that provides secure authentication and authorization
* solutions for web and mobile applications. The classes in this package * solutions for web and mobile applications. The classes in this package
* provide the necessary functionality to handle JSON Web Tokens (JWTs) using * provide the necessary functionality to handle JSON Web Tokens (JWTs) using
* the {@code com.auth0:java-jwt} library. * the {@code com.auth0:java-jwt} library.
* </p>
* *
* <p> * <p>
* The main class in this package is the {@link * The main class in this package is the {@link
* cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver}, which * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver}, which
* implements the {@link cn.org.codecrafters.simplejwt.TokenResolver} interface * implements the {@link cn.org.codecrafters.simplejwt.TokenResolver} interface
* and uses the {@code com.auth0:java-jwt} library to handle JWT operations. It * and uses the {@code com.auth0:java-jwt} library to handle JWT operations. It
@@ -37,7 +33,6 @@
* {@code com.auth0:java-jwt} library. Developers can use this class as the * {@code com.auth0:java-jwt} library. Developers can use this class as the
* main token resolver in the Simple JWT project when integrating {@code * main token resolver in the Simple JWT project when integrating {@code
* com.auth0:java-jwt} as the JWT management library. * com.auth0:java-jwt} as the JWT management library.
* </p>
* *
* <p> * <p>
* The {@code AuthzeroTokenResolver} relies on the {@code com.auth0:java-jwt} * The {@code AuthzeroTokenResolver} relies on the {@code com.auth0:java-jwt}
@@ -45,7 +40,6 @@
* validation, and extraction. It utilizes the {@code com.auth0:java-jwt} * validation, and extraction. It utilizes the {@code com.auth0:java-jwt}
* {@code Algorithm} class to define and use different algorithms for JWT * {@code Algorithm} class to define and use different algorithms for JWT
* signing and verification. * signing and verification.
* </p>
* *
* <p> * <p>
* To use the {@code AuthzeroTokenResolver}, developers must provide the * To use the {@code AuthzeroTokenResolver}, developers must provide the
@@ -54,14 +48,12 @@
* issuer name, and the secret key used for token signing and validation. The * issuer name, and the secret key used for token signing and validation. The
* {@code AuthzeroTokenResolverConfig} class provides a convenient way to * {@code AuthzeroTokenResolverConfig} class provides a convenient way to
* configure these dependencies. * configure these dependencies.
* </p>
* *
* <p> * <p>
* Developers using the {@code com.auth0:java-jwt} integration should be * Developers using the {@code com.auth0:java-jwt} integration should be
* familiar with the concepts and usage of the {@code com.auth0:java-jwt} * familiar with the concepts and usage of the {@code com.auth0:java-jwt}
* library and follow the official {@code com.auth0:java-jwt} documentation for * library and follow the official {@code com.auth0:java-jwt} documentation for
* best practices and security considerations. * best practices and security considerations.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -22,19 +22,19 @@ import java.util.Map;
/** /**
* <p> * <p>
* TokenPayload - Interface for JWT Payload Data Classes. * TokenPayload - Interface for JWT Payload Data Classes.
* </p> *
* <p> * <p>
* The {@code TokenPayload} interface is used to mark a data class as suitable * The {@code TokenPayload} interface is used to mark a data class as suitable
* for being used as the payload in a JSON Web Token (JWT). Any class * for being used as the payload in a JSON Web Token (JWT). Any class
* implementing this interface can be used to represent the payload data that * implementing this interface can be used to represent the payload data that
* will be included in a JWT. * will be included in a JWT.
* </p> *
* <p> * <p>
* Implementing this interface indicates that the data class contains * Implementing this interface indicates that the data class contains
* information that needs to be securely transmitted and verified as part of a * information that needs to be securely transmitted and verified as part of a
* JWT. The payload typically contains claims or attributes that provide * JWT. The payload typically contains claims or attributes that provide
* additional information about the JWT subject or context. * additional information about the JWT subject or context.
* </p> *
* <p> * <p>
* <b>Usage:</b> * <b>Usage:</b>
* To use a class as a JWT payload, simply implement the {@code TokenPayload} * To use a class as a JWT payload, simply implement the {@code TokenPayload}
@@ -44,7 +44,6 @@ import java.util.Map;
* // Class implementation with payload data... * // Class implementation with payload data...
* } * }
* </pre> * </pre>
* </p>
* *
* @since 1.0.0 * @since 1.0.0
* @version 1.0.0 * @version 1.0.0
@@ -26,26 +26,25 @@ import java.util.Map;
* extracting, and renewing tokens, particularly JSON Web Tokens (JWTs). It * extracting, and renewing tokens, particularly JSON Web Tokens (JWTs). It
* provides a set of methods to generate tokens with various payload * provides a set of methods to generate tokens with various payload
* configurations, extract payloads from tokens, and renew expired tokens. * configurations, extract payloads from tokens, and renew expired tokens.
* </p> *
* <p> * <p>
* <b>Token Creation:</b> * <b>Token Creation:</b>
* The interface provides overloaded methods for creating tokens with different * The interface provides overloaded methods for creating tokens with different
* payload configurations, including expiration time, audience, subject, and * payload configurations, including expiration time, audience, subject, and
* custom payload data. Clients can choose the appropriate method based on * custom payload data. Clients can choose the appropriate method based on
* their specific token requirements. * their specific token requirements.
* </p> *
* <p> * <p>
* <b>Token Extraction:</b> * <b>Token Extraction:</b>
* The interface includes methods to extract payload information from a given * The interface includes methods to extract payload information from a given
* token. Clients can use these methods to obtain the payload data encoded in * token. Clients can use these methods to obtain the payload data encoded in
* the token. * the token.
* </p> *
* <p> * <p>
* <b>Token Renewal:</b> * <b>Token Renewal:</b>
* The interface also offers methods for token renewal. Clients can renew an * The interface also offers methods for token renewal. Clients can renew an
* expired token by providing a new expiration time, audience, subject, and * expired token by providing a new expiration time, audience, subject, and
* optional custom payload data. * optional custom payload data.
* </p>
* *
* @param <ResolvedTokenType> the type of the result obtained by the * @param <ResolvedTokenType> the type of the result obtained by the
* third-party library when parsing JWTs * third-party library when parsing JWTs
@@ -23,14 +23,12 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* <h3>ExcludeFromPayload</h3>
* <h4>Annotation to Exclude Property from JWT Payload.</h4>
* <p> * <p>
* This annotation is used to mark a property of a data class that should be * This annotation is used to mark a property of a data class that should be
* excluded from being automatically injected into the JSON Web Token (JWT) * excluded from being automatically injected into the JSON Web Token (JWT)
* payload during token generation. When a property is annotated with this * payload during token generation. When a property is annotated with this
* annotation, it will not be included as part of the JWT payload. * annotation, it will not be included as part of the JWT payload.
* </p> *
* *
* <p><b>Usage:</b> * <p><b>Usage:</b>
* To exclude a property from the JWT payload, annotate the property with * To exclude a property from the JWT payload, annotate the property with
@@ -47,17 +45,15 @@ import java.lang.annotation.Target;
* // Getters and setters... * // Getters and setters...
* } * }
* }</pre> * }</pre>
* </p>
* *
* <p><b>Note:</b> * <p><b>Note:</b>
* This annotation should be used only on properties that are not intended to * This annotation should be used only on properties that are not intended to
* be included in the JWT payload due to their sensitive nature or for other * be included in the JWT payload due to their sensitive nature or for other
* reasons. It is important to carefully choose which properties are excluded * reasons. It is important to carefully choose which properties are excluded
* from the payload to ensure the JWT remains secure and efficient. * from the payload to ensure the JWT remains secure and efficient.
* </p>
* *
* @since 1.0.0
* @version 1.0.0 * @version 1.0.0
* @since 1.0.0
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@@ -23,13 +23,13 @@ import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm;
* <p> * <p>
* The TokenResolverConfig interface provides a mechanism to configure a * The TokenResolverConfig interface provides a mechanism to configure a
* TokenResolver with algorithm functions. * TokenResolver with algorithm functions.
* </p> *
* <p> * <p>
* This generic interface is used to define the configuration details for a * This generic interface is used to define the configuration details for a
* TokenResolver that utilizes algorithm functions. The interface allows for * TokenResolver that utilizes algorithm functions. The interface allows for
* specifying algorithm functions corresponding to different TokenAlgorithm * specifying algorithm functions corresponding to different TokenAlgorithm
* instances supported by the TokenResolver implementation. * instances supported by the TokenResolver implementation.
* </p> *
* *
* @param <AlgorithmFunction> the type representing algorithm functions used by * @param <AlgorithmFunction> the type representing algorithm functions used by
* the TokenResolver * the TokenResolver
@@ -21,14 +21,12 @@
* the Simple JWT library. They are used to customize the behavior of the * the Simple JWT library. They are used to customize the behavior of the
* library and allow developers to fine-tune various aspects of JWT generation * library and allow developers to fine-tune various aspects of JWT generation
* and validation. * and validation.
* </p>
* *
* <p> * <p>
* Other configuration classes may be added in the future to support additional * Other configuration classes may be added in the future to support additional
* customization options and features. Developers using the Simple JWT library * customization options and features. Developers using the Simple JWT library
* should be familiar with the available configuration options to ensure proper * should be familiar with the available configuration options to ensure proper
* integration and usage of the library. * integration and usage of the library.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -20,47 +20,77 @@ package cn.org.codecrafters.simplejwt.constants;
import java.util.List; import java.util.List;
/** /**
* <p> * The {@code PredefinedKeys} class contains constants for standard JSON Web Token (JWT) claims. These constants
* This class contains predefined keys that are commonly used in JSON Web * represent the names of the standard claims that can be included in a JWT payload. Developers can use these constants
* Tokens (JWT). * when working with JWTs to ensure consistent naming of the claims.
* </p>
* *
* <p> * <p>The class provides the following standard JWT claim constants:
* JWTs consist of a set of claims represented as key-value pairs. These claims * <ul>
* store various pieces of information, such as the subject, issuer, expiration * <li>{@link #ISSUER}: Represents the "iss" (Issuer) claim.</li>
* time, and more. To ensure consistency and avoid spelling mistakes, this * <li>{@link #SUBJECT}: Represents the "sub" (Subject) claim.</li>
* class provides constants for the standard keys used in JWT claims. * <li>{@link #AUDIENCE}: Represents the "aud" (Audience) claim.</li>
* </p> * <li>{@link #EXPIRATION_TIME}: Represents the "exp" (Expiration Time) claim.</li>
* <li>{@link #NOT_BEFORE}: Represents the "nbf" (Not Before) claim.</li>
* <li>{@link #ISSUED_AT}: Represents the "iat" (Issued At) claim.</li>
* <li>{@link #JWT_ID}: Represents the "jti" (JWT ID) claim.</li>
* </ul>
* *
* <p> * <p>The class also contains a list of all the standard claim constants, accessible via the {@link #KEYS} field. This
* Developers using this JWT library can use these predefined keys to set and * list can be useful for iterating through all the standard claims or checking for the presence of specific claims.
* retrieve claims in a safer and more readable way. By using constants instead *
* of plain strings, it helps prevent typos and makes the code more * <p>Note: This class is final and cannot be instantiated. It only serves as a utility class to hold the standard JWT
* maintainable. * claim constants.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public final class PredefinedKeys { public final class PredefinedKeys {
// Constants for standard JWT claims /**
* Constant representing the "iss" (Issuer) claim in a JWT payload.
*/
public static final String ISSUER = "iss"; public static final String ISSUER = "iss";
/**
* Constant representing the "sub" (Subject) claim in a JWT payload.
*/
public static final String SUBJECT = "sub"; public static final String SUBJECT = "sub";
/**
* Constant representing the "aud" (Audience) claim in a JWT payload.
*/
public static final String AUDIENCE = "aud"; public static final String AUDIENCE = "aud";
/**
* Constant representing the "exp" (Expiration Time) claim in a JWT payload.
*/
public static final String EXPIRATION_TIME = "exp"; public static final String EXPIRATION_TIME = "exp";
/**
* Constant representing the "nbf" (Not Before) claim in a JWT payload.
*/
public static final String NOT_BEFORE = "nbf"; public static final String NOT_BEFORE = "nbf";
/**
* Constant representing the "iat" (Issued At) claim in a JWT payload.
*/
public static final String ISSUED_AT = "iat"; public static final String ISSUED_AT = "iat";
/**
* Constant representing the "jti" (JWT ID) claim in a JWT payload.
*/
public static final String JWT_ID = "jti"; public static final String JWT_ID = "jti";
/**
* List containing all the standard JWT claim constants.
*/
public static final List<String> KEYS = List.of(ISSUER, SUBJECT, AUDIENCE, EXPIRATION_TIME, NOT_BEFORE, ISSUED_AT, JWT_ID);
/**
* Private constructor to prevent instantiation of the {@code PredefinedKeys} class.
* This class is intended to be used as a utility class with only static constants and methods.
*/
private PredefinedKeys() { private PredefinedKeys() {
// Private constructor to prevent instantiation // Private constructor to prevent instantiation
} }
public static final List<String> KEYS = List.of(ISSUER, SUBJECT, AUDIENCE, EXPIRATION_TIME, NOT_BEFORE, ISSUED_AT, JWT_ID);
} }
@@ -20,13 +20,12 @@ package cn.org.codecrafters.simplejwt.constants;
import lombok.Getter; import lombok.Getter;
/** /**
* <p>
* The {@code TokenAlgorithm} enum class defines the algorithms that can be * The {@code TokenAlgorithm} enum class defines the algorithms that can be
* used for signing and verifying JSON Web Tokens (JWT). JWT allows various * used for signing and verifying JSON Web Tokens (JWT). JWT allows various
* cryptographic algorithms to be used for secure token generation and * cryptographic algorithms to be used for secure token generation and
* validation. This enum provides a list of supported algorithms to ensure * validation. This enum provides a list of supported algorithms to ensure
* consistent usage and avoid potential security issues. * consistent usage and avoid potential security issues.
* </p> *
* <p><b>Supported Algorithms:</b> * <p><b>Supported Algorithms:</b>
* This enum includes the following supported algorithms: * This enum includes the following supported algorithms:
* <ul> * <ul>
@@ -40,21 +39,56 @@ import lombok.Getter;
* <li>{@link TokenAlgorithm#ES384}: ECDSA with SHA-384</li> * <li>{@link TokenAlgorithm#ES384}: ECDSA with SHA-384</li>
* <li>{@link TokenAlgorithm#ES512}: ECDSA with SHA-512</li> * <li>{@link TokenAlgorithm#ES512}: ECDSA with SHA-512</li>
* </ul> * </ul>
* </p>
* *
* @version 1.0.0 * @version 1.0.0
* @since 1.0.0 * @since 1.0.0
*/ */
@Getter @Getter
public enum TokenAlgorithm { public enum TokenAlgorithm {
/**
* HMAC using SHA-256
*/
HS256, HS256,
/**
* HMAC using SHA-384
*/
HS384, HS384,
/**
* HMAC using SHA-512
*/
HS512, HS512,
/**
* RSASSA-PKCS-v1_5 using SHA-256
*/
RS256, RS256,
/**
* RSASSA-PKCS-v1_5 using SHA-384
*/
RS384, RS384,
/**
* RSASSA-PKCS-v1_5 using SHA-512
*/
RS512, RS512,
/**
* ECDSA using P-256 and SHA-256
*/
ES256, ES256,
/**
* ECDSA using P-384 and SHA-384
*/
ES384, ES384,
/**
* ECDSA using P-521 and SHA-512
*/
ES512, ES512,
; ;
@@ -21,10 +21,10 @@ package cn.org.codecrafters.simplejwt.exceptions;
* <p> * <p>
* This {@code UnsupportedAlgorithmException} is to indicates that the given * This {@code UnsupportedAlgorithmException} is to indicates that the given
* algorithm is not supported by TokenResolver yet. * algorithm is not supported by TokenResolver yet.
* </p> *
* <p> * <p>
* To support a specified algorithm, you could * To support a specified algorithm, you could
* </p> *
* *
* @author Zihlu Wang * @author Zihlu Wang
*/ */
@@ -54,11 +54,11 @@ public class UnsupportedAlgorithmException extends RuntimeException {
* <p> * <p>
* Constructs a new runtime exception with the specified detail message and * Constructs a new runtime exception with the specified detail message and
* cause. * cause.
* </p> *
* <p>Note that the detail message associated with * <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in * {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message. * this runtime exception's detail message.
* </p> *
* *
* @param message the detail message (which is saved for later retrieval * @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method). * by the {@link #getMessage()} method).
@@ -21,7 +21,7 @@
* custom exception classes related to the Simple JWT library. These * custom exception classes related to the Simple JWT library. These
* exceptions are thrown when there are issues or errors during the generation * exceptions are thrown when there are issues or errors during the generation
* , validation, or processing of JSON Web Tokens (JWTs) in Java applications. * , validation, or processing of JSON Web Tokens (JWTs) in Java applications.
* </p> *
* *
* <p> * <p>
* Custom exception classes in this package are designed to enhance the * Custom exception classes in this package are designed to enhance the
@@ -30,13 +30,12 @@
* identify and troubleshoot issues related to JWT generation, validation, or * identify and troubleshoot issues related to JWT generation, validation, or
* extraction and ensure smooth operation and error handling in their * extraction and ensure smooth operation and error handling in their
* applications. * applications.
* </p> *
* *
* <p> * <p>
* Developers using the Simple JWT library should be aware of the possible * Developers using the Simple JWT library should be aware of the possible
* exceptions that can be thrown and handle them appropriately to ensure secure * exceptions that can be thrown and handle them appropriately to ensure secure
* and reliable JWT handling in their Java applications. * and reliable JWT handling in their Java applications.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -24,7 +24,7 @@
* used to secure web and mobile applications. This library aims to simplify * used to secure web and mobile applications. This library aims to simplify
* the JWT handling process and provide convenient abstractions for JWT * the JWT handling process and provide convenient abstractions for JWT
* generation, validation, and extraction. * generation, validation, and extraction.
* </p> *
* *
* <p> * <p>
* The Simple JWT library is designed to be flexible and customizable, allowing * The Simple JWT library is designed to be flexible and customizable, allowing
@@ -32,13 +32,13 @@
* classes based on their specific application requirements. It aims to * classes based on their specific application requirements. It aims to
* simplify the JWT handling process while maintaining security and best * simplify the JWT handling process while maintaining security and best
* practices for working with JWTs. * practices for working with JWTs.
* </p> *
* *
* <p> * <p>
* Developers should refer to the official documentation and examples for the * Developers should refer to the official documentation and examples for the
* Simple JWT project to understand how to use the library effectively and * Simple JWT project to understand how to use the library effectively and
* securely in their Java applications. * securely in their Java applications.
* </p> *
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 CodeCraftersCN.
*
* 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 cn.org.codecrafters.simplejwt.autoconfiguration;
import cn.org.codecrafters.guid.GuidCreator;
import cn.org.codecrafters.simplejwt.autoconfiguration.properties.SimpleJwtProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import java.util.UUID;
/**
* GuidAutoConfiguration
*
* @author Zihlu Wang
*/
@Slf4j
@AutoConfiguration
@ConditionalOnMissingBean(value = GuidCreator.class, name = "jtiCreator")
public class GuidAutoConfiguration {
@Bean
public GuidCreator<UUID> jtiCreator() {
return UUID::randomUUID;
}
}
@@ -24,6 +24,7 @@ import cn.org.codecrafters.simplejwt.autoconfiguration.properties.SimpleJwtPrope
import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -39,14 +40,14 @@ import java.util.UUID;
* Simple JWT library when used in a Spring Boot application. It provides * Simple JWT library when used in a Spring Boot application. It provides
* default settings and configurations to ensure that the library works * default settings and configurations to ensure that the library works
* smoothly without requiring manual configuration. * smoothly without requiring manual configuration.
* </p> *
* *
* <p> * <p>
* This auto-configuration class sets up the necessary beans and components * This auto-configuration class sets up the necessary beans and components
* required for JWT generation and validation. It automatically creates and * required for JWT generation and validation. It automatically creates and
* configures the {@link TokenResolver} bean based on the available options and * configures the {@link TokenResolver} bean based on the available options and
* properties. * properties.
* </p> *
* *
* <p> * <p>
* Developers using the Simple JWT library with Spring Boot do not need to * Developers using the Simple JWT library with Spring Boot do not need to
@@ -54,31 +55,21 @@ import java.util.UUID;
* setting up the necessary components and configurations automatically. * setting up the necessary components and configurations automatically.
* However, developers still have the flexibility to customize the behavior of * However, developers still have the flexibility to customize the behavior of
* the library by providing their own configurations and properties. * the library by providing their own configurations and properties.
* </p> *
* *
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
* @since 1.0.0 * @since 1.0.0
*/ */
@Slf4j @Slf4j
@Configuration @AutoConfiguration
@EnableConfigurationProperties(value = {SimpleJwtProperties.class}) @EnableConfigurationProperties(value = {SimpleJwtProperties.class})
public class SimpleJwtAutoConfiguration { public class SimpleJwtAutoConfiguration {
/** /**
* The GuidCreator instance to be used for generating JWT IDs (JTI). * The GuidCreator instance to be used for generating JWT IDs (JTI).
*/ */
private GuidCreator<?> jtiCreator; private final GuidCreator<?> jtiCreator;
/**
* Sets the GuidCreator instance to be used for generating JWT IDs (JTI).
*
* @param jtiCreator the {@code GuidCreator} instance
*/
@Autowired
public void setJtiCreator(GuidCreator<?> jtiCreator) {
this.jtiCreator = jtiCreator;
}
/** /**
* The {@code SimpleJwtProperties} instance containing the configuration * The {@code SimpleJwtProperties} instance containing the configuration
@@ -93,23 +84,11 @@ public class SimpleJwtAutoConfiguration {
* @param simpleJwtProperties the SimpleJwtProperties instance * @param simpleJwtProperties the SimpleJwtProperties instance
*/ */
@Autowired @Autowired
public SimpleJwtAutoConfiguration(SimpleJwtProperties simpleJwtProperties) { public SimpleJwtAutoConfiguration(SimpleJwtProperties simpleJwtProperties, GuidCreator<?> jtiCreator) {
this.jtiCreator = jtiCreator;
this.simpleJwtProperties = simpleJwtProperties; this.simpleJwtProperties = simpleJwtProperties;
} }
/**
* Creates a new {@code GuidCreator} bean if no existing bean with the name
* "jtiCreator" is found. The created {@code GuidCreator} is used for
* generating JWT IDs (JTI).
*
* @return the GuidCreator instance
*/
@Bean
@ConditionalOnMissingBean(value = GuidCreator.class, name = "jtiCreator")
public GuidCreator<?> jtiCreator() {
return (GuidCreator<UUID>) UUID::randomUUID;
}
/** /**
* Creates a new {@link TokenResolver} bean using {@link * Creates a new {@link TokenResolver} bean using {@link
* AuthzeroTokenResolver} if no existing {@link TokenResolver} bean is * AuthzeroTokenResolver} if no existing {@link TokenResolver} bean is
@@ -22,7 +22,7 @@
* provide default settings and configurations to ensure that the library works * provide default settings and configurations to ensure that the library works
* smoothly and seamlessly without requiring developers to manually configure * smoothly and seamlessly without requiring developers to manually configure
* it. * it.
* </p> *
* *
* <p> * <p>
* Developers using the Simple JWT library with Spring Boot do not need to * Developers using the Simple JWT library with Spring Boot do not need to
@@ -31,7 +31,7 @@
* automatically. However, developers still have the flexibility to customize * automatically. However, developers still have the flexibility to customize
* the behavior of the library by providing their own configurations and * the behavior of the library by providing their own configurations and
* properties. * properties.
* </p> *
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -27,7 +27,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* store the properties related to Simple JWT library configuration. These * store the properties related to Simple JWT library configuration. These
* properties can be configured in the application's properties file (e.g., * properties can be configured in the application's properties file (e.g.,
* application.properties) with the prefix "code-crafters.simple-jwt". * application.properties) with the prefix "code-crafters.simple-jwt".
* </p> *
* *
* <p> * <p>
* SimpleJwtProperties provides configuration options for the JWT algorithm, * SimpleJwtProperties provides configuration options for the JWT algorithm,
@@ -35,14 +35,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* cn.org.codecrafters.simplejwt.autoconfiguration.SimpleJwtAutoConfiguration} * cn.org.codecrafters.simplejwt.autoconfiguration.SimpleJwtAutoConfiguration}
* class to set up the necessary configurations for JWT generation and * class to set up the necessary configurations for JWT generation and
* validation. * validation.
* </p> *
* *
* <p> * <p>
* Developers can customize the JWT algorithm, issuer, and secret by setting * Developers can customize the JWT algorithm, issuer, and secret by setting
* the corresponding properties in the application's properties file. The * the corresponding properties in the application's properties file. The
* SimpleJwtAutoConfiguration class reads these properties and uses them to * SimpleJwtAutoConfiguration class reads these properties and uses them to
* create the TokenResolver bean with the desired configuration. * create the TokenResolver bean with the desired configuration.
* </p> *
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@@ -23,7 +23,7 @@
* configured in the application's properties file (e.g., * configured in the application's properties file (e.g.,
* application.properties) to customize the behavior and settings of the Simple * application.properties) to customize the behavior and settings of the Simple
* JWT library. * JWT library.
* </p> *
* *
* <p> * <p>
* Developers can customize the JWT algorithm, issuer, and secret by setting * Developers can customize the JWT algorithm, issuer, and secret by setting
@@ -32,7 +32,6 @@
* reads these properties and uses them to create the {@link * reads these properties and uses them to create the {@link
* cn.org.codecrafters.simplejwt.TokenResolver} bean with the desired * cn.org.codecrafters.simplejwt.TokenResolver} bean with the desired
* configuration. * configuration.
* </p>
* *
* @since 1.0.0 * @since 1.0.0
*/ */