Skip to content

Commit f429ca7

Browse files
committed
[fix] : jwt.secret 키 수정 #36
1 parent 977fe55 commit f429ca7

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/main/java/org/dfbf/soundlink/SoundLinkJavaApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
66

7-
@SpringBootApplication(exclude= SecurityAutoConfiguration.class)
7+
@SpringBootApplication
88
public class SoundLinkJavaApplication {
99

1010
public static void main(String[] args) {

src/main/java/org/dfbf/soundlink/global/auth/JwtProvider.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.dfbf.soundlink.global.auth;
22

33
import io.jsonwebtoken.security.Keys;
4+
import jakarta.annotation.PostConstruct;
45
import jakarta.servlet.http.Cookie;
56
import jakarta.servlet.http.HttpServletRequest;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -10,6 +11,7 @@
1011
import io.jsonwebtoken.*;
1112

1213
import javax.crypto.SecretKey;
14+
import java.util.Base64;
1315
import java.util.Date;
1416
import java.util.concurrent.TimeUnit;
1517

@@ -24,7 +26,17 @@ public class JwtProvider {
2426
private long REFRESH_EXPIRATION_TIME;
2527

2628
//시크릿 키
27-
private final SecretKey SECRET_KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256);
29+
@Value("${jwt.secret}")
30+
private String secretKey;
31+
private SecretKey SECRET_KEY;
32+
33+
@PostConstruct
34+
public void init(){
35+
byte[] keyBytes = Base64.getDecoder().decode(secretKey); //인코딩된 문자열 ->바이트 배열로 변환
36+
this.SECRET_KEY = Keys.hmacShaKeyFor(keyBytes); //안전한 HMAC 키로 변환
37+
System.out.println("Decoded secretKey: " + secretKey); // 디버깅용
38+
System.out.println("Generated SECRET_KEY: " + SECRET_KEY); // 디버깅용
39+
}
2840

2941
@Autowired
3042
private RedisTemplate<String, String> redisTemplate;
@@ -38,6 +50,7 @@ public String createAccessToken(long userId) {
3850
.setClaims(claims)
3951
.setIssuedAt(now)
4052
.setExpiration(new Date(now.getTime()+ACCESS_EXPIRATION_TIME))
53+
.setHeaderParam("typ", "JWT")
4154
.signWith(SECRET_KEY, SignatureAlgorithm.HS256)
4255
.compact();
4356
}
@@ -50,6 +63,7 @@ public String createRefreshToken(long userId) {
5063
.setClaims(claims)
5164
.setIssuedAt(now)
5265
.setExpiration(new Date(now.getTime()+REFRESH_EXPIRATION_TIME))
66+
.setHeaderParam("typ", "JWT")
5367
.signWith(SECRET_KEY, SignatureAlgorithm.HS256)
5468
.compact();
5569
try {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.dfbf.soundlink.domain.user;
2+
3+
import io.jsonwebtoken.security.Keys;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
9+
import javax.crypto.SecretKey;
10+
import java.util.Base64;
11+
12+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
13+
14+
@SpringBootTest
15+
public class CreateJwtTest {
16+
17+
@Value("${jwt.secret}")
18+
private String secretKeyPlain;
19+
20+
@Test
21+
void secretKeyValid(){
22+
assertThat(secretKeyPlain).isNotNull();
23+
}
24+
25+
@Test
26+
// @DisplayName("secretKey 원문으로 hmac 암호화 알고리즘에 맞는 SecretKey 객체를 만들 수 있다.")
27+
void t2() {
28+
// secretKeyPlain이 Base64 인코딩된 상태라면 디코딩하여 사용해야 함
29+
byte[] keyBytes = Base64.getDecoder().decode(secretKeyPlain);
30+
31+
// HMAC 서명용 SecretKey 생성
32+
SecretKey secretKey = Keys.hmacShaKeyFor(keyBytes);
33+
34+
assertThat(secretKey).isNotNull();
35+
}
36+
}

0 commit comments

Comments
 (0)