二 Spring Security之密碼加密

2021-09-23 17:12:50 字數 1481 閱讀 1136

@override

protected

void

configure

(authenticationmanagerbuilder auth)

throws exception

/** * 密碼加密,必須為 @bean ,否則報錯

* 作用:例項化密碼加密規則,該規則首先會校驗資料庫中儲存的密碼是否符合其規則(經過 bcryptpasswordencoder 加密的密碼

* 的字串符合一定的規則):

* 1.若不符合,直接報錯;

* 2.若符合,則會將前端傳遞的密碼經過 bcryptpasswordencoder 加密,再和資料庫中的密碼進行比對,一樣則通過

* 所以,這裡要求,我們存入進資料庫的密碼不能是明文,而必須是經過 bcryptpasswordencoder 加密後,才能存入資料庫

*/@bean

public bcryptpasswordencoder passwordencoder()

bcryptpasswordencoder相關知識:

使用者表的密碼通常使用md5等不可逆演算法加密後儲存,為防止彩虹表破解更會先使用乙個特定的字串(如網域名稱)加密,然後再使用乙個隨機的salt(鹽值)加密。

特定字串是程式**中固定的,salt是每個密碼單獨隨機,一般給使用者表加乙個字段單獨儲存,比較麻煩。

bcrypt演算法將salt隨機並混入最終加密後的密碼,驗證時也無需單獨提供之前的salt,從而無需單獨處理salt問題。

bcryptpasswordencoder 是在**使用的?登入時用到了daoauthenticationprovider,它有乙個方法

#additionalauthenticationchecks(userdetails userdetails, usernamepasswordauthenticationtoken authentication),此方法用來校驗從資料庫取得的使用者資訊和使用者輸入的資訊是否匹配。

在註冊時,需要對使用者密碼加密

應用 bcryptpasswordencoder 之後,明文密碼是無法被識別的,就會校驗失敗,只有存入密文密碼才能被正常識別。所以,應該在註冊時對使用者密碼進行加密。

private string encryptpassword

(string password)

新使用者註冊後,資料庫中就會存入密文密碼,示例:

補充說明:即使不同的使用者註冊時輸入相同的密碼,存入資料庫的密文密碼也會不同。

SpringSecurity學習筆記 二

一 設計許可權部分的資料表 1 resources 表 需要攔截的 url id url 1 employee list.do 2 employee update.do 3 employee update ui.do 2 authorities 表 許可權表 id name display name...

spring security 安全框架

本文 http itblood.com spring security security framework.html 安全常識 acegi介紹 以宣告式方式為基於spring的web應用新增認證和授權控制 acegi體系結構 認證管理器 訪問控制管理器。認證 authenticationproce...

SpringSecurity認證流程

在之前的文章 springboot spring security 基本使用及個性化登入配置 中對springsecurity進行了簡單的使用介紹,基本上都是對於介面的介紹以及功能的實現。這一篇文章嘗試從原始碼的角度來上對使用者認證流程做乙個簡單的分析。在具體分析之前,我們可以先看看springse...