-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from zhangbin1010/dev
[Upgrade] 增加用户密码加解密工具类
- Loading branch information
Showing
5 changed files
with
83 additions
and
38 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
cloud/oauth-server/src/main/kotlin/pers/acp/admin/oauth/component/UserPasswordEncrypt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pers.acp.admin.oauth.component | ||
|
||
import org.springframework.stereotype.Component | ||
import pers.acp.core.CommonTools | ||
import pers.acp.core.security.Sha256Encrypt | ||
|
||
/** | ||
* 密码加解密工具类 | ||
*/ | ||
@Component | ||
class UserPasswordEncrypt { | ||
/** | ||
* 密码加密(用于存储) | ||
* @param loginNo 登录账号 | ||
* @param password 密码明文 | ||
* @return 加密后的存储密文 | ||
*/ | ||
fun encrypt(loginNo: String, password: String) = Sha256Encrypt.encrypt(Sha256Encrypt.encrypt(password) + loginNo) | ||
|
||
/** | ||
* 密码加密(用于登录) | ||
* @param encryptedPassword 加密后的密码存储密文 | ||
* @return 加密后的验证密文 | ||
*/ | ||
fun encryptForLogin(encryptedPassword: String) = | ||
encryptForLogin(encryptedPassword, CommonTools.getDateTimeString(dateTimeFormat = "yyyyMMddHH")) | ||
|
||
/** | ||
* 密码登录验证 | ||
* @param rawPassword 用户输入的密码 | ||
* @param encryptedPassword 加密后的密码存储密文 | ||
* @return true|false | ||
*/ | ||
fun matches(rawPassword: String, encryptedPassword: String): Boolean = | ||
CommonTools.getNowDateTime().let { now -> | ||
for (o in -offset..offset) { | ||
val password = encryptForLogin( | ||
encryptedPassword, | ||
CommonTools.getDateTimeString(now.plusHours(o), "yyyyMMddHH") | ||
) | ||
if (rawPassword.equals(password, ignoreCase = true)) { | ||
return true | ||
} | ||
} | ||
false | ||
} | ||
|
||
/** | ||
* 密码加密(用于登录) | ||
* @param encryptedPassword 加密后的密码存储密文 | ||
* @param factor 加密因子 | ||
* @return 加密后的验证密文 | ||
*/ | ||
private fun encryptForLogin(encryptedPassword: String, factor: String) = | ||
Sha256Encrypt.encrypt(encryptedPassword + factor) | ||
|
||
companion object { | ||
private const val offset = 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
cloud/oauth-server/src/test/java/pers/acp/admin/oauth/nobuild/InitData.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters