Springboot Shiro 修改密碼

2021-10-03 20:06:26 字數 2735 閱讀 4589

首先從前端說起,在修改密碼的時候首先需要填寫舊密碼

我這裡使用的是jquery-validate,主要看一下這裡,把填寫的舊密碼傳到後台進行check

$("#changepasswordform").validate(,}}

},

但是我資料庫中的密碼是md5加鹽加密兩次得到的密文,而前端傳過去的是明文,該如何解決呢?第一步,到controller層了

/**

* 檢查對應的使用者和密碼是否正確

* @param oldword

* @return

*/@responsebody

public string checkuserandword(string oldword)

第二步  service層

這裡我們可以看到 user user = shiroutil.getuser();  這行**才是關鍵

而encryptpassword就是拿同樣的鹽值,加上傳到後台的明文密碼,用md5加密兩次

@override

public string matchs(string oldword) else

return constant.match_not_fit;

}@override

public string encryptpassword(string password, string salt)

第三步:shiroutil.getuser()

這裡最關鍵的就是securityutils.getsubject().getprincipal(),其他的不看

這裡就要說到,它是如何獲取當前使用者資訊的

public static subject getsubject()

public static user getuser()

return user;

}

先說shiro的配置檔案,網上說use-prefix選項,其配置有兩點需要注意:

要在配置檔案的最上邊(或者相關屬性的最前邊);

一定要先於其它相關配置生效

雖然我試了不這樣也可以,但畢竟人家遇到問題這樣解決的,所以我們也按這樣來,以防萬一。

@configuration

public class shiroconfiguration

@bean

//@conditionalo****singbean

public defaultadvisorautoproxycreator defaultadvisorautoproxycreator()

//如果沒有這兩個配置,可能會授權失敗,所以依賴中還需要配置aop的依賴

@bean

public authorizationattributesourceadvisor authorizationattributesourceadvisor(hashedcredentialsmatcher matcher)

然後看自定義realm中的認證

******authenticationinfo info = new ******authenticationinfo(user,passwordindb, bytesource.util.bytes(salt),getname());

這裡就是securityutils.getsubject().getprincipal() 為什麼可以獲取到當前使用者資訊,如果傳username,那就只能獲取到username

@override

protected authenticationinfo dogetauthenticationinfo(authenticationtoken token) throws authenticationexception {

logger.debug("######## shiro執行認證 ########");

//獲取賬號密碼

usernamepasswordtoken uptoken = (usernamepasswordtoken) token;

string username = uptoken.getusername();

//獲取資料庫中的密碼

user user = userservice.getbyusername(username);

string passwordindb = user.getpassword();

string salt = user.getsalt();

//認證資訊裡存放賬號密碼, getname() 是當前realm的繼承方法,通常返回當前類名 :databaserealm

//鹽也放進去

******authenticationinfo info = new ******authenticationinfo(user,passwordindb, bytesource.util.bytes(salt),getname());

return info;

好了,然後就是回到第二步,將密碼進行匹配,返回結果給前端

Spring boot shiro 跨域失效

這兩種方法在springboot中都能解決跨域的問題,但在整合shiro後,跨域就是失效了。因為shiro的過濾器會在跨域處理之前執行,這就導致未經允許跨域的請求先到達shiro過濾器,這樣就會出現跨域錯誤。解決方案 使用filter的方式解決跨域 component public class co...

SpringBoot Shiro登入原始碼解析

專案登入的controller截圖如下 還是從當前執行緒取出subject物件,在啟動時候已經放置了subject物件,所以此處subject物件不為空,新建乙個usernamepasswordtoken物件,把前端使用者名稱 密碼傳遞到其建構函式中,該物件實現了remembermeauthenti...

Linux Windows 修改 mysql 密碼

在記住原來的密碼下 mysql u root p 進入 mysql 介面 use mysql update user set password password xinmima where user root flush privileges q 重新登入立即生效忘記原來的密碼 net stop m...