小编这次要给大家分享的是Spring security如何实现密码加密,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
xml配置如下
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
BCryptPasswordEncoder相关知识:
用户表的密码通常使用MD5等不可逆算法加密后存储,为防止彩虹表破解更会先使用一个特定的字符串(如域名)加密,然后再使用一个随机的salt(盐值)加密。
特定字符串是程序代码中固定的,salt是每个密码单独随机,一般给用户表加一个字段单独存储,比较麻烦。
BCrypt算法将salt随机并混入最终加密后的密码,验证时也无需单独提供之前的salt,从而无需单独处理salt问题。
代码如下
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class Test {
public static void main(String[] args) {
// springsecurity 注册加密方法
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String encode = bCryptPasswordEncoder.encode("1");
System.out.println(encode);
//$2a$10$H2HTe3SVdKMk8ewC3gRKouva7U6DAQspHqyhcdg805JGHAApV1Wci
//$2a$10$Iz4Y52GmirUf5SRW6jTIA.0cgaS0mKTYZVN2cFFeK8DXk9YHVhJDW
// springsecurity 登录加密方法
BCrypt bCrypt = new BCrypt();
String hashpw = bCrypt.hashpw("1", "$2a$10$Iz4Y52GmirUf5SRW6jTIA.0cgaS0mKTYZVN2cFFeK8DXk9YHVhJDW");
System.out.println(hashpw);
}
}
看完这篇关于Spring security如何实现密码加密的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。