国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

spring security 5.x實現兼容多種密碼的加密方式

2024-07-13 10:17:26
字體:
來源:轉載
供稿:網友

前言

本文主要給大家介紹了關于spring security 5.x實現兼容多種密碼的加密方式,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

1、spring security PasswordEncoder

spring security 5不需要配置密碼的加密方式,而是用戶密碼加前綴的方式表明加密方式,如:

  • {MD5}88e2d8cd1e92fd5544c8621508cd706b代表使用的是MD5加密方式;
  • {bcrypt}$2a$10$eZeGvVV2ZXr/vgiVFzqzS.JLV878ApBgRT9maPK1Wrg0ovsf4YuI6代表使用的是bcrypt加密方式。

spring security官方推薦使用更加安全的bcrypt加密方式。

這樣可以在同一系統中支持多種加密方式,遷移用戶比較省事。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 測試

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 測試

spring security 5.x默認使用bcrypt加密

@Slf4jpublic class DomainUserDetailsService { public static void main(String[] args){  PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();  String encode = passwordEncoder.encode("password");  log.info("加密后的密碼:" + encode);  log.info("bcrypt密碼對比:" + passwordEncoder.matches("password", encode));  String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5加密前的密碼為:password  log.info("MD5密碼對比:" + passwordEncoder.matches("password", encode)); }}

spring,security,加密,密碼,密碼加密

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汶上县| 淄博市| 东辽县| 绥江县| 靖江市| 宁陕县| 三河市| 永春县| 宕昌县| 荣成市| 大关县| 虹口区| 沙田区| 沾益县| 长寿区| 万年县| 湘潭市| 安宁市| 德阳市| 海原县| 六安市| 阳谷县| 沙洋县| 蒙城县| 洪泽县| 永济市| 晋宁县| 永兴县| 高清| 游戏| 连州市| 南安市| 简阳市| 阳西县| 高台县| 砀山县| 沙坪坝区| 惠来县| 闵行区| 天全县| 芦溪县|