spring security 5.x實(shí)現(xiàn)兼容多種密碼的加密方式
前言
本文主要給大家介紹了關(guān)于spring security 5.x實(shí)現(xiàn)兼容多種密碼的加密方式,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
1、spring security PasswordEncoder
spring security 5不需要配置密碼的加密方式,而是用戶密碼加前綴的方式表明加密方式,如:
- {MD5}88e2d8cd1e92fd5544c8621508cd706b代表使用的是MD5加密方式;
- {bcrypt}$2a$10$eZeGvVV2ZXr/vgiVFzqzS.JLV878ApBgRT9maPK1Wrg0ovsf4YuI6代表使用的是bcrypt加密方式。
spring security官方推薦使用更加安全的bcrypt加密方式。
這樣可以在同一系統(tǒng)中支持多種加密方式,遷移用戶比較省事。spring security 5支持的加密方式在PasswordEncoderFactories中定義:
public class PasswordEncoderFactories {
public static PasswordEncoder createDelegatingPasswordEncoder() {
String encodingId = "bcrypt";
Map<String, PasswordEncoder> encoders = new HashMap();
encoders.put(encodingId, new BCryptPasswordEncoder());
encoders.put("ldap", new LdapShaPasswordEncoder());
encoders.put("MD4", new Md4PasswordEncoder());
encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
encoders.put("noop", NoOpPasswordEncoder.getInstance());
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
encoders.put("scrypt", new SCryptPasswordEncoder());
encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
encoders.put("sha256", new StandardPasswordEncoder());
return new DelegatingPasswordEncoder(encodingId, encoders);
}
private PasswordEncoderFactories() {
}
}
2 測(cè)試
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hfcsbc</groupId>
<artifactId>security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>security</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
2.2 測(cè)試
spring security 5.x默認(rèn)使用bcrypt加密
@Slf4j
public class DomainUserDetailsService {
public static void main(String[] args){
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
String encode = passwordEncoder.encode("password");
log.info("加密后的密碼:" + encode);
log.info("bcrypt密碼對(duì)比:" + passwordEncoder.matches("password", encode));
String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5加密前的密碼為:password
log.info("MD5密碼對(duì)比:" + passwordEncoder.matches("password", encode));
}
}

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java實(shí)現(xiàn)字符串與字節(jié)數(shù)組之間相互轉(zhuǎn)換
在Java編程中,字符串(String)和字節(jié)數(shù)組(byte[])之間的相互轉(zhuǎn)換是非常常見(jiàn)的操作,這種轉(zhuǎn)換在網(wǎng)絡(luò)編程、文件處理、加密解密等場(chǎng)景中尤為重要,本文將詳細(xì)介紹如何在Java中實(shí)現(xiàn)字符串與字節(jié)數(shù)組之間的相互轉(zhuǎn)換,需要的朋友可以參考下2025-02-02
基于JavaScript動(dòng)態(tài)規(guī)劃編寫(xiě)一個(gè)益智小游戲
最近在學(xué)習(xí)動(dòng)態(tài)規(guī)劃相關(guān)的知識(shí),所以本文將利用動(dòng)態(tài)規(guī)劃編寫(xiě)一個(gè)簡(jiǎn)單的益智小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06
Java實(shí)現(xiàn)的DES加密解密工具類實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的DES加密解密工具類,結(jié)合具體實(shí)例形式分析了Java實(shí)現(xiàn)的DES加密解密工具類定義與使用方法,需要的朋友可以參考下2017-09-09
Springboot集成JSR303參數(shù)校驗(yàn)的方法實(shí)現(xiàn)
這篇文章主要介紹了Springboot集成JSR303參數(shù)校驗(yàn)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(48)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08
Jackson java動(dòng)態(tài)去除返回json中的值方式
文章介紹了在Java中使用@JsonInclude注解動(dòng)態(tài)去除返回JSON中的非必需字段(如分頁(yè)信息)的解決方案,通過(guò)在字段上添加@JsonInclude注解并選擇合適的策略(如NON_NULL或NON_EMPTY),可以在非分頁(yè)情況下取消分頁(yè)字段,從而提高返回結(jié)果的靈活性和效率2024-12-12
MyBatisPlus3如何向數(shù)據(jù)庫(kù)中存入List
本文主要介紹了Mybatis Plus的類型處理器的使用,通過(guò)User.java和UserMapper.xml示例進(jìn)行詳細(xì)的解析,并提供了JSON解析器的使用方法,希望通過(guò)這篇文章,可以幫助大家更好的理解和掌握Mybatis Plus的類型處理器2024-10-10
Java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法
今天需要寫(xiě)一個(gè)導(dǎo)出的Excel的功能,但是發(fā)現(xiàn)當(dāng)數(shù)據(jù)量到3萬(wàn)條時(shí),列數(shù)在23列時(shí),內(nèi)存溢出,CPU使用100%,測(cè)試環(huán)境直接炸掉。小編給大家分享基于java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法,感興趣的朋友一起看看吧2019-11-11
SpringBoot配置默認(rèn)HikariCP數(shù)據(jù)源
咱們開(kāi)發(fā)項(xiàng)目的過(guò)程中用到很多的開(kāi)源數(shù)據(jù)庫(kù)鏈接池,比如druid、c3p0、BoneCP等等,本文主要介紹了SpringBoot配置默認(rèn)HikariCP數(shù)據(jù)源,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11

