|
@@ -1,26 +1,72 @@
|
|
|
package com.ym.mec.auth.core.handler;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
-import javax.servlet.ServletException;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.ym.mec.auth.api.entity.SysUser;
|
|
|
+import com.ym.mec.auth.api.entity.SysUserLogin;
|
|
|
+import com.ym.mec.auth.api.entity.SysUserLoginLog;
|
|
|
+import com.ym.mec.auth.service.SysUserLoginLogService;
|
|
|
+import com.ym.mec.auth.service.SysUserLoginService;
|
|
|
+import com.ym.mec.auth.service.SysUserService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
@Component
|
|
|
public class BaseAuthenticationSuccessEventHandler extends SavedRequestAwareAuthenticationSuccessHandler {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysUserLoginService sysUserLoginService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserLoginLogService sysUserLoginLogService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserService sysUserService;
|
|
|
+ @Autowired
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+
|
|
|
private final static Logger logger = LoggerFactory.getLogger(BaseAuthenticationSuccessEventHandler.class);
|
|
|
|
|
|
@Override
|
|
|
- public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException,
|
|
|
- IOException {
|
|
|
+ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication){
|
|
|
logger.info("用户:{} 登录成功", authentication.getPrincipal());
|
|
|
+ HashMap<String,String> hashMap = objectMapper.convertValue(authentication.getPrincipal(), HashMap.class);
|
|
|
+ SysUser sysUser = sysUserService.queryByUsername(hashMap.get("username"));
|
|
|
+ if(sysUser == null){
|
|
|
+ sysUser = sysUserService.queryByPhone(hashMap.get("username"));
|
|
|
+ }
|
|
|
+ Date date = new Date();
|
|
|
+ //修改添加登录信息
|
|
|
+ SysUserLogin userLogin = sysUserLoginService.findLoginByUserId(sysUser.getUserId());
|
|
|
+ if(userLogin == null){
|
|
|
+ userLogin = new SysUserLogin();
|
|
|
+ userLogin.setLastLoginIp(request.getRemoteAddr());
|
|
|
+ userLogin.setUserId(sysUser.getUserId());
|
|
|
+ userLogin.setLoginCount(1);
|
|
|
+ sysUserLoginService.insert(setUserLogin(userLogin,date));
|
|
|
+ }else {
|
|
|
+ userLogin.setLastLoginIp(request.getRemoteAddr());
|
|
|
+ userLogin.setLoginCount(userLogin.getLoginCount() + 1);
|
|
|
+ sysUserLoginService.update(setUserLogin(userLogin,date));
|
|
|
+ }
|
|
|
+ //添加登录日志
|
|
|
+ SysUserLoginLog sysUserLoginLog = new SysUserLoginLog();
|
|
|
+ sysUserLoginLog.setLoginTime(date);
|
|
|
+ sysUserLoginLog.setLoginIp(request.getRemoteAddr());
|
|
|
+ sysUserLoginLog.setUserId(sysUser.getUserId());
|
|
|
+ sysUserLoginLogService.insert(sysUserLoginLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ private SysUserLogin setUserLogin(SysUserLogin userLogin,Date date){
|
|
|
+ userLogin.setFailCount(0);
|
|
|
+ userLogin.setLastLoginTime(date);
|
|
|
+ return userLogin;
|
|
|
}
|
|
|
|
|
|
}
|