@@ -0,0 +1,101 @@
|
||||
# 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)
|
||||
- [`cn.org.codecrafters:simple-jwt-jjwt`](../simple-jwt-jjwt/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>com.onixbyte</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 'com.onixbyte: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 `com.onixbyte.identitygenerator.IdentityGenerator` 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).
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import java.net.URI
|
||||
|
||||
plugins {
|
||||
java
|
||||
id("java-library")
|
||||
id("maven-publish")
|
||||
id("signing")
|
||||
}
|
||||
|
||||
val artefactVersion: String by project
|
||||
val projectUrl: String by project
|
||||
val projectGithubUrl: String by project
|
||||
val licenseName: String by project
|
||||
val licenseUrl: String by project
|
||||
|
||||
group = "com.onixbyte"
|
||||
version = artefactVersion
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
withSourcesJar()
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
tasks.withType<JavaCompile> {
|
||||
options.encoding = "UTF-8"
|
||||
}
|
||||
|
||||
tasks.withType<Jar> {
|
||||
exclude("logback.xml")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly(libs.slf4j)
|
||||
implementation(libs.logback)
|
||||
|
||||
api(project(":identity-generator"))
|
||||
api(project(":jwt-toolbox-facade"))
|
||||
api(project(":jwt-toolbox-auth0"))
|
||||
implementation(libs.springBoot.autoconfigure)
|
||||
implementation(libs.springBoot.starter.logging)
|
||||
implementation(libs.springBoot.configurationProcessor)
|
||||
annotationProcessor(libs.springBoot.configurationProcessor)
|
||||
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testImplementation(libs.springBoot.starter.test)
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
withSourcesJar()
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("simpleJwtSpringBootStarter") {
|
||||
groupId = group.toString()
|
||||
artifactId = "simple-jwt-spring-boot-starter"
|
||||
version = artefactVersion
|
||||
|
||||
pom {
|
||||
name = "Simple JWT :: Spring Boot Starter"
|
||||
description = "Simple JWT all-in-one package for Spring Boot."
|
||||
url = projectUrl
|
||||
|
||||
licenses {
|
||||
license {
|
||||
name = licenseName
|
||||
url = licenseUrl
|
||||
}
|
||||
}
|
||||
|
||||
scm {
|
||||
connection = "scm:git:git://github.com:OnixByte/JDevKit.git"
|
||||
developerConnection = "scm:git:git://github.com:OnixByte/JDevKit.git"
|
||||
url = projectGithubUrl
|
||||
}
|
||||
|
||||
developers {
|
||||
developer {
|
||||
id = "zihluwang"
|
||||
name = "Zihlu Wang"
|
||||
email = "really@zihlu.wang"
|
||||
timezone = "Asia/Hong_Kong"
|
||||
}
|
||||
|
||||
developer {
|
||||
id = "siujamo"
|
||||
name = "Siu Jam'o"
|
||||
email = "jamo.siu@outlook.com"
|
||||
timezone = "Asia/Shanghai"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
from(components["java"])
|
||||
|
||||
signing {
|
||||
sign(publishing.publications["simpleJwtSpringBootStarter"])
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
name = "sonatypeNexus"
|
||||
url = URI(providers.gradleProperty("repo.maven-central.host").get())
|
||||
credentials {
|
||||
username = providers.gradleProperty("repo.maven-central.username").get()
|
||||
password = providers.gradleProperty("repo.maven-central.password").get()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.onixbyte.simplejwt.autoconfiguration;
|
||||
|
||||
import com.onixbyte.identitygenerator.IdentityGenerator;
|
||||
import com.onixbyte.simplejwt.TokenResolver;
|
||||
import com.onixbyte.simplejwt.authzero.AuthzeroTokenResolver;
|
||||
import com.onixbyte.simplejwt.autoconfiguration.properties.SimpleJwtProperties;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.onixbyte.simplejwt.constants.TokenAlgorithm;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* {@code AuthzeroTokenResolverAutoConfiguration} is responsible for automatically configuring the
|
||||
* Simple JWT library with
|
||||
* {@code com.auth0:java-jwt} when used in a Spring Boot application. It provides default settings
|
||||
* and configurations to ensure that the library works smoothly without requiring
|
||||
* manual configuration.
|
||||
* <p>
|
||||
* This autoconfiguration class sets up the necessary beans and components required for JWT
|
||||
* generation and validation. It automatically creates and configures the
|
||||
* {@link AuthzeroTokenResolver} bean based on the available options and properties.
|
||||
* <p>
|
||||
* Developers using the Simple JWT library with Spring Boot do not need to explicitly configure the
|
||||
* library, as the autoconfiguration takes care of setting up the necessary components and
|
||||
* configurations automatically. However, developers still have the flexibility to customise the
|
||||
* behavior of the library by providing their own configurations and properties.
|
||||
*
|
||||
* @author zihluwang
|
||||
* @version 1.6.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(value = {SimpleJwtProperties.class})
|
||||
@ConditionalOnClass({DecodedJWT.class, AuthzeroTokenResolver.class})
|
||||
@ConditionalOnMissingBean({TokenResolver.class})
|
||||
@ConditionalOnBean(value = {IdentityGenerator.class}, name = "jtiCreator")
|
||||
@AutoConfigureAfter(value = GuidAutoConfiguration.class)
|
||||
public class AuthzeroTokenResolverAutoConfiguration {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(AuthzeroTokenResolverAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* Constructs a new {@code SimpleJwtAutoConfiguration} instance with the
|
||||
* provided SimpleJwtProperties.
|
||||
*
|
||||
* @param simpleJwtProperties a {@link SimpleJwtProperties} instance
|
||||
* @param jtiCreator a creator to create ids for JSON Web Token
|
||||
* @param objectMapper jackson JSON Handler
|
||||
*/
|
||||
@Autowired
|
||||
public AuthzeroTokenResolverAutoConfiguration(SimpleJwtProperties simpleJwtProperties,
|
||||
@Qualifier("jtiCreator") IdentityGenerator<?> jtiCreator,
|
||||
ObjectMapper objectMapper) {
|
||||
this.jtiCreator = jtiCreator;
|
||||
this.simpleJwtProperties = simpleJwtProperties;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link TokenResolver} bean using {@link AuthzeroTokenResolver} if no existing
|
||||
* {@link TokenResolver} bean is found. The {@link AuthzeroTokenResolver} is configured with the
|
||||
* provided {@link IdentityGenerator}, {@code algorithm}, {@code issuer}, and {@code secret}
|
||||
* properties from {@link SimpleJwtProperties}.
|
||||
*
|
||||
* @return the {@link TokenResolver} instance
|
||||
*/
|
||||
@Bean
|
||||
public TokenResolver<DecodedJWT> tokenResolver() {
|
||||
var builder = AuthzeroTokenResolver.builder();
|
||||
|
||||
if (TokenAlgorithm.ECDSA_ALGORITHMS.contains(simpleJwtProperties.getAlgorithm())) {
|
||||
builder.keyPair(simpleJwtProperties.getPublicKey(), simpleJwtProperties.getPrivateKey())
|
||||
.algorithm(simpleJwtProperties.getAlgorithm());
|
||||
} else if (TokenAlgorithm.HMAC_ALGORITHMS.contains(simpleJwtProperties.getAlgorithm())) {
|
||||
builder.secret(simpleJwtProperties.getSecret())
|
||||
.algorithm(simpleJwtProperties.getAlgorithm());
|
||||
}
|
||||
|
||||
builder.issuer(simpleJwtProperties.getIssuer());
|
||||
builder.jtiCreator(jtiCreator);
|
||||
builder.objectMapper(objectMapper);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private final IdentityGenerator<?> jtiCreator;
|
||||
|
||||
private final SimpleJwtProperties simpleJwtProperties;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.onixbyte.simplejwt.autoconfiguration;
|
||||
|
||||
import com.onixbyte.identitygenerator.IdentityGenerator;
|
||||
import com.onixbyte.simplejwt.autoconfiguration.conditions.GuidCreatorCondition;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Autoconfiguration for injecting a {@link IdentityGenerator} for generating jwt id.
|
||||
*
|
||||
* @author Zihlu Wang
|
||||
* @version 1.1.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
public class GuidAutoConfiguration {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(GuidAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public GuidAutoConfiguration() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default {@code jtiCreator} with UUID.
|
||||
*
|
||||
* @return UUID creator
|
||||
*/
|
||||
@Bean(name = "jtiCreator")
|
||||
@Conditional(GuidCreatorCondition.class)
|
||||
public IdentityGenerator<?> jtiCreator() {
|
||||
return UUID::randomUUID;
|
||||
}
|
||||
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.onixbyte.simplejwt.autoconfiguration.conditions;
|
||||
|
||||
import com.onixbyte.identitygenerator.IdentityGenerator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The conditions to create bean {@code jtiCreator}.
|
||||
*
|
||||
* @author Zihlu Wang
|
||||
* @version 1.1.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class GuidCreatorCondition implements Condition {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(GuidCreatorCondition.class);
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public GuidCreatorCondition() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The condition to create bean {@code jtiCreator}.
|
||||
* <p>
|
||||
* If Spring does not have a bean of type {@link IdentityGenerator} named {@code jtiCreator} in the
|
||||
* application context, then create {@code jtiCreator}.
|
||||
*
|
||||
* @param context the spring application context
|
||||
* @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata
|
||||
* class} or {@link org.springframework.core.type.MethodMetadata method}
|
||||
* being checked
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
final var beanFactory = Objects.requireNonNull(context.getBeanFactory());
|
||||
var isContainJtiCreator = beanFactory.containsBean("jtiCreator");
|
||||
if (isContainJtiCreator) {
|
||||
return !(beanFactory.getBean("jtiCreator") instanceof IdentityGenerator<?>);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.onixbyte.simplejwt.autoconfiguration.properties;
|
||||
|
||||
import com.onixbyte.simplejwt.SecretCreator;
|
||||
import com.onixbyte.simplejwt.autoconfiguration.AuthzeroTokenResolverAutoConfiguration;
|
||||
import com.onixbyte.simplejwt.constants.TokenAlgorithm;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* {@code SimpleJwtProperties} is a configuration properties class used to store the properties
|
||||
* related to Simple JWT library configurations. These properties can be configured in the
|
||||
* application's properties file (e.g., application.properties) with the prefix
|
||||
* "onixbyte.simple-jwt".
|
||||
* <p>
|
||||
* {@code SimpleJwtProperties} provides configuration options for the JWT algorithm, issuer,
|
||||
* and secret. The properties are used by the {@link AuthzeroTokenResolverAutoConfiguration} to
|
||||
* set up the necessary configurations for JWT generation and validation.
|
||||
* <p>
|
||||
* Developers can customise the JWT algorithm, issuer, and secret by setting the corresponding
|
||||
* properties in the application's properties file. The {@code SimpleJwtAutoConfiguration} class
|
||||
* reads these properties and uses them to create the TokenResolver bean with the
|
||||
* desired configuration.
|
||||
*
|
||||
* @author Zihlu Wang
|
||||
* @version 1.1.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "onixbyte.simple-jwt")
|
||||
public class SimpleJwtProperties {
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public SimpleJwtProperties() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The algorithm used for JWT generation and validation. Default value is
|
||||
* {@link TokenAlgorithm#HS256}
|
||||
*/
|
||||
private TokenAlgorithm algorithm = TokenAlgorithm.HS256;
|
||||
|
||||
/**
|
||||
* The issuer value to be included in the generated JWT. Default value is an empty String.
|
||||
*/
|
||||
private String issuer = "";
|
||||
|
||||
/**
|
||||
* The secret key used for JWT generation and validation. Default value is the result of call to
|
||||
* {@link SecretCreator#createSecret(int, boolean, boolean, boolean)}.
|
||||
*/
|
||||
private String secret = SecretCreator.createSecret(32, true, true, true);
|
||||
|
||||
/**
|
||||
* The private key, PEM formatted.
|
||||
*/
|
||||
private String privateKey;
|
||||
|
||||
/**
|
||||
* The public key, PEM formatted
|
||||
*/
|
||||
private String publicKey;
|
||||
|
||||
/**
|
||||
* Algorithm getter.
|
||||
*
|
||||
* @return algorithm
|
||||
*/
|
||||
public TokenAlgorithm getAlgorithm() {
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Algorithm setter.
|
||||
*
|
||||
* @param algorithm the algorithm
|
||||
*/
|
||||
public void setAlgorithm(TokenAlgorithm algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Issuer getter.
|
||||
*
|
||||
* @return issuer
|
||||
*/
|
||||
public String getIssuer() {
|
||||
return issuer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Issuer setter.
|
||||
*
|
||||
* @param issuer the issuer
|
||||
*/
|
||||
public void setIssuer(String issuer) {
|
||||
this.issuer = issuer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Secret setter.
|
||||
*
|
||||
* @return secret
|
||||
*/
|
||||
public String getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Secret setter.
|
||||
*
|
||||
* @param secret the secret
|
||||
*/
|
||||
public void setSecret(String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private key getter.
|
||||
*
|
||||
* @return private key
|
||||
*/
|
||||
public String getPrivateKey() {
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private key setter.
|
||||
*
|
||||
* @param privateKey private key
|
||||
*/
|
||||
public void setPrivateKey(String privateKey) {
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public key getter.
|
||||
*
|
||||
* @return public key
|
||||
*/
|
||||
public String getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public key setter.
|
||||
*
|
||||
* @param publicKey public key
|
||||
*/
|
||||
public void setPublicKey(String publicKey) {
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
com.onixbyte.simplejwt.autoconfiguration.GuidAutoConfiguration
|
||||
com.onixbyte.simplejwt.autoconfiguration.AuthzeroTokenResolverAutoConfiguration
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 2024-2025 OnixByte.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<configuration>
|
||||
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
||||
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
||||
|
||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>${COLOURFUL_OUTPUT}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user