使用Springboot整合mybatisplus+springsecurity+vue3实现登陆密码错误3次后需要等待30秒再次登录功能

用户密码加密解密:

        这个管理系统用户密码是使用springscurity的BCcryPasswordEncoder().encode进行加密;当然在登录需要进行解密在login接口下需要使用!bCryptPasswordEncoder.matches进行解密。

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    public static void main(String[] args) {
        String encode = new BCryptPasswordEncoder().encode("123456");
        System.out.println(encode);
    }

判断密码正确是否后的操作:

        解密后判断密码是否正确,如果密码正确就直接登录成功,反之密码错误后,就会判断您具体登录几次,而我们需要实现登录密码错误3次后需要等待30秒才能再次登录。因此需要以什么方式来判断您具体登录几次,我的方式是通过在user表中建立一个user_loginAttempts的字段,在当密码错误时,我们就向数据库增加1:

	//增加登陆次数
	@Update("UPDATE user SET user_loginAttempts = user_loginAttempts + 1 WHERE user_id = #{userId}")
	void incrementLoginAttempts(Integer userId);
    	//判断密码是否正确
    	if(!bCryptPasswordEncoder.matches(userParam.getPassword(), selectByName.getPassword())) {
		//修改登录次数
			//查询登录次数
			Integer loginAttempts = userService.selectloginAttemptsByUserName(res.getUsername());
			//增加登陆次数
			userService.incrementLoginAttempts(res.getUserId());

 我们的数据库设置默认为0,因此点击一次登录错误后,就需要进行判断您还有几次登录机会;of course在点击一次后在页面就会弹出您还有两次机会;第一次和第二次错误判断如下:

if (res.getUserLoginAttempts() == 1){
	return Result.error("-1", "Password error!You have one more opportunities to log in");
}else {
	return Result.error("-1", "Password error!You have two more opportunities to log in");}

再次点击后就会弹出还有一次机会;

那么你如果下一次再次登录错误就达到了三次错误了,So就需要考虑以什么方式来解决等待30秒才能登录成功这个问题。我使用比较简单的一种方式:在user表中建立了一个记载第三次错误登录的字段user_loginLastTime。那么第三次错误了就将具体时间写入mysql;好在第四次登录的时候就需要等待30秒才能登录了。

第三次登录错误:

if(res.getUserLoginAttempts() == 2){
	res.setUserLoginLastTime(LocalDateTime.now());
	userService.updateuserLoginLastTime(res);
	return Result.error("-1","You will need to wait 30 seconds before logging in");
}

第四次登录错误:

 直接是以当前和第三次存储时间进行比较

		if(res.getUserLoginAttempts()>2){
			LocalDateTime lastLoginTime = res.getUserLoginLastTime();
			LocalDateTime currentTime = LocalDateTime.now();
			//判断
			Duration duration = Duration.between(lastLoginTime,currentTime);
			if (duration.getSeconds()<30){
				long i = 30;
				return Result.error("-1", "You've been waiting "+(i-duration.getSeconds())+" seconds");
			}else {
				userService.updateLoginAttemptsZero(res.getUserId());
				return Result.error("-1","Login restrictions reset");
			}
		}

此项目已经开源,源码地址student-manager-system: student-erp后端基于Spring Boot框架,前端基于vue3框架,使用Sping Security作为安全框架,目前专注于对各个院校的管理,针对到学生/教师/管理员、班级、专业、学院、大学之间的权限管理问题。学生/教师/管理员直接通过用户表,角色表来区分三者之间的区别。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程汐笙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值