阿超
>
BCryptPasswordEncoder
今眼前的迷雾已然消散,自由、祖国,唯有你们才是我的信念。——雨果
在web
开发中我们可以使用org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
来进行密码加密
这里写一个Demo
说明一下基本姿势
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.ruben;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;
public class BCryptPasswordEncoderDemo { public static void main(String[] args) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); List<String> pwdList = Stream.generate(() -> encoder.encode("ruben")).limit(10).collect(Collectors.toList()); for (int i = 4; i > 0; i--) { Collections.shuffle(pwdList); String pwd = pwdList.stream().findAny().orElseThrow(() -> new RuntimeException("It can not happen!")); System.out.println(pwd); System.out.println(encoder.matches("ruben", pwd)); } } }
|
最后打印结果