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

SpringBoot整合BCrypt實現(xiàn)密碼加密

 更新時間:2021年11月26日 17:34:16   作者:小鄭要做干飯人  
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合BCrypt進(jìn)行密碼加密,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了SpringBoot整合BCrypt實現(xiàn)密碼加密的具體代碼,供大家參考,具體內(nèi)容如下

一. 首先在pom依賴中加入依賴:

<!-- security依賴包 (加密) -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
</dependency>

二.在啟動類中加入@EnableScheduling注解,獲得BCrypt支持:

package com.zzx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
//獲得BCrypt支持
@EnableScheduling
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


}

三.模擬獲得登錄密碼:

package com.zzx.test;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @date: 2021/11/25/ 14:30
 * @author: ZhengZiXuan
 * @title: 使用BCrypt進(jìn)行密碼加密
 * @description: 引入Security依賴默認(rèn)開啟了登錄校驗,訪問API會跳轉(zhuǎn)到登錄頁,如果只是需要BCrypt加密功能可以在啟動類配置@SpringBootApplication (exclude = { org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class })禁用Security相關(guān)功能。
 */
public class BCryptTest {
    public static void main(String[] args) {
        //模擬從前端獲得的密碼
        String password = "123456";
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String newPassword = bCryptPasswordEncoder.encode(password);
        System.out.println("加密的密碼為: "+newPassword);
        boolean same_password_result = bCryptPasswordEncoder.matches(password,newPassword);
        //返回true
        System.out.println("相同代碼對比: "+same_password_result);
        boolean other_password_result = bCryptPasswordEncoder.matches("1234456",newPassword);
        //返回false
        System.out.println("其他密碼對比: " + other_password_result);
    }
}

運行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論