ソースを参照

登录账号密码错误多次需要进行验证码验证功能

user5 2 年 前
コミット
e54e6eb5fc

+ 1 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/security/config/WebSecurityConfig.java

@@ -71,6 +71,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                         "/sys/sysConfig/getConfig",
                         "/getAppFlowChart",
                         "/sys/getCode",
+                        "/sys/getLoginCodeNumber",
                         "/app/sys/getCode",
                         "/sys/casLogin").permitAll() // 允许请求无需认证
                 .antMatchers( HttpMethod.OPTIONS, "/**").permitAll()

+ 2 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/constant/CacheNames.java

@@ -39,5 +39,7 @@ public interface CacheNames {
 
      String USER_CACHE_TOKEN = "user:cache:token:";
 
+     String USER_CACHE_LOGIN_CODE = "user:cache:code:loginName"; //用户登录次数
+
 
 }

+ 53 - 3
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/controller/LoginController.java

@@ -21,6 +21,8 @@ import com.jeeplus.sys.model.LoginForm;
 import com.jeeplus.sys.service.SysConfigService;
 import com.jeeplus.sys.service.UserService;
 import com.jeeplus.sys.service.dto.UserDTO;
+import com.jeeplus.sys.utils.DictUtils;
+import com.jeeplus.sys.utils.StringUtils;
 import com.jeeplus.sys.utils.UserUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -51,6 +53,8 @@ import java.nio.file.AccessDeniedException;
 import java.util.Date;
 import java.util.UUID;
 
+import static java.awt.SystemColor.info;
+
 /**
  * 登录Controller
  *
@@ -83,8 +87,26 @@ public class LoginController {
         String username = loginForm.getUsername ();
         String password = loginForm.getPassword ();
         String code = loginForm.getCode ();
-        if(!code.equals ( RedisUtils.getInstance ().get (CacheNames.SYS_CACHE_CODE, loginForm.getUuid ()))){
-            throw new AccountExpiredException ( ErrorConstants.LOGIN_ERROR_ERROR_VALIDATE_CODE );
+        Integer redisLoginNumber = (Integer) RedisUtils.getInstance ().get ( CacheNames.USER_CACHE_LOGIN_CODE + username );
+        if(null == redisLoginNumber){
+            redisLoginNumber = 0;
+        }else{
+            redisLoginNumber ++ ;
+        }
+        RedisUtils.getInstance().set(CacheNames.USER_CACHE_LOGIN_CODE + username , redisLoginNumber);
+        //给登录次数记录设置6小时的过期时间
+        RedisUtils.getInstance().expire(CacheNames.USER_CACHE_LOGIN_CODE + username , 21600);
+
+        String dictValue = DictUtils.getDictLabel("login_number", "login_verification_number", null);
+        //字典中限制显示次数
+        Integer loginNumber = 0;
+        if(StringUtils.isNotBlank(dictValue)) {
+            loginNumber = Integer.valueOf(dictValue);
+        }
+        if(redisLoginNumber >= loginNumber){
+            if(!code.equals ( RedisUtils.getInstance ().get (CacheNames.SYS_CACHE_CODE, loginForm.getUuid ()))){
+                throw new AccountExpiredException ( ErrorConstants.LOGIN_ERROR_ERROR_VALIDATE_CODE );
+            }
         }
         SecurityUtils.login (username, password, authenticationManager  ); //登录操作spring security
 
@@ -101,7 +123,8 @@ public class LoginController {
         responseUtil.add ( TokenProvider.TOKEN, token);
         //更新登录信息
         updateUserLoginInfo ( responseUtil, userDTO , token);
-
+        //删除redis中登录次数的信息
+        RedisUtils.getInstance ().delete ( CacheNames.USER_CACHE_LOGIN_CODE + username );
         return responseUtil.ok ( );
     }
 
@@ -206,4 +229,31 @@ public class LoginController {
     }
 
 
+    /**
+     * 获取登录次数
+     * @throws
+     */
+    @ApiOperation ("获取登录次数")
+    @ApiLog("获取登录次数")
+    @GetMapping("/sys/getLoginCodeNumber")
+    public ResponseEntity getLoginCodeNumber(String userName){
+        //字典中限制显示次数
+        Integer loginNumber = 0;
+        //redis中记录登录次数
+        Object redisLoginNumber = RedisUtils.getInstance ().get ( CacheNames.USER_CACHE_LOGIN_CODE + userName );
+        if(null == redisLoginNumber){
+            redisLoginNumber = 0;
+        }
+        String dictValue = DictUtils.getDictLabel("login_number", "login_verification_number", null);
+        if(StringUtils.isNotBlank(dictValue)){
+            loginNumber = Integer.valueOf(dictValue);
+            if(loginNumber > 0){
+                loginNumber -- ;
+            }
+        }
+
+        return ResponseUtil.newInstance ().add ( "redisLoginNumber", redisLoginNumber ).add ( "loginNumber", loginNumber ).ok ();
+    }
+
+
 }