Skip to content

Commit

Permalink
Merge branch 'feat/#147' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	cakey-api/src/main/java/com/cakey/common/filter/RequiredAuthenticationFilter.java
#	cakey-api/src/main/java/com/cakey/user/service/UserService.java
#	cakey-auth/src/main/java/com/cakey/jwt/auth/JwtProvider.java
  • Loading branch information
sjk4618 committed Jan 24, 2025
2 parents 96d98a4 + 9d7eadd commit cec3a22
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 52 deletions.
3 changes: 1 addition & 2 deletions cakey-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ dependencies {
implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'

}

// ์‹คํ–‰๊ฐ€๋Šฅํ•œ jar๋กœ ์ƒ์„ฑํ•˜๋Š” ์˜ต์…˜, main์ด ์—†๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” false๋กœ ๋น„ํ™œ์„ฑํ™”ํ•จ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.cakey.store.domain.Station;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ protected void doFilterInternal(

filterChain.doFilter(request, response); // ๋‹ค์Œ ํ•„ํ„ฐ๋กœ ์š”์ฒญ ์ „๋‹ฌ
} catch (Exception e) {
log.error("--------------------์ฟ ํ‚ค ์—๋Ÿฌ------------------------");
log.error(e.getMessage());

// ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ JSON ์‘๋‹ต ์ƒ์„ฑ
final ErrorBaseCode errorCode = ErrorBaseCode.UNAUTHORIZED;

Expand Down
44 changes: 0 additions & 44 deletions cakey-api/src/main/java/com/cakey/config/CacheConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
import com.cakey.store.service.StoreService;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v1/store")
public class StoreController {

private final StoreService storeService;

//์Šคํ† ์–ด ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ
Expand Down Expand Up @@ -71,6 +72,7 @@ public ResponseEntity<BaseResponse<?>> getStoreInfoListByStationAndLatest(
//์ „์ฒด ์ง€ํ•˜์ฒ ์—ญ ์กฐํšŒ
@GetMapping("/station")
public ResponseEntity<BaseResponse<?>> getAllStation() {
log.error("----------------------testtestsetset---------------"); // todo: ์ถ”ํ›„ ์‚ญ์ œ( ๋กœ๊ทธ ํ…Œ์ŠคํŠธ์šฉ)
return ApiResponseUtil.success(
SuccessCode.OK,
storeService.getAllStation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cloud.openfeign.aot.FeignChildContextInitializer;
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -85,7 +86,7 @@ public LoginSuccessRes login(
} else { //์ „์— ์ด๋ฏธ ์šฐ๋ฆฌ ์œ ์ €

//๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ ์‚ญ์ œ
deleteRefreshToken(userId);
jwtProvider.deleteRefreshToken(userId);

final Token newToken = jwtProvider.issueToken(userId);

Expand Down Expand Up @@ -113,7 +114,7 @@ public void logout(final long userId, final HttpServletResponse response) {
throw new UserNotFoundException(UserErrorCode.USER_NOT_FOUND);
}
deleteRefreshCookie(response);
deleteRefreshToken(userId);
jwtProvider.deleteRefreshToken(userId);
}

@CacheEvict(value = "refresh")
Expand Down
4 changes: 2 additions & 2 deletions cakey-api/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %logger{0} - %msg%n</pattern>
</encoder>
<file>/home/ubuntu/CAKEY-ERROR-LOG.log</file>
<file>*/home/ubuntu/CAKEY-ERROR-LOG.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/ubuntu/CAKEY-ERROR-LOG-%d{yyyy-MM-dd}-%i-log.zip</fileNamePattern>
<fileNamePattern>*/home/ubuntu/CAKEY-ERROR-LOG-%d{yyyy-MM-dd}-%i-log.zip</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
Expand Down
4 changes: 4 additions & 0 deletions cakey-auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ dependencies {

implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0' //feign
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' //oauth2

//cache
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
}
33 changes: 33 additions & 0 deletions cakey-auth/src/main/java/com/cakey/jwt/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cakey.jwt;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableCaching
@Configuration
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<Cache> caches = new ArrayList<>();
caches.add(new CaffeineCache("refresh", Caffeine.newBuilder()
.initialCapacity(100)
.maximumSize(500)
.recordStats()
.build()));
cacheManager.setCaches(caches);
return cacheManager;
}
}
5 changes: 4 additions & 1 deletion cakey-auth/src/main/java/com/cakey/jwt/auth/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.jsonwebtoken.Jws;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.BaseErrorListener;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

Expand All @@ -33,6 +33,9 @@ public String generateRefreshToken(final long userId) {
return jwtGenerator.generateRefreshToken(userId);
}

@CacheEvict(value = "refresh")
public void deleteRefreshToken(final long userId) { }

public long getUserIdFromSubject(final String token) {
Jws<Claims> jws = jwtGenerator.parseToken(token);
String subject = jws.getBody().getSubject();
Expand Down
13 changes: 13 additions & 0 deletions cakey-auth/src/test/java/com/cakey/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cakey;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
@SpringBootApplication
@ComponentScan(basePackages = "com.cakey.jwt") // ์‹ค์ œ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋กœ ์ˆ˜์ •
public class TestConfiguration {
}
100 changes: 100 additions & 0 deletions cakey-auth/src/test/java/com/cakey/jwt/auth/JwtGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.cakey.jwt.auth;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;


@SpringBootTest(properties = {
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
})
@TestPropertySource(properties = {
"jwt.secret=testasdfasdfasddfsecretfdadfsdfasdfasdfasdfasdf",
"jwt.accessTokenExpirationTime=3600",
"jwt.refreshTokenExpirationTime=604800"
})
//@ComponentScan(basePackages = "com.cakey.jwt.auth")
class JwtGeneratorTest {

@Autowired
private JwtGenerator jwtGenerator;

@Autowired
private CacheManager cacheManager;

@Autowired
private JwtProvider jwtProvider;

@Test
@DisplayName("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ์— ๋“ฑ๋ก")
void generateRefreshToken() {
// Given
// ์บ์‹œ ์ดˆ๊ธฐํ™”
cacheManager.getCacheNames().forEach(cacheName -> {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.clear();
}
});

long firstId = 123L;
long secondId = 456L;

final String token1 = jwtGenerator.generateRefreshToken(firstId);
System.out.println("token 1 : " + token1);

final String token2 = jwtGenerator.generateRefreshToken(secondId);
System.out.println("token 2 : " + token2);

// When
final Cache cache = cacheManager.getCache("refresh");

// Then
// ์บ์‹œ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
final String firstCachedToken = cache.get(firstId, String.class);
assertThat(firstCachedToken).isNotNull();
System.out.println("token 1: " + firstCachedToken);
assertThat(firstCachedToken).isEqualTo(token1); // ์บ์‹œ๋œ ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ํ† ํฐ๊ณผ ๋™์ผํ•ด์•ผ ํ•จ

final String secondCachedToken = cache.get(secondId, String.class);
assertThat(secondCachedToken).isNotNull();
System.out.println("token 2: " + secondCachedToken);
assertThat(secondCachedToken).isEqualTo(token2); // ์บ์‹œ๋œ ๊ฐ’์ด ๋‘ ๋ฒˆ์งธ ํ† ํฐ๊ณผ ๋™์ผํ•ด์•ผ ํ•จ
}

@Test
@DisplayName("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ ์‚ญ์ œ")
void deleteRefreshToken() {
//Given
final long userId = 123L;

Cache cache = cacheManager.getCache("refresh");
System.out.println(cache);
assertThat(cache).isNotNull();

String cachedValue = cache.get(userId, String.class);
System.out.println(cachedValue);
assertThat(cachedValue).isNotNull();

//When
jwtProvider.deleteRefreshToken(userId);

//Then
cache = cacheManager.getCache("refresh");
System.out.println(cache);

cachedValue = cache.get(userId, String.class);
System.out.println(cachedValue);

Assertions.assertThat(cachedValue).isNull(); // ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์‚ญ์ œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
}
}

0 comments on commit cec3a22

Please sign in to comment.