9
9
import jakarta .annotation .PostConstruct ;
10
10
import jakarta .servlet .http .HttpServletRequest ;
11
11
import lombok .RequiredArgsConstructor ;
12
- import org .springframework .beans .factory .annotation .Value ;
13
12
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
14
13
import org .springframework .security .core .Authentication ;
15
14
import org .springframework .security .core .userdetails .UserDetails ;
20
19
import java .time .LocalDateTime ;
21
20
import java .util .Date ;
22
21
22
+ import static com .gcms .v3 .global .security .jwt .JwtProperties .*;
23
+
23
24
@ Component
24
25
@ RequiredArgsConstructor
25
26
public class JwtTokenProvider {
26
27
27
- @ Value ("${jwt.secret}" )
28
- private String secretKey ;
29
- private static final String AUTHORITIES = "auth" ;
30
- private static final String GRANT_TYPE = "Bearer" ;
31
- private static final String TOKEN_PREFIX = "Bearer " ;
32
- private static final long ACCESS_TOKEN_TIME = 1000 * 60 * 30L ;
33
- private static final long REFRESH_TOKEN_TIME = 1000L * 60 * 60 * 24 * 7 ;
34
- private static Key key ;
28
+ private static Key accessTokenkey ;
29
+ private static Key refreshtokenkey ;
35
30
private final AuthDetailsService authDetailsService ;
31
+ private final JwtProperties jwtProperties ;
36
32
37
33
@ PostConstruct
38
34
public void init () {
39
- byte [] keyBytes = Decoders .BASE64 .decode (secretKey );
40
- key = Keys .hmacShaKeyFor (keyBytes );
35
+ byte [] keyBytes = Decoders .BASE64 .decode (jwtProperties .getAccessTokenKey ());
36
+ accessTokenkey = Keys .hmacShaKeyFor (keyBytes );
37
+
38
+ byte [] refreshKeyBytes = Decoders .BASE64 .decode (jwtProperties .getRefreshTokenKey ());
39
+ refreshtokenkey = Keys .hmacShaKeyFor (refreshKeyBytes );
41
40
}
42
41
43
42
public TokenInfoResponseDto generateToken (String email ) {
@@ -60,7 +59,7 @@ private String generateAccessToken(String email) {
60
59
.setHeaderParam ("typ" , GRANT_TYPE )
61
60
.claim (AUTHORITIES , "JWT" )
62
61
.setExpiration (accessTokenExpiresIn )
63
- .signWith (key , SignatureAlgorithm .HS256 )
62
+ .signWith (accessTokenkey , SignatureAlgorithm .HS256 )
64
63
.compact ();
65
64
}
66
65
@@ -72,7 +71,7 @@ private String generateRefreshToken(String email) {
72
71
return Jwts .builder ()
73
72
.setSubject (email )
74
73
.setHeaderParam ("typ" , "JWT" )
75
- .signWith (key , SignatureAlgorithm .HS256 )
74
+ .signWith (refreshtokenkey , SignatureAlgorithm .HS256 )
76
75
.claim (AUTHORITIES , "JWT" )
77
76
.setIssuedAt (new Date ())
78
77
.setExpiration (refreshTokenExpiresIn )
@@ -92,7 +91,7 @@ public Authentication getAuthentication(String token) {
92
91
93
92
private Claims parseClaims (String assessToken ) {
94
93
try {
95
- return Jwts .parserBuilder ().setSigningKey (key ).build ().parseClaimsJws (assessToken ).getBody ();
94
+ return Jwts .parserBuilder ().setSigningKey (accessTokenkey ).build ().parseClaimsJws (assessToken ).getBody ();
96
95
} catch (ExpiredJwtException e ) {
97
96
return e .getClaims ();
98
97
}
@@ -108,7 +107,7 @@ public String resolveToken(HttpServletRequest request) {
108
107
109
108
public boolean validateToken (String token ) {
110
109
try {
111
- Jwts .parserBuilder ().setSigningKey (key ).build ().parseClaimsJws (token );
110
+ Jwts .parserBuilder ().setSigningKey (accessTokenkey ).build ().parseClaimsJws (token );
112
111
return true ;
113
112
} catch (SecurityException | MalformedJwtException e ) {
114
113
throw new InvalidAuthTokenException ();
0 commit comments