-
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.
- BCryptHashEncrypter -> HashService 변경
- Loading branch information
Showing
27 changed files
with
498 additions
and
89 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/airjnc/common/annotation/TwoFieldMatch.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,37 @@ | ||
package com.airjnc.common.annotation; | ||
|
||
import com.airjnc.common.util.beanvalidator.TwoFieldMatchValidator; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
// DTO 바인딩 검사에서 선택된 두 필드에 대하여 해당 두 필드의 값이 동일한 지 체크하는 애노테이션입니다. | ||
// 향후 사용할 가능성이 있을 것 같아, 현재 사용하진 않지만 지우지 않고 남겨둡니다. | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = TwoFieldMatchValidator.class) | ||
public @interface TwoFieldMatch { | ||
|
||
String first(); | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
String message() default "{TwoFieldMatch}"; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
String second(); | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
TwoFieldMatch[] value(); | ||
} | ||
} |
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,16 @@ | ||
package com.airjnc.common.service; | ||
|
||
import org.mindrot.jbcrypt.BCrypt; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class HashService { | ||
|
||
public String encrypt(String plain) { | ||
return BCrypt.hashpw(plain, BCrypt.gensalt()); | ||
} | ||
|
||
public boolean isMatch(String plain, String hash) { | ||
return BCrypt.checkpw(plain, hash); | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/com/airjnc/common/util/BCryptHashEncrypter.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
src/main/java/com/airjnc/common/util/beanvalidator/TwoFieldMatchValidator.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,68 @@ | ||
package com.airjnc.common.util.beanvalidator; | ||
|
||
import com.airjnc.common.annotation.TwoFieldMatch; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class TwoFieldMatchValidator implements ConstraintValidator<TwoFieldMatch, Object> { | ||
|
||
private String firstFieldName; | ||
|
||
private String secondFieldName; | ||
|
||
private String message; | ||
|
||
@Override | ||
public void initialize(TwoFieldMatch constraintAnnotation) { | ||
firstFieldName = constraintAnnotation.first(); | ||
secondFieldName = constraintAnnotation.second(); | ||
message = constraintAnnotation.message(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object value, ConstraintValidatorContext context) { | ||
boolean isValid = true; | ||
try { | ||
Method firstMethod = value.getClass().getMethod("get" + replaceFirstToUpperCase(firstFieldName)); | ||
Method secondMethod = value.getClass().getMethod("get" + replaceFirstToUpperCase(secondFieldName)); | ||
|
||
Object firstObj = firstMethod.invoke(value); | ||
Object secondObj = secondMethod.invoke(value); | ||
|
||
isValid = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
if (!isValid) { | ||
context.buildConstraintViolationWithTemplate(message) | ||
/* | ||
* `addPropertNode` -> `@TwoFieldMatch` 를 사용할 경우, 필드 접근 방법을 정의한다. | ||
* Ex) `@TwoFieldMatch class Ex { String a; }` 일 경우, `addPropertyNode("a")` 해주면 된다. | ||
* Ex) `@TwoFieldMatch class Ex { Ex2 ex2; } class Ex2{ String a; }` 일 경우, `addPropertyNode("ex2").addPropertyNode("a")` 해주면 된다. | ||
*/ | ||
.addPropertyNode(secondFieldName) | ||
/* | ||
* 에러 메시지[message]와 node key[secondFieldName] 값을 넘겨주며, 해당 node는 errors[].field에 바인딩된다. | ||
* `addConstraintViolation` 를 사용할 경우, fieldError에 담긴다. | ||
* `addConstraintViolation` 를 사용하지 않고 `context.buildConstraintViolationWithTemplate(message);` 만 사용할 경우, global error에 담긴다. | ||
*/ | ||
.addConstraintViolation() | ||
// `addConstraintViolation` 를 통해 field error을 담았고, `disableDefaultConstraintViolation` 을 통해 global error을 제거한다. | ||
.disableDefaultConstraintViolation(); | ||
} | ||
return isValid; | ||
} | ||
|
||
private String replaceFirstToUpperCase(String str) { | ||
char c = Character.toUpperCase(str.charAt(0)); | ||
StringBuilder sb = new StringBuilder(); | ||
int length = str.length(); | ||
sb.append(c); | ||
for (int i = 1; i < length; i++) { | ||
sb.append(str.charAt(i)); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
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
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
Oops, something went wrong.