Skip to content

Commit c971c2e

Browse files
authored
Merge pull request #118 from Recipe-Project/feature/dev_improvements
Feature/dev improvements
2 parents 08780bd + f8c4861 commit c971c2e

18 files changed

Lines changed: 311 additions & 89 deletions

File tree

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ dependencies {
6262
implementation group: 'com.google.http-client', name: 'google-http-client-jackson2', version: '1.25.0'
6363
implementation group: 'com.google.collections', name: 'google-collections', version: '1.0'
6464

65-
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
65+
implementation 'org.springframework.boot:spring-boot-starter-cache'
66+
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
6667

6768
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '4.1.3'
6869

src/main/java/com/recipe/app/src/common/client/apple/dto/AppleAuthResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.recipe.app.src.common.client.apple.dto;
22

3+
import com.recipe.app.src.user.domain.NicknameGenerator;
34
import com.recipe.app.src.user.domain.User;
45
import lombok.Builder;
56
import lombok.Getter;
@@ -16,7 +17,7 @@ public User toEntity(String fcmToken) {
1617

1718
return User.builder()
1819
.socialId("apple_" + sub)
19-
.nickname(name != null ? name : "Apple User")
20+
.nickname(NicknameGenerator.generate())
2021
.email(email)
2122
.deviceToken(fcmToken)
2223
.build();

src/main/java/com/recipe/app/src/common/client/google/dto/GoogleAuthResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.recipe.app.src.common.client.google.dto;
22

3+
import com.recipe.app.src.user.domain.NicknameGenerator;
34
import com.recipe.app.src.user.domain.User;
45
import lombok.Getter;
56

@@ -14,7 +15,7 @@ public User toEntity(String fcmToken) {
1415

1516
return User.builder()
1617
.socialId("google_" + sub)
17-
.nickname(name)
18+
.nickname(NicknameGenerator.generate())
1819
.email(email)
1920
.deviceToken(fcmToken)
2021
.build();

src/main/java/com/recipe/app/src/common/client/kakao/dto/KakaoAuthResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.recipe.app.src.common.client.kakao.dto;
22

3+
import com.recipe.app.src.user.domain.NicknameGenerator;
34
import com.recipe.app.src.user.domain.User;
45
import lombok.Getter;
56

@@ -13,7 +14,7 @@ public User toEntity(String fcmToken) {
1314

1415
return User.builder()
1516
.socialId("kakao_" + id)
16-
.nickname(kakao_account.getNickname())
17+
.nickname(NicknameGenerator.generate())
1718
.email(kakao_account.getEmail())
1819
.deviceToken(fcmToken)
1920
.build();

src/main/java/com/recipe/app/src/common/client/naver/dto/NaverAuthInfoResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.recipe.app.src.common.client.naver.dto;
22

3+
import com.recipe.app.src.user.domain.NicknameGenerator;
34
import com.recipe.app.src.user.domain.User;
45
import lombok.Getter;
56

@@ -15,7 +16,7 @@ public User toEntity(String fcmToken) {
1516

1617
return User.builder()
1718
.socialId("naver_" + id)
18-
.nickname(name)
19+
.nickname(NicknameGenerator.generate())
1920
.email(email)
2021
.phoneNumber(mobile)
2122
.deviceToken(fcmToken)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.recipe.app.src.common.config;
2+
3+
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
@Configuration
10+
public class CacheConfig {
11+
12+
/**
13+
* Access Token 블랙리스트 전용 Caffeine Cache
14+
* JWT exp와 동일하게 24시간 유지
15+
*/
16+
@Bean(name = "accessTokenBlacklistCache")
17+
public com.github.benmanes.caffeine.cache.Cache<String, String> accessTokenBlacklistCache() {
18+
return Caffeine.newBuilder()
19+
.expireAfterWrite(24, TimeUnit.HOURS)
20+
.maximumSize(10000)
21+
.build();
22+
}
23+
}

src/main/java/com/recipe/app/src/common/config/RedisConfig.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/com/recipe/app/src/common/config/WebSecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5050
.requestMatchers("/fridges/**").authenticated()
5151
.requestMatchers("/fridges/basket/**").authenticated()
5252
.requestMatchers("/ingredients/**").authenticated()
53+
.requestMatchers("/recipes/public/**").permitAll()
5354
.requestMatchers("/recipes/**").authenticated()
5455
.anyRequest().permitAll())
5556
.addFilterBefore(new JwtFilter(jwtUtil, userDetailsService), UsernamePasswordAuthenticationFilter.class)

src/main/java/com/recipe/app/src/common/utils/JwtUtil.java

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
package com.recipe.app.src.common.utils;
22

3+
import com.github.benmanes.caffeine.cache.Cache;
34
import com.recipe.app.src.common.client.apple.dto.ApplePublicKeyResponse;
4-
import io.jsonwebtoken.*;
5+
import io.jsonwebtoken.Claims;
6+
import io.jsonwebtoken.ExpiredJwtException;
7+
import io.jsonwebtoken.Jws;
8+
import io.jsonwebtoken.Jwts;
9+
import io.jsonwebtoken.MalformedJwtException;
10+
import io.jsonwebtoken.SignatureAlgorithm;
11+
import io.jsonwebtoken.UnsupportedJwtException;
512
import io.jsonwebtoken.security.SignatureException;
613
import jakarta.servlet.http.HttpServletRequest;
714
import org.slf4j.Logger;
815
import org.slf4j.LoggerFactory;
16+
import org.springframework.beans.factory.annotation.Qualifier;
917
import org.springframework.beans.factory.annotation.Value;
10-
import org.springframework.data.redis.core.RedisTemplate;
1118
import org.springframework.stereotype.Service;
12-
import org.springframework.transaction.annotation.Transactional;
13-
import org.springframework.util.StringUtils;
1419

1520
import javax.crypto.spec.SecretKeySpec;
1621
import java.math.BigInteger;
1722
import java.security.Key;
1823
import java.security.KeyFactory;
1924
import java.security.PublicKey;
2025
import java.security.spec.RSAPublicKeySpec;
21-
import java.time.Duration;
2226
import java.util.Base64;
2327
import java.util.Date;
2428

2529
@Service
2630
public class JwtUtil {
2731

28-
private final RedisTemplate<String, String> redisTemplate;
32+
private final Cache<String, String> accessTokenBlacklistCache;
2933
private final Logger logger = LoggerFactory.getLogger(JwtUtil.class);
3034
private final static String TOKEN_KEY = "userId";
31-
private final static String REFRESH_TOKEN_KEY_PREFIX = "refresh_token_user_id_";
3235
private final static String ACCESS_TOKEN_BLACKLIST_VALUE = "access_token_blacklist";
3336
private final static String TOKEN_HEADER = "Authorization";
3437
@Value("${jwt.secret}")
@@ -38,8 +41,8 @@ public class JwtUtil {
3841
@Value("${jwt.refresh-token-validity-in-ms}")
3942
private long refreshTokenValidMillisecond;
4043

41-
public JwtUtil(RedisTemplate<String, String> redisTemplate) {
42-
this.redisTemplate = redisTemplate;
44+
public JwtUtil(@Qualifier("accessTokenBlacklistCache") Cache<String, String> accessTokenBlacklistCache) {
45+
this.accessTokenBlacklistCache = accessTokenBlacklistCache;
4346
}
4447

4548
public String createAccessToken(Long userId) {
@@ -60,16 +63,12 @@ public String createRefreshToken(Long userId) {
6063
Date now = new Date();
6164
Key key = new SecretKeySpec(Base64.getDecoder().decode(this.secretKey), SignatureAlgorithm.HS256.getJcaName());
6265

63-
String token = Jwts.builder()
66+
return Jwts.builder()
6467
.claim(TOKEN_KEY, userId)
6568
.setIssuedAt(now)
6669
.setExpiration(new Date(now.getTime() + refreshTokenValidMillisecond))
6770
.signWith(key)
6871
.compact();
69-
70-
redisTemplate.opsForValue().set(REFRESH_TOKEN_KEY_PREFIX + userId, token, Duration.ofMillis(refreshTokenValidMillisecond));
71-
72-
return token;
7372
}
7473

7574
public String resolveAccessToken(HttpServletRequest request) {
@@ -89,10 +88,9 @@ public long getUserId(String token) {
8988
.get(TOKEN_KEY, Long.class);
9089
}
9190

92-
@Transactional(readOnly = true)
9391
public boolean isValidAccessToken(String accessToken) {
9492

95-
if (StringUtils.hasText(redisTemplate.opsForValue().get(accessToken))) {
93+
if (accessTokenBlacklistCache.getIfPresent(accessToken) != null) {
9694
return false;
9795
}
9896

@@ -101,15 +99,7 @@ public boolean isValidAccessToken(String accessToken) {
10199

102100
public boolean isValidRefreshToken(String refreshToken) {
103101

104-
if (isValidToken(refreshToken)) {
105-
106-
long userId = getUserId(refreshToken);
107-
String foundRefreshToken = redisTemplate.opsForValue().get(REFRESH_TOKEN_KEY_PREFIX + userId);
108-
109-
return refreshToken.equals(foundRefreshToken);
110-
}
111-
112-
return false;
102+
return isValidToken(refreshToken);
113103
}
114104

115105
private boolean isValidToken(String token) {
@@ -129,16 +119,9 @@ private boolean isValidToken(String token) {
129119
return false;
130120
}
131121

132-
@Transactional
133-
public void removeRefreshToken(Long userId) {
134-
135-
redisTemplate.delete(REFRESH_TOKEN_KEY_PREFIX + userId);
136-
}
137-
138-
@Transactional
139122
public void setAccessTokenBlacklist(String accessToken) {
140123

141-
redisTemplate.opsForValue().set(accessToken, ACCESS_TOKEN_BLACKLIST_VALUE, Duration.ofMillis(accessTokenValidMillisecond));
124+
accessTokenBlacklistCache.put(accessToken, ACCESS_TOKEN_BLACKLIST_VALUE);
142125
}
143126

144127
public Claims parseAppleIdToken(String idToken, ApplePublicKeyResponse publicKey) {

src/main/java/com/recipe/app/src/file/S3FileService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public S3FileService(AmazonS3 s3Client) {
2525

2626
public String uploadFile(MultipartFile file) throws IOException {
2727

28-
String fileName = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
28+
String fileName = UUID.randomUUID() + "-" + file.getOriginalFilename();
2929
ObjectMetadata objectMetadata = new ObjectMetadata();
3030
objectMetadata.setContentType(file.getContentType());
3131
objectMetadata.setContentLength(file.getSize());

0 commit comments

Comments
 (0)