Skill/spring

[JAVA] Java 비밀번호 정규식 / ID 동일문자 / 같은 문자 / 연속 문자 / 연속 숫자 공통 Util 소스

진열사랑 2022. 12. 5. 20:40

출처: https://ohtanja.tistory.com/m/18

영문/숫자/특수문자 사용
영문, 숫자
영문,  특수문자
특수문자, 숫자
같은문자, 숫자
인지 체크하는 변수를 선언한다.

public static final String p1 = "^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[$@$!%*#?&])[A-Za-z[0-9]$@$!%*#?&]{8,20}$";
public static final String p2 = "^[A-Za-z[0-9]]{10,20}$";
public static final String p3 = "^[[0-9]$@$!%*#?&]{10,20}$";
public static final String p4 = "^[[A-Za-z]$@$!%*#?&]{10,20}$";
public static final String p5 = "(\\w)\\1\\1"; Matcher m;

기존 비밀번호와 새 비밀번호가 같으면 리턴한다.
새 비밀번호의 정규식 패턴을 확인한다.

public boolean passwordChk(String newPassword, String oldPassword, String userId) {
    boolean check = false;
    m =  Pattern.compile(p1).matcher(newPassword); if(m.find()) { check = true; } m = Pattern.compile(p2).matcher(newPassword); if(m.find()) { check = true; } m = Pattern.compile(p3).matcher(newPassword); if(m.find()) { check = true; } m = Pattern.compile(p4).matcher(newPassword); if(m.find()) { chk = true; } if(check) { if(samePwd(newPassword)) { return false; } if(continuousPwd(newPassword)) { return false; } if(newPassword.equals(oldPassword)) { return false; } if(sameId(newPassword, userId)) { return false; } } return true; }