Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#24 [feat] 인증 코드 redis 서비스 구현 #91

Closed
wants to merge 12 commits into from
13 changes: 13 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ jobs:
distribution: 'corretto'
java-version: '17'

- name: Setup MySQL
uses: samin/mysql-action@v1
with:
character set server: 'utf8'
mysql database: 'moddy'
mysql user: 'moddy'
mysql password: ${{ secrets.DatabasePassword }}

- name: Start Redis
uses: supercharge/[email protected]
with:
redis-version: 6

- name: application.yml 생성
run: |
cd src/main/resources
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/moddy/server/config/redis/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.moddy.server.config.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private int port;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.moddy.server.config.redis;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor
public class VerificationCodeRedisService {
private final RedisTemplate<String, String> redisTemplate;
private static final int EXPIRE_VERIFICATION_CODE_TIME = 3;

public void saveVerificationCode(String phoneNumber, String verificationCode) {
ValueOperations<String, String> values = redisTemplate.opsForValue();
values.set(phoneNumber, verificationCode);
redisTemplate.expire(phoneNumber, EXPIRE_VERIFICATION_CODE_TIME, TimeUnit.MINUTES);
}

public boolean isVerificationCodeEmpty(String phoneNumber) {
ValueOperations<String, String> values = redisTemplate.opsForValue();
return values.get(phoneNumber) == null;
}

public String getVerificationCode(String phoneNumber) {
ValueOperations<String, String> values = redisTemplate.opsForValue();
return values.get(phoneNumber);
}

public void deleteVerificationCode(String phoneNumber) {
redisTemplate.delete(phoneNumber);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.moddy.server.config.redis;

//@SpringBootTest
class VerificationCodeRedisServiceTest {
// @Autowired
// private VerificationCodeRedisService verificationCodeRedisService;
//
// @Test
// @DisplayName("전화 번호를 키로 사용하여 인증 코드를 저장할 수 있다.")
// public void saveVerificationCodeTest() {
// // given
// String phoneNumber = "01000000000";
// String verificationCode = "123456";
//
// // when
// verificationCodeRedisService.saveVerificationCode(phoneNumber, verificationCode);
//
// // then
// String response = verificationCodeRedisService.getVerificationCode(phoneNumber);
// assertThat(response).isEqualTo(verificationCode);
// }
//
// @Test
// @DisplayName("특정 전화 번호의 값을 제거할 수 있다.")
// public void deleteVerificationCodeTest() {
// // given
// String phoneNumber = "01000000000";
// String verificationCode = "123456";
// verificationCodeRedisService.saveVerificationCode(phoneNumber, verificationCode);
//
// // when
// verificationCodeRedisService.deleteVerificationCode(phoneNumber);
//
// // then
// boolean response = verificationCodeRedisService.isVerificationCodeEmpty(phoneNumber);
// assertThat(response).isTrue();
// }
}
Loading