feat(simple-jwt-jjwt): Complete the implementation with io.jsonwebtoken:jjwt-api

This commit is contained in:
Zihlu Wang
2023-08-04 19:45:23 +08:00
parent 9405908964
commit 6557b65d8a
11 changed files with 693 additions and 36 deletions
@@ -34,17 +34,15 @@ import org.springframework.context.annotation.Bean;
/**
* <p>
* SimpleJwtAutoConfiguration is responsible for automatically configuring the
* Simple JWT library when used in a Spring Boot application. It provides
* default settings and configurations to ensure that the library works
* smoothly without requiring manual configuration.
*
* 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 auto-configuration class sets up the necessary beans and components
* required for JWT generation and validation. It automatically creates and
* configures the {@link TokenResolver} bean based on the available options and
* properties.
*
*
* <p>
* Developers using the Simple JWT library with Spring Boot do not need to
@@ -61,7 +59,9 @@ import org.springframework.context.annotation.Bean;
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(value = {SimpleJwtProperties.class})
public class SimpleJwtAutoConfiguration {
@ConditionalOnClass({DecodedJWT.class, AuthzeroTokenResolver.class})
@ConditionalOnMissingBean({TokenResolver.class})
public class AuthzeroTokenResolverAutoConfiguration {
/**
* The GuidCreator instance to be used for generating JWT IDs (JTI).
@@ -81,7 +81,7 @@ public class SimpleJwtAutoConfiguration {
* @param simpleJwtProperties the SimpleJwtProperties instance
*/
@Autowired
public SimpleJwtAutoConfiguration(SimpleJwtProperties simpleJwtProperties, GuidCreator<?> jtiCreator) {
public AuthzeroTokenResolverAutoConfiguration(SimpleJwtProperties simpleJwtProperties, GuidCreator<?> jtiCreator) {
this.jtiCreator = jtiCreator;
this.simpleJwtProperties = simpleJwtProperties;
}
@@ -96,14 +96,14 @@ public class SimpleJwtAutoConfiguration {
* @return the {@link TokenResolver} instance
*/
@Bean
@ConditionalOnClass({DecodedJWT.class, AuthzeroTokenResolver.class})
@ConditionalOnMissingBean({TokenResolver.class})
@ConditionalOnBean(value = {GuidCreator.class}, name = "jtiCreator")
public TokenResolver<DecodedJWT> tokenResolver() {
return new AuthzeroTokenResolver(this.jtiCreator,
return new AuthzeroTokenResolver(
jtiCreator,
simpleJwtProperties.algorithm(),
simpleJwtProperties.issuer(),
simpleJwtProperties.secret());
simpleJwtProperties.secret()
);
}
}
@@ -0,0 +1,111 @@
/*
* 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.TokenResolver;
import cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver;
import cn.org.codecrafters.simplejwt.autoconfiguration.properties.SimpleJwtProperties;
import cn.org.codecrafters.simplejwt.jjwt.JjwtTokenResolver;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import lombok.extern.slf4j.Slf4j;
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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* <p>
* JjwtTokenResolverAutoConfiguration is responsible for automatically
* configuring the Simple JWT library with {@code io.jsonwebtoken:jjwt-api}
* 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 auto-configuration class sets up the necessary beans and components
* required for JWT generation and validation. It automatically creates and
* configures the {@link TokenResolver} 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 auto-configuration takes care of
* setting up the necessary components and configurations automatically.
* However, developers still have the flexibility to customize the behavior of
* the library by providing their own configurations and properties.
*
* @author Zihlu Wang
* @version 1.0.0
* @since 1.0.0
*/
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(value = {SimpleJwtProperties.class})
@ConditionalOnClass({Jws.class, Claims.class, JjwtTokenResolver.class})
@ConditionalOnMissingBean({TokenResolver.class})
public class JjwtTokenResolverAutoConfiguration {
/**
* The GuidCreator instance to be used for generating JWT IDs (JTI).
*/
private final GuidCreator<?> jtiCreator;
/**
* The {@code SimpleJwtProperties} instance containing the configuration
* properties for Simple JWT.
*/
private final SimpleJwtProperties simpleJwtProperties;
/**
* Constructs a new {@code SimpleJwtAutoConfiguration} instance with the
* provided SimpleJwtProperties.
*
* @param simpleJwtProperties the SimpleJwtProperties instance
*/
@Autowired
public JjwtTokenResolverAutoConfiguration(SimpleJwtProperties simpleJwtProperties, GuidCreator<?> jtiCreator) {
this.jtiCreator = jtiCreator;
this.simpleJwtProperties = simpleJwtProperties;
}
/**
* Creates a new {@link TokenResolver} bean using {@link
* JjwtTokenResolver} if no existing {@link TokenResolver} bean is
* found. The {@link JjwtTokenResolver} is configured with the
* provided {@link GuidCreator}, {@code algorithm}, {@code issuer}, and
* {@code secret} properties from {@link SimpleJwtProperties}.
*
* @return the {@link TokenResolver} instance
*/
@Bean
@ConditionalOnBean(value = {GuidCreator.class}, name = "jtiCreator")
public TokenResolver<Jws<Claims>> tokenResolver() {
return new JjwtTokenResolver(
jtiCreator,
simpleJwtProperties.algorithm(),
simpleJwtProperties.issuer(),
simpleJwtProperties.secret()
);
}
}
@@ -17,6 +17,8 @@
package cn.org.codecrafters.simplejwt.autoconfiguration.properties;
import cn.org.codecrafters.simplejwt.SecretCreator;
import cn.org.codecrafters.simplejwt.autoconfiguration.AuthzeroTokenResolverAutoConfiguration;
import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -32,7 +34,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* <p>
* SimpleJwtProperties provides configuration options for the JWT algorithm,
* issuer, and secret. The properties are used by the {@link
* cn.org.codecrafters.simplejwt.autoconfiguration.SimpleJwtAutoConfiguration}
* AuthzeroTokenResolverAutoConfiguration}
* class to set up the necessary configurations for JWT generation and
* validation.
*
@@ -51,19 +53,23 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class SimpleJwtProperties {
/**
* The algorithm used for JWT generation and validation.
* The algorithm used for JWT generation and validation. Default value is
* {@link TokenAlgorithm#HS256}
*/
private TokenAlgorithm algorithm;
private TokenAlgorithm algorithm = TokenAlgorithm.HS256;
/**
* The issuer value to be included in the generated JWT.
* The issuer value to be included in the generated JWT. Default value is
* an empty String.
*/
private String issuer;
private String issuer = "";
/**
* The secret key used for JWT generation and validation.
* 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;
private String secret = SecretCreator.createSecret(32, true, true, true);
/**
* Returns the JWT algorithm configured in the properties.
@@ -1,2 +1,3 @@
cn.org.codecrafters.simplejwt.autoconfiguration.GuidAutoConfiguration
cn.org.codecrafters.simplejwt.autoconfiguration.SimpleJwtAutoConfiguration
cn.org.codecrafters.simplejwt.autoconfiguration.AuthzeroTokenResolverAutoConfiguration
cn.org.codecrafters.simplejwt.autoconfiguration.JjwtTokenResolverAutoConfiguration