Skip to content

Commit

Permalink
Merge pull request #103 from Seasoning-Today/crypto-ver2
Browse files Browse the repository at this point in the history
기록장 암호화 재적용 외 기타 작업
  • Loading branch information
csct3434 authored Mar 27, 2024
2 parents c795249 + 9d541bd commit 6686a8c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand All @@ -13,6 +14,9 @@
@SpringBootApplication
public class SeasoningApplication {

@Value("${spring.datasource.url}")
private String dataSourceUrl;

public static void main(String[] args) {
SpringApplication.run(SeasoningApplication.class, args);
}
Expand All @@ -23,4 +27,9 @@ void setTimeZone() {
log.info("Server Time : {}", LocalDateTime.now());
}

@PostConstruct
void logDatasourceUrl() {
log.info("Datasource URL : {}", dataSourceUrl);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
Expand All @@ -20,6 +21,7 @@
import org.hibernate.annotations.OnDeleteAction;
import today.seasoning.seasoning.article.dto.RegisterArticleCommand;
import today.seasoning.seasoning.common.BaseTimeEntity;
import today.seasoning.seasoning.common.cipher.CryptoConverter;
import today.seasoning.seasoning.common.util.TsidUtil;
import today.seasoning.seasoning.solarterm.domain.SolarTerm;
import today.seasoning.seasoning.user.domain.User;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class Article extends BaseTimeEntity {
private int createdTerm;

@Lob
@Convert(converter = CryptoConverter.class)
private String contents;

@OneToMany(mappedBy = "article", cascade = {CascadeType.REMOVE, CascadeType.PERSIST}, orphanRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public String encode(String plainText) {
if (plainText == null) {
return null;
}

try {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(plainText.getBytes(UTF_8));
Expand All @@ -45,7 +44,6 @@ public String decode(String encodedText) {
if (encodedText == null) {
return null;
}

try {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decoded = cipher.doFinal(Base64.decodeBase64(encodedText));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package today.seasoning.seasoning.common.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -68,7 +70,9 @@ public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleUnexpectedException(Exception e) {
logger.error("Exception: {}", e.getMessage());
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.error(errors.toString());
ErrorResponse errorResponse = new ErrorResponse("예상치 못한 오류 발생");
return ResponseEntity.badRequest().body(errorResponse);
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/today/seasoning/seasoning/CipherUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package today.seasoning.seasoning;

import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import today.seasoning.seasoning.common.cipher.CipherUtil;

public class CipherUtilTest extends BaseIntegrationTest {

@Autowired
CipherUtil cipherUtil;

@InjectSoftAssertions
SoftAssertions softAssertions;

@Test
@DisplayName("암복호화 테스트")
void test() {
try {
String content = "abc";
String encoded = cipherUtil.encode(content);
String decoded = cipherUtil.decode(encoded);
softAssertions.assertThat(content).isNotEqualTo(encoded);
softAssertions.assertThat(decoded).isEqualTo(content);
} catch (Exception e) {
softAssertions.fail(e.getMessage());
}
}

}

0 comments on commit 6686a8c

Please sign in to comment.