spring boot正则表达式匹配bcrypt
假设要匹配的密文:
A$2a$10$Cih2shiBNg5jWrj0i.2hbuzZ5.g9T6caaxNP4yYtp3.wpi48rXomu
代码如下:
Pattern BCRYPT_PATTERN = Pattern .compile("\\A\\$2a?\\$"); // A$2a$10$Cih2shiBNg5jWrj0i.2hbuzZ5.g9T6caaxNP4yYtp3.wpi48rXomu if (!BCRYPT_PATTERN.matcher("A$2a$").matches()) { log.warn("Encoded password does not look like BCrypt"); }else { log.info("匹配"); } String str = "$2a$10$Cih2shiBNg5jWrj0i.2hbuzZ5.g9T6caaxNP4yYtp3.wpi48rXomu"; String pattern = "\\A\\$2a?\\$\\d\\d\\$[./0-9A-Za-z]{53}"; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(str); System.out.println("匹配 = {}" + m.matches());