Skip to content

Commit 5895557

Browse files
committed
refactor: Optimsed codes.
Move private and protected methods or fields to the last of java files.
1 parent cb537a6 commit 5895557

File tree

16 files changed

+201
-153
lines changed

16 files changed

+201
-153
lines changed

gradlew

100644100755
File mode changed.

simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/SecretCreator.java

+29-30
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,6 @@
3232
*/
3333
public final class SecretCreator {
3434

35-
/**
36-
* Private constructor to prevent instantiation
37-
*/
38-
private SecretCreator() {
39-
}
40-
41-
/**
42-
* The string containing all lowercase characters that can be used to
43-
* generate the secret.
44-
*/
45-
private static final String LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
46-
47-
/**
48-
* The string containing all uppercase characters that can be used to
49-
* generate the secret.
50-
*/
51-
private static final String UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52-
53-
/**
54-
* The string containing all digit characters that can be used to generate
55-
* the secret.
56-
*/
57-
private static final String DIGITS = "0123456789";
58-
59-
/**
60-
* The string containing all special sign characters that can be used to
61-
* generate the secret.
62-
*/
63-
private static final String SPECIAL_SIGNS = "!@#$%^&,*()_+-=,[]{}|;:,'\",.<>/?";
64-
6535
/**
6636
* Generates a secure secret with the specified length and character sets.
6737
*
@@ -153,4 +123,33 @@ public static String createSecret(int length) {
153123
return createSecret(length, false, false, false);
154124
}
155125

126+
/**
127+
* Private constructor will protect this class from being instantiated.
128+
*/
129+
private SecretCreator() {
130+
}
131+
132+
/**
133+
* The string containing all lowercase characters that can be used to
134+
* generate the secret.
135+
*/
136+
private static final String LOWERCASE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
137+
138+
/**
139+
* The string containing all uppercase characters that can be used to
140+
* generate the secret.
141+
*/
142+
private static final String UPPERCASE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
143+
144+
/**
145+
* The string containing all digit characters that can be used to generate
146+
* the secret.
147+
*/
148+
private static final String DIGITS = "0123456789";
149+
150+
/**
151+
* The string containing all special sign characters that can be used to
152+
* generate the secret.
153+
*/
154+
private static final String SPECIAL_SIGNS = "!@#$%^&,*()_+-=,[]{}|;:,'\",.<>/?";
156155
}

simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/PredefinedKeys.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ public final class PredefinedKeys {
8888
public static final List<String> KEYS = List.of(ISSUER, SUBJECT, AUDIENCE, EXPIRATION_TIME, NOT_BEFORE, ISSUED_AT, JWT_ID);
8989

9090
/**
91-
* Private constructor to prevent instantiation of the
92-
* {@code PredefinedKeys} class.
93-
* <p>
94-
* This class is intended to be used as a utility class with only static
95-
* constants and methods.
91+
* Private constructor will protect this class from being instantiated.
9692
*/
9793
private PredefinedKeys() {
9894
}

simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/TokenDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public enum TokenDataType {
6363
*/
6464
private final Class<?> mappedClass;
6565

66+
/**
67+
* Create a TokenDataType with a mapped class.
68+
*/
6669
TokenDataType(Class<?> mappedClass) {
6770
this.mappedClass = mappedClass;
6871
}

simple-jwt-jjwt/src/main/java/cn/org/codecrafters/simplejwt/jjwt/JjwtTokenResolver.java

+34-10
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@
9898
@Slf4j
9999
public class JjwtTokenResolver implements TokenResolver<Jws<Claims>> {
100100

101-
private final GuidCreator<?> jtiCreator;
102-
103-
private final SecureDigestAlgorithm<SecretKey, SecretKey> algorithm;
104-
105-
private final String issuer;
106-
107-
private final SecretKey key;
108-
109-
private final JjwtTokenResolverConfig config = JjwtTokenResolverConfig.getInstance();
110-
111101
/**
112102
* Create a resolver with specified algorithm, issuer, secret and guid strategy.
113103
*
@@ -432,6 +422,15 @@ public <T extends TokenPayload> String renew(String oldToken, T payload) {
432422
return renew(oldToken, Duration.ofMinutes(30), payload);
433423
}
434424

425+
/**
426+
* Build a new token with specified data.
427+
*
428+
* @param expireAfter the validity time of the token
429+
* @param audience the audience of the token
430+
* @param subject the subject of the token
431+
* @param claims the data to be included in the token
432+
* @return the built token
433+
*/
435434
private String buildToken(Duration expireAfter, String audience, String subject, Map<String, Object> claims) {
436435
var now = LocalDateTime.now();
437436
var builder = Jwts.builder()
@@ -453,4 +452,29 @@ private String buildToken(Duration expireAfter, String audience, String subject,
453452
return builder.signWith(key, algorithm)
454453
.compact();
455454
}
455+
456+
/**
457+
* The ID creator for creating unique JWT IDs.
458+
*/
459+
private final GuidCreator<?> jtiCreator;
460+
461+
/**
462+
* The algorithm to sign this token.
463+
*/
464+
private final SecureDigestAlgorithm<SecretKey, SecretKey> algorithm;
465+
466+
/**
467+
* The issuer of this token.
468+
*/
469+
private final String issuer;
470+
471+
/**
472+
* The signature key of this token.
473+
*/
474+
private final SecretKey key;
475+
476+
/**
477+
* The config of this token resolver.
478+
*/
479+
private final JjwtTokenResolverConfig config = JjwtTokenResolverConfig.getInstance();
456480
}

simple-jwt-jjwt/src/main/java/cn/org/codecrafters/simplejwt/jjwt/config/JjwtTokenResolverConfig.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@
6363
*/
6464
public final class JjwtTokenResolverConfig implements TokenResolverConfig<SecureDigestAlgorithm<SecretKey, SecretKey>> {
6565

66-
private JjwtTokenResolverConfig() {
67-
}
68-
69-
private static final Map<TokenAlgorithm, SecureDigestAlgorithm<SecretKey, SecretKey>> SUPPORTED_ALGORITHMS = new HashMap<>() {{
70-
put(TokenAlgorithm.HS256, Jwts.SIG.HS256);
71-
put(TokenAlgorithm.HS384, Jwts.SIG.HS384);
72-
put(TokenAlgorithm.HS512, Jwts.SIG.HS512);
73-
}};
74-
75-
private static JjwtTokenResolverConfig instance;
76-
7766
public static JjwtTokenResolverConfig getInstance() {
7867
if (instance == null) {
7968
instance = new JjwtTokenResolverConfig();
@@ -106,4 +95,25 @@ public SecureDigestAlgorithm<SecretKey, SecretKey> getAlgorithm(TokenAlgorithm a
10695
}
10796
return SUPPORTED_ALGORITHMS.get(algorithm);
10897
}
98+
99+
/**
100+
* Private constructor will protect this class from being instantiated.
101+
*/
102+
private JjwtTokenResolverConfig() {
103+
}
104+
105+
/**
106+
* A {@code Map} to map a {@link TokenAlgorithm} to {@link SecureDigestAlgorithm}.
107+
*/
108+
private static final Map<TokenAlgorithm, SecureDigestAlgorithm<SecretKey, SecretKey>> SUPPORTED_ALGORITHMS = new HashMap<>() {{
109+
put(TokenAlgorithm.HS256, Jwts.SIG.HS256);
110+
put(TokenAlgorithm.HS384, Jwts.SIG.HS384);
111+
put(TokenAlgorithm.HS512, Jwts.SIG.HS512);
112+
}};
113+
114+
/**
115+
* The instance of this config class.
116+
*/
117+
private static JjwtTokenResolverConfig instance;
118+
109119
}

simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/AuthzeroTokenResolverAutoConfiguration.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@
6565
@AutoConfigureAfter(value = GuidAutoConfiguration.class)
6666
public class AuthzeroTokenResolverAutoConfiguration {
6767

68-
/**
69-
* The GuidCreator instance to be used for generating JWT IDs (JTI).
70-
*/
71-
private final GuidCreator<?> jtiCreator;
72-
73-
/**
74-
* The {@code SimpleJwtProperties} instance containing the configuration
75-
* properties for Simple JWT.
76-
*/
77-
private final SimpleJwtProperties simpleJwtProperties;
78-
79-
private final ObjectMapper objectMapper;
80-
8168
/**
8269
* Constructs a new {@code SimpleJwtAutoConfiguration} instance with the
8370
* provided SimpleJwtProperties.
@@ -112,4 +99,17 @@ public TokenResolver<DecodedJWT> tokenResolver() {
11299
);
113100
}
114101

102+
/**
103+
* The GuidCreator instance to be used for generating JWT IDs (JTI).
104+
*/
105+
private final GuidCreator<?> jtiCreator;
106+
107+
/**
108+
* The {@code SimpleJwtProperties} instance containing the configuration
109+
* properties for Simple JWT.
110+
*/
111+
private final SimpleJwtProperties simpleJwtProperties;
112+
113+
private final ObjectMapper objectMapper;
114+
115115
}

simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/GuidAutoConfiguration.java

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
@AutoConfiguration
4040
public class GuidAutoConfiguration {
4141

42+
/**
43+
* Create a default {@code jtiCreator} with UUID.
44+
*
45+
* @return UUID creator
46+
*/
4247
@Bean(name = "jtiCreator")
4348
@Conditional(GuidCreatorCondition.class)
4449
public GuidCreator<?> jtiCreator() {

simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/JjwtTokenResolverAutoConfiguration.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,6 @@
6565
@AutoConfigureAfter(value = GuidAutoConfiguration.class)
6666
public class JjwtTokenResolverAutoConfiguration {
6767

68-
/**
69-
* The GuidCreator instance to be used for generating JWT IDs (JTI).
70-
*/
71-
private final GuidCreator<?> jtiCreator;
72-
73-
/**
74-
* The {@code SimpleJwtProperties} instance containing the configuration
75-
* properties for Simple JWT.
76-
*/
77-
private final SimpleJwtProperties simpleJwtProperties;
78-
7968
/**
8069
* Constructs a new {@code SimpleJwtAutoConfiguration} instance with the
8170
* provided SimpleJwtProperties.
@@ -107,4 +96,15 @@ public TokenResolver<Jws<Claims>> tokenResolver() {
10796
);
10897
}
10998

99+
/**
100+
* The GuidCreator instance to be used for generating JWT IDs (JTI).
101+
*/
102+
private final GuidCreator<?> jtiCreator;
103+
104+
/**
105+
* The {@code SimpleJwtProperties} instance containing the configuration
106+
* properties for Simple JWT.
107+
*/
108+
private final SimpleJwtProperties simpleJwtProperties;
109+
110110
}

simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/conditions/GuidCreatorCondition.java

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
*/
1818
@Slf4j
1919
public class GuidCreatorCondition implements Condition {
20+
21+
/**
22+
* The condition to create bean {@code jtiCreator}.
23+
* <p>
24+
* If Spring does not have a bean of type
25+
* {@link cn.org.codecrafters.guid.GuidCreator} named {@code jtiCreator}
26+
* in the application context, then create {@code jtiCreator}.
27+
*
28+
* @param context the spring application context
29+
* @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
30+
* or {@link org.springframework.core.type.MethodMetadata method} being checked
31+
*/
2032
@Override
2133
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
2234
final var beanFactory = Objects.requireNonNull(context.getBeanFactory());

0 commit comments

Comments
 (0)