-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 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
Showing
12 changed files
with
167 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
100
cakey-auth/src/test/java/com/cakey/jwt/auth/JwtGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); // ํค์ ํด๋นํ๋ ๊ฐ์ด ์ญ์ ๋์๋์ง ํ์ธ | ||
} | ||
} |