|
@@ -1,18 +1,24 @@
|
|
package com.jiayue.ssi.handler;
|
|
package com.jiayue.ssi.handler;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.jiayue.ssi.config.SendMailUtil;
|
|
import com.jiayue.ssi.constant.CacheConstants;
|
|
import com.jiayue.ssi.constant.CacheConstants;
|
|
import com.jiayue.ssi.constant.Constants;
|
|
import com.jiayue.ssi.constant.Constants;
|
|
-import com.jiayue.ssi.constant.LoginConstants;
|
|
|
|
|
|
+import com.jiayue.ssi.entity.SysAlarm;
|
|
|
|
+import com.jiayue.ssi.entity.SysPolicy;
|
|
import com.jiayue.ssi.entity.SysUser;
|
|
import com.jiayue.ssi.entity.SysUser;
|
|
import com.jiayue.ssi.factory.LoginFactory;
|
|
import com.jiayue.ssi.factory.LoginFactory;
|
|
-import com.jiayue.ssi.service.SysParameterService;
|
|
|
|
|
|
+import com.jiayue.ssi.service.SysAlarmService;
|
|
|
|
+import com.jiayue.ssi.service.SysPolicyService;
|
|
import com.jiayue.ssi.service.SysUserService;
|
|
import com.jiayue.ssi.service.SysUserService;
|
|
|
|
+import com.jiayue.ssi.util.AesUtils;
|
|
import com.jiayue.ssi.util.LocalCache;
|
|
import com.jiayue.ssi.util.LocalCache;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
@@ -29,7 +35,11 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF
|
|
@Autowired
|
|
@Autowired
|
|
SysUserService sysUserService;
|
|
SysUserService sysUserService;
|
|
@Autowired
|
|
@Autowired
|
|
- SysParameterService sysParameterService;
|
|
|
|
|
|
+ SysPolicyService sysPolicyService;
|
|
|
|
+ @Autowired
|
|
|
|
+ SysAlarmService sysAlarmService;
|
|
|
|
+ @Autowired
|
|
|
|
+ SendMailUtil sendMailUtil;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
|
|
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
|
|
@@ -42,19 +52,11 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF
|
|
errorTips = "用户名或密码不正确";
|
|
errorTips = "用户名或密码不正确";
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
|
|
+ SysPolicy sysPolicy = sysPolicyService.getOne(new QueryWrapper<>());
|
|
// 失败总次数
|
|
// 失败总次数
|
|
- int errCount = 5;
|
|
|
|
- try {
|
|
|
|
- errCount = Integer.parseInt(sysParameterService.queryByKey("errCount", "5"));
|
|
|
|
- } catch (Exception errCountErr) {
|
|
|
|
- // 日志输出转换失败的提示
|
|
|
|
- }
|
|
|
|
- int lockedTime = 1200000;
|
|
|
|
- try {
|
|
|
|
- lockedTime = Integer.parseInt(sysParameterService.queryByKey("lockedTime", "1200000"));
|
|
|
|
- } catch (Exception lockedTimeErr) {
|
|
|
|
- // 日志输出转换失败的提示
|
|
|
|
- }
|
|
|
|
|
|
+ int errCount = sysPolicy.getLoginFails();
|
|
|
|
+ // 锁定时长
|
|
|
|
+ int lockedTime = sysPolicy.getLoginLock()*1000*60;
|
|
if (user.getStatus().equals("1")) {
|
|
if (user.getStatus().equals("1")) {
|
|
Long xz = (user.getLockTime() + lockedTime - System.currentTimeMillis()) / 1000;
|
|
Long xz = (user.getLockTime() + lockedTime - System.currentTimeMillis()) / 1000;
|
|
if (xz < 0) {
|
|
if (xz < 0) {
|
|
@@ -87,6 +89,37 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF
|
|
user.setStatus("1");
|
|
user.setStatus("1");
|
|
user.setLockTime(System.currentTimeMillis());
|
|
user.setLockTime(System.currentTimeMillis());
|
|
sysUserService.updateUser(user);
|
|
sysUserService.updateUser(user);
|
|
|
|
+ String noticeWay = "";
|
|
|
|
+ if ("A".equals(sysPolicy.getExcLevelLogin())){
|
|
|
|
+ noticeWay = sysPolicy.getExcNoticeWayA();
|
|
|
|
+ } else if ("B".equals(sysPolicy.getExcLevelLogin())){
|
|
|
|
+ noticeWay = sysPolicy.getExcNoticeWayB();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 通知系统管理员
|
|
|
|
+ if ("0".equals(noticeWay)){
|
|
|
|
+ List<SysUser> sysUserList = sysUserService.selectUserByRole("1");
|
|
|
|
+ if (sysUserList.size()>0){
|
|
|
|
+ String[] mailArray = new String[sysUserList.size()];
|
|
|
|
+ for (int i=0;i<sysUserList.size();i++){
|
|
|
|
+ mailArray[i] = AesUtils.decryptStr(sysUserList.get(i).getMailbox());
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 发送邮件
|
|
|
|
+ sendMailUtil.executeSendMail(mailArray,"账号【"+user.getUsername()+"】登录失败","账号【"+user.getUsername()+"】登录失败"+errCount+"次,锁定账号!");
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ System.out.println("邮箱验证码发送失败!");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else if ("1".equals(noticeWay)){
|
|
|
|
+ // 告警
|
|
|
|
+ SysAlarm sysAlarm = new SysAlarm();
|
|
|
|
+ sysAlarm.setAlarmContent("账号【"+user.getUsername()+"】登录失败"+errCount+"次,锁定账号!");
|
|
|
|
+ sysAlarm.setReadSign("0");
|
|
|
|
+ // 低于策略设定值则告警
|
|
|
|
+ sysAlarmService.save(sysAlarm);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -101,5 +134,4 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF
|
|
response.setStatus(401);
|
|
response.setStatus(401);
|
|
response.getWriter().write(errorTips);
|
|
response.getWriter().write(errorTips);
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|