跳到主要内容

quan-crypto

基于Shiro中的凭证工具类封装的密码生成工具,可以实现自定义算法加密密码。提高了普通密码的安全性。

在验证密码时,可以自定义凭证校验处理器RetryLimitHashedCredentialsMatcher;比如密码校验失败后的处理策略等。

当前工具实现了登录失败次数的校验,当调用PasswordService#verify() 方法时生效。

使用方法

1、引入工具依赖包

提示

工具包目前只在quan-tools模块中,需要通过quan-tools引入。 后期视具体需求决定是否上传到 Maven 仓库。

dependencies {
api project(':quan-tools:quan-crypto')
}

2、yml配置:

示例配置仅供参考
quan:
crypto:
timeOut: 3600
retryCount: 5
algorithmName: md5
hashIterations: 3
realmName: quan

配置参数说明

timeOut

  • 类型:long
  • 默认: 3600

默认缓存过期时间,配置密码校验次数的缓存时间,单位:秒

retryCount

  • 类型:int
  • 默认: 5

重试次数,当登录密码校验错误后,允许的重试次数。 当同一账号密码校验超过该次数时,将锁定该账号的登录操作。

锁定时间=登录失败重试的次数 * 缓存过期时间(timeOut

algorithmName

  • 类型:string
  • 默认: md5

加密方式,配置密码加密的方式

hashIterations

  • 类型:int
  • 默认: 3

迭代次数,使用给定的盐对所提供的凭证进行哈希的次数

realmName

  • 类型:string
  • 默认: quan

加密主体的名称

工具类说明-PasswordService

encryptPassword

根据账号和密码等信息,生成校验凭证。

请求参数:

参数类型是否必须说明
accountString登录账号
passwordString登录密码
saltString加盐(执行方法后,自动注入该参数)
secretString凭证(执行方法后,自动注入该参数)

返回参数: 无

该方法会注入请求参数中的saltsecret参数值。

verify

用于登录凭证校验。

请求参数:

参数类型是否必须说明
accountString登录账号
passwordString登录密码
saltString
secretString凭证
ipString用户登录IP

返回参数:

  • true:校验成功
  • false:校验失败

应用示例

@RequiredArgsConstructor
@Component
public class Test {

/**
* Spring注入方式
*/
private final PasswordService passwordService;

public PageResult<SysUserAccountPO> generatePasswordTest(String account, String password) {
// 生成登录凭证
CryptoParam cryptoParam = passwordService.encryptPassword(account, password);

String salt = cryptoParam.getSalt();
String secret = cryptoParam.getSecret();
System.out.println(salt);
System.out.println(secret);

// 校验登录凭证
// 初始化校验参数
cryptoParam.setAccount(account);
cryptoParam.setPassword(password);
cryptoParam.setIp("127.0.0.1");
cryptoParam.setSalt(salt);
cryptoParam.setSecret(secret);
// 验证密码(true:验证通过,false:验证失败)
boolean verify = passwordService.verify(cryptoParam);
}

/**
* 直接运行方式
* 一般应用于测试用例或手动生成账号登录凭证
*/
public static void main(String[] args) {
PasswordService passwordService = new PasswordService(null, new CryptoProperties());
// 传入账号、明文登录密码
CryptoParam cryptoParam = passwordService.encryptPassword("super", "123456");
System.out.println(cryptoParam.toString());
}
}

CryptoParam中获取saltsecret,然后修改sys_user_account对应的saltsecret即可。