issue # validator, converter 구현 중
This commit is contained in:
@@ -10,6 +10,6 @@ public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
return new BCryptPasswordEncoder(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,20 @@ package com.baekyangdan.scheduler.repository.user;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baekyangdan.scheduler.entity.user.UserEntity;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<UserEntity, String> {
|
||||
Optional<UserEntity> findByEmail(String email);
|
||||
Optional<UserEntity> findById(String id);
|
||||
|
||||
@Transactional
|
||||
@Modifying(clearAutomatically = true)
|
||||
@Query("UPDATE UserEntity u SET u.password = :password WHERE u.email = :email")
|
||||
int resetPasswordByEmail(@Param("password") String newPassword, @Param("email") String email);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baekyangdan.scheduler.utils.converter;
|
||||
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PasswordConverter {
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public PasswordConverter(PasswordEncoder passwordEncoder) {
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
public String encode(String rawPassword) {
|
||||
return passwordEncoder.encode(rawPassword);
|
||||
}
|
||||
|
||||
public boolean matches(String rawPassword, String encodedPassword) {
|
||||
return passwordEncoder.matches(rawPassword, encodedPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baekyangdan.scheduler.utils.generator;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PasswordGenerator {
|
||||
private static final String DIGITS = "0123456789";
|
||||
private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
|
||||
private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
private static final String SPECIAL="!@#$";
|
||||
|
||||
private static final String ALL = DIGITS + LOWER + UPPER + SPECIAL;
|
||||
private static final SecureRandom random = new SecureRandom();
|
||||
|
||||
public static String generateRandomPassword() {
|
||||
int length = 10;
|
||||
|
||||
List<Character> passwordCharacters = new ArrayList<>(length);
|
||||
|
||||
passwordCharacters.add(randomCharFrom(DIGITS));
|
||||
passwordCharacters.add(randomCharFrom(LOWER));
|
||||
passwordCharacters.add(randomCharFrom(UPPER));
|
||||
passwordCharacters.add(randomCharFrom(SPECIAL));
|
||||
|
||||
for (int i = 4; i < length; i++) {
|
||||
passwordCharacters.add(randomCharFrom(ALL));
|
||||
}
|
||||
|
||||
Collections.shuffle(passwordCharacters, random);
|
||||
|
||||
StringBuilder sb = new StringBulilder(length);
|
||||
|
||||
for (char c : passwordCharacters) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static char randomCharFrom(String s) {
|
||||
int idx = random.nextInt(s.length());
|
||||
return s.charAt(idx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baekyangdan.scheduler.utils.validation;
|
||||
|
||||
public class BaseValidation {
|
||||
private String emailRegExp = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,}$"
|
||||
|
||||
protected static boolean validateStringEmpty(String value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
if (value.trim().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -5,29 +5,33 @@ import org.springframework.security.access.method.P;
|
||||
import com.baekyangdan.scheduler.code.user.SignUpValidationCode;
|
||||
import com.baekyangdan.scheduler.repository.user.UserRepository;
|
||||
import com.baekyangdan.scheduler.request.user.UserRequest;
|
||||
import com.baekyangdan.scheduler.utils.validation.BaseValidation;
|
||||
|
||||
public class UserValidation {
|
||||
public class UserValidation extends BaseValidation {
|
||||
|
||||
public static boolean validateSignUpForm(UserRequest.SignUp data) throws Exception {
|
||||
|
||||
if (validateStringEmpty(data.getId())) {
|
||||
throw new Exception(SignUpValidationCode.ID_EMPTY.getMessage());
|
||||
}
|
||||
if (data.getEmail() == null || data.getEmail().trim().isEmpty()) {
|
||||
|
||||
|
||||
if (validateStringEmpty(data.getEmail())) {
|
||||
throw new Exception(SignUpValidationCode.EMAIL_EMPTY.getMessage());
|
||||
}
|
||||
|
||||
if (validationStringEmpty(data.getPassword())) {
|
||||
throw new Exception(SignUpValidationCode.PASSWORD_EMPTY.getMessage());
|
||||
}
|
||||
|
||||
if (validationStringEmpty(data.getName())) {
|
||||
throw new Exception(SignUpValidationCode.NAME_EMPTY.getMessage());
|
||||
}
|
||||
|
||||
if (validationStringEmpty(data.getNickname())) {
|
||||
throw new Exception(SignUpValidationCode.NICKNAME_EMPTY.getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean validateStringEmpty(String value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value.trim().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user