亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring security 5.x實現(xiàn)兼容多種密碼的加密方式

 更新時間:2018年01月28日 11:39:50   作者:彭超  
spring security針對該功能有兩種實現(xiàn)方式,一種是簡單的使用加密來保證基于 cookie 的 token 的安全,另一種是通過數(shù)據(jù)庫或其它持久化存儲機制來保存生成的 token。這篇文章主要給大家介紹了關于spring security 5.x實現(xiàn)兼容多種密碼的加密方式,需要的朋友可以參考下。

前言

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

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

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加密

@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密碼對比:" + passwordEncoder.matches("password", encode));
  String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5加密前的密碼為:password
  log.info("MD5密碼對比:" + passwordEncoder.matches("password", encode));
 }
}

總結(jié)

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

相關文章

  • java實現(xiàn)停車場管理系統(tǒng)

    java實現(xiàn)停車場管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)停車場管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • java網(wǎng)絡爬蟲連接超時解決實例代碼

    java網(wǎng)絡爬蟲連接超時解決實例代碼

    這篇文章主要介紹了java網(wǎng)絡爬蟲連接超時解決的問題,分享了一則使用httpclient解決連接超時的Java爬蟲實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • IDEA 自帶的數(shù)據(jù)庫工具真的很牛(收藏版)

    IDEA 自帶的數(shù)據(jù)庫工具真的很牛(收藏版)

    這篇文章主要介紹了IDEA 自帶的數(shù)據(jù)庫工具真的很牛(收藏版),本文以 IntelliJ IDEA/ Mac 版本作為演示,其他版本的應該也差距不大,需要的朋友可以參考下
    2021-04-04
  • Spring Boot 集成 Mybatis Plus 自動填充字段的實例詳解

    Spring Boot 集成 Mybatis Plus 自動填充字段的實例詳解

    這篇文章主要介紹了Spring Boot 集成 Mybatis Plus 自動填充字段,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 復雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實現(xiàn)

    復雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實現(xiàn)

    這篇文章主要介紹了復雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java設計模式之觀察者模式(Observer模式)介紹

    Java設計模式之觀察者模式(Observer模式)介紹

    這篇文章主要介紹了Java設計模式之觀察者模式(Observer模式)介紹,Java深入到一定程度,就不可避免的碰到設計模式(design pattern)這一概念,了解設計模式,將使自己對java中的接口或抽象類應用有更深的理解,需要的朋友可以參考下
    2015-03-03
  • 關于線程池異步線程中再次獲取線程池資源的問題

    關于線程池異步線程中再次獲取線程池資源的問題

    這篇文章主要介紹了關于線程池異步線程中再次獲取線程池資源的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 淺談Java中的LinkedHashSet哈希鏈表

    淺談Java中的LinkedHashSet哈希鏈表

    這篇文章主要介紹了淺談Java中的LinkedHashSet哈希鏈表,LinkedHashSet 是 Java 中的一個集合類,它是 HashSet 的子類,并實現(xiàn)了 Set 接口,與 HashSet 不同的是,LinkedHashSet 保留了元素插入的順序,并且具有 HashSet 的快速查找特性,需要的朋友可以參考下
    2023-09-09
  • Java線性表的順序表示及實現(xiàn)

    Java線性表的順序表示及實現(xiàn)

    這篇文章主要介紹了Java線性表的順序表示及實現(xiàn),順序表是在計算機內(nèi)存中以數(shù)組的形式保存的線性表,線性表的順序存儲是指用一組地址連續(xù)的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中
    2022-07-07
  • 深入探究Spring底層核心原理

    深入探究Spring底層核心原理

    理解IOC與AOP的實現(xiàn)機制,優(yōu)化應用性能與可維護性。Spring通過IOC容器管理Bean,AOP實現(xiàn)切面編程,支持事務管理、ORM框架等。深入理解Spring原理,可以幫助我們更好地使用Spring框架,提高開發(fā)效率與質(zhì)量
    2023-04-04

最新評論