当前位置:   article > 正文

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

vue密码尝试3次

用户密码加密解密:

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

  1. @Bean
  2. public BCryptPasswordEncoder bCryptPasswordEncoder() {
  3. return new BCryptPasswordEncoder();
  4. }
  5. public static void main(String[] args) {
  6. String encode = new BCryptPasswordEncoder().encode("123456");
  7. System.out.println(encode);
  8. }

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

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

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

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

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

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

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

第三次登录错误:

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

第四次登录错误:

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/53944
推荐阅读
相关标签
  

闽ICP备14008679号