From 0bee2e75107b91cbe7bab8045319bb6709a3606f Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 19 十一月 2024 17:23:25 +0800 Subject: [PATCH] 1 --- se-modules/se-system/src/main/java/com/se/system/domain/RegisterBody.java | 11 + se-modules/se-system/src/main/java/com/se/system/controller/TokenController.java | 95 +++++++++++ se-modules/se-system/src/main/java/com/se/system/service/SysRecordLogService.java | 48 ++++++ se-modules/se-system/src/main/java/com/se/system/service/SysLoginService.java | 163 ++++++++++++++++++++ se-modules/se-system/src/main/java/com/se/system/service/SysPasswordService.java | 87 ++++++++++ se-modules/se-system/src/main/java/com/se/system/domain/LoginBody.java | 39 ++++ 6 files changed, 443 insertions(+), 0 deletions(-) diff --git a/se-modules/se-system/src/main/java/com/se/system/controller/TokenController.java b/se-modules/se-system/src/main/java/com/se/system/controller/TokenController.java new file mode 100644 index 0000000..916ae1d --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/controller/TokenController.java @@ -0,0 +1,95 @@ +package com.se.system.controller; + +import com.se.common.core.domain.R; +import com.se.common.core.utils.AesUtils; +import com.se.common.core.utils.JwtUtils; +import com.se.common.core.utils.StringUtils; +import com.se.common.security.auth.AuthUtil; +import com.se.common.security.service.TokenService; +import com.se.common.security.utils.SecurityUtils; +import com.se.system.api.model.LoginUser; +import com.se.system.domain.LoginBody; +import com.se.system.service.SysLoginService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; + +/** + * token 鎺у埗 + * + * @author admin + */ +@RestController +public class TokenController { + @Resource + private TokenService tokenService; + + @Autowired + private SysLoginService sysLoginService; + + @Value("${enableEncrypt}") + boolean enableEncrypt; + + @PostMapping("login") + public R<?> login(@RequestBody LoginBody form) throws Exception { + if (enableEncrypt && !StringUtils.isEmpty(form.getPassword())) { + form.setPassword(AesUtils.decrypt(form.getPassword())); + } + // 鐢ㄦ埛鐧诲綍 + LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword()); + // 鑾峰彇鐧诲綍token + return R.ok(tokenService.createToken(userInfo)); + } + + @GetMapping("validate") + @PostMapping("validate") + public R<Object> validate(HttpServletRequest request) { + try { + boolean flag = false; + String token = SecurityUtils.getToken(request); + if (!StringUtils.isNotEmpty(token)) { + String userName = JwtUtils.getUserName(token); + flag = !StringUtils.isEmpty(userName); + } + + return R.ok(flag); + } catch (Exception ex) { + return R.fail(ex.getMessage()); + } + } + + @DeleteMapping("logout") + public R<?> logout(HttpServletRequest request) { + String token = SecurityUtils.getToken(request); + if (StringUtils.isNotEmpty(token)) { + String username = JwtUtils.getUserName(token); + // 鍒犻櫎鐢ㄦ埛缂撳瓨璁板綍 + AuthUtil.logoutByToken(token); + // 璁板綍鐢ㄦ埛閫�鍑烘棩蹇� + sysLoginService.logout(username); + } + return R.ok(); + } + + @PostMapping("refresh") + public R<?> refresh(HttpServletRequest request) { + LoginUser loginUser = tokenService.getLoginUser(request); + if (StringUtils.isNotNull(loginUser)) { + // 鍒锋柊浠ょ墝鏈夋晥鏈� + tokenService.refreshToken(loginUser); + return R.ok(); + } + return R.ok(); + } + + /*@PostMapping("register") + public R<?> register(@RequestBody RegisterBody registerBody) + { + // 鐢ㄦ埛娉ㄥ唽 + sysLoginService.register(registerBody.getUsername(), registerBody.getPassword()); + return R.ok(); + }*/ +} diff --git a/se-modules/se-system/src/main/java/com/se/system/domain/LoginBody.java b/se-modules/se-system/src/main/java/com/se/system/domain/LoginBody.java new file mode 100644 index 0000000..4f471e6 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/domain/LoginBody.java @@ -0,0 +1,39 @@ +package com.se.system.domain; + +/** + * 鐢ㄦ埛鐧诲綍瀵硅薄 + * + * @author admin + */ +public class LoginBody +{ + /** + * 鐢ㄦ埛鍚� + */ + private String username; + + /** + * 鐢ㄦ埛瀵嗙爜 + */ + private String password; + + public String getUsername() + { + return username; + } + + public void setUsername(String username) + { + this.username = username; + } + + public String getPassword() + { + return password; + } + + public void setPassword(String password) + { + this.password = password; + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/domain/RegisterBody.java b/se-modules/se-system/src/main/java/com/se/system/domain/RegisterBody.java new file mode 100644 index 0000000..aca0d09 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/domain/RegisterBody.java @@ -0,0 +1,11 @@ +package com.se.system.domain; + +/** + * 鐢ㄦ埛娉ㄥ唽瀵硅薄 + * + * @author admin + */ +public class RegisterBody extends LoginBody +{ + +} diff --git a/se-modules/se-system/src/main/java/com/se/system/service/SysLoginService.java b/se-modules/se-system/src/main/java/com/se/system/service/SysLoginService.java new file mode 100644 index 0000000..231122c --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/SysLoginService.java @@ -0,0 +1,163 @@ +package com.se.system.service; + +import com.se.common.core.constant.CacheConstants; +import com.se.common.core.constant.Constants; +import com.se.common.core.constant.SecurityConstants; +import com.se.common.core.constant.UserConstants; +import com.se.common.core.domain.R; +import com.se.common.core.enums.UserStatus; +import com.se.common.core.exception.ServiceException; +import com.se.common.core.text.Convert; +import com.se.common.core.utils.DateUtils; +import com.se.common.core.utils.StringUtils; +import com.se.common.core.utils.ip.IpUtils; +import com.se.common.redis.service.RedisService; +import com.se.common.security.utils.SecurityUtils; +import com.se.system.api.RemoteUserService; +import com.se.system.api.domain.SysUser; +import com.se.system.api.model.LoginUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * 鐧诲綍鏍¢獙鏂规硶 + * + * @author admin + */ +@Component +public class SysLoginService +{ + @Autowired + private RemoteUserService remoteUserService; + + @Autowired + private SysPasswordService passwordService; + + @Autowired + private SysRecordLogService recordLogService; + + @Resource + private RedisService redisService; + + /** + * 鐧诲綍 + */ + public LoginUser login(String username, String password) + { + // 鐢ㄦ埛鍚嶆垨瀵嗙爜涓虹┖ 閿欒 + if (StringUtils.isAnyBlank(username, password)) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "鐢ㄦ埛/瀵嗙爜蹇呴』濉啓"); + throw new ServiceException("鐢ㄦ埛/瀵嗙爜蹇呴』濉啓"); + } + // 瀵嗙爜濡傛灉涓嶅湪鎸囧畾鑼冨洿鍐� 閿欒 + if (password.length() < UserConstants.PASSWORD_MIN_LENGTH + || password.length() > UserConstants.PASSWORD_MAX_LENGTH) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "鐢ㄦ埛瀵嗙爜涓嶅湪鎸囧畾鑼冨洿"); + throw new ServiceException("鐢ㄦ埛瀵嗙爜涓嶅湪鎸囧畾鑼冨洿"); + } + // 鐢ㄦ埛鍚嶄笉鍦ㄦ寚瀹氳寖鍥村唴 閿欒 + if (username.length() < UserConstants.USERNAME_MIN_LENGTH + || username.length() > UserConstants.USERNAME_MAX_LENGTH) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "鐢ㄦ埛鍚嶄笉鍦ㄦ寚瀹氳寖鍥�"); + throw new ServiceException("鐢ㄦ埛鍚嶄笉鍦ㄦ寚瀹氳寖鍥�"); + } + // IP榛戝悕鍗曟牎楠� + String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST)); + if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "寰堥仐鎲撅紝璁块棶IP宸茶鍒楀叆绯荤粺榛戝悕鍗�"); + throw new ServiceException("寰堥仐鎲撅紝璁块棶IP宸茶鍒楀叆绯荤粺榛戝悕鍗�"); + } + // 鏌ヨ鐢ㄦ埛淇℃伅 + R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER); + + if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "鐧诲綍鐢ㄦ埛涓嶅瓨鍦�"); + throw new ServiceException("鐧诲綍鐢ㄦ埛锛�" + username + " 涓嶅瓨鍦�"); + } + + if (R.FAIL == userResult.getCode()) + { + throw new ServiceException(userResult.getMsg()); + } + + LoginUser userInfo = userResult.getData(); + SysUser user = userResult.getData().getSysUser(); + if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "瀵逛笉璧凤紝鎮ㄧ殑璐﹀彿宸茶鍒犻櫎"); + throw new ServiceException("瀵逛笉璧凤紝鎮ㄧ殑璐﹀彿锛�" + username + " 宸茶鍒犻櫎"); + } + if (UserStatus.DISABLE.getCode().equals(user.getStatus())) + { + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "鐢ㄦ埛宸插仠鐢紝璇疯仈绯荤鐞嗗憳"); + throw new ServiceException("瀵逛笉璧凤紝鎮ㄧ殑璐﹀彿锛�" + username + " 宸插仠鐢�"); + } + passwordService.validate(user, password); + recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "鐧诲綍鎴愬姛"); + recordLoginInfo(user.getUserId()); + return userInfo; + } + + /** + * 璁板綍鐧诲綍淇℃伅 + * + * @param userId 鐢ㄦ埛ID + */ + public void recordLoginInfo(Long userId) + { + SysUser sysUser = new SysUser(); + sysUser.setUserId(userId); + // 鏇存柊鐢ㄦ埛鐧诲綍IP + sysUser.setLoginIp(IpUtils.getIpAddr()); + // 鏇存柊鐢ㄦ埛鐧诲綍鏃堕棿 + sysUser.setLoginDate(DateUtils.getNowDate()); + remoteUserService.recordUserLogin(sysUser, SecurityConstants.INNER); + } + + public void logout(String loginName) + { + recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "閫�鍑烘垚鍔�"); + } + + /** + * 娉ㄥ唽 + */ + public void register(String username, String password) + { + // 鐢ㄦ埛鍚嶆垨瀵嗙爜涓虹┖ 閿欒 + if (StringUtils.isAnyBlank(username, password)) + { + throw new ServiceException("鐢ㄦ埛/瀵嗙爜蹇呴』濉啓"); + } + if (username.length() < UserConstants.USERNAME_MIN_LENGTH + || username.length() > UserConstants.USERNAME_MAX_LENGTH) + { + throw new ServiceException("璐︽埛闀垮害蹇呴』鍦�2鍒�20涓瓧绗︿箣闂�"); + } + if (password.length() < UserConstants.PASSWORD_MIN_LENGTH + || password.length() > UserConstants.PASSWORD_MAX_LENGTH) + { + throw new ServiceException("瀵嗙爜闀垮害蹇呴』鍦�5鍒�20涓瓧绗︿箣闂�"); + } + + // 娉ㄥ唽鐢ㄦ埛淇℃伅 + SysUser sysUser = new SysUser(); + sysUser.setUserName(username); + sysUser.setNickName(username); + sysUser.setPassword(SecurityUtils.encryptPassword(password)); + R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER); + + if (R.FAIL == registerResult.getCode()) + { + throw new ServiceException(registerResult.getMsg()); + } + recordLogService.recordLogininfor(username, Constants.REGISTER, "娉ㄥ唽鎴愬姛"); + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/service/SysPasswordService.java b/se-modules/se-system/src/main/java/com/se/system/service/SysPasswordService.java new file mode 100644 index 0000000..a50f458 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/SysPasswordService.java @@ -0,0 +1,87 @@ +package com.se.system.service; + +import com.se.common.core.constant.CacheConstants; +import com.se.common.core.constant.Constants; +import com.se.common.core.exception.ServiceException; +import com.se.common.redis.service.RedisService; +import com.se.common.security.utils.SecurityUtils; +import com.se.system.api.domain.SysUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +/** + * 鐧诲綍瀵嗙爜鏂规硶 + * + * @author admin + */ +@Component +@SuppressWarnings("ALL") +public class SysPasswordService +{ + @Autowired + private RedisService redisService; + + private int maxRetryCount = CacheConstants.PASSWORD_MAX_RETRY_COUNT; + + private Long lockTime = CacheConstants.PASSWORD_LOCK_TIME; + + @Autowired + private SysRecordLogService recordLogService; + + /** + * 鐧诲綍璐︽埛瀵嗙爜閿欒娆℃暟缂撳瓨閿悕 + * + * @param username 鐢ㄦ埛鍚� + * @return 缂撳瓨閿甼ey + */ + private String getCacheKey(String username) + { + return CacheConstants.PWD_ERR_CNT_KEY + username; + } + + public void validate(SysUser user, String password) + { + String username = user.getUserName(); + + Integer retryCount = redisService.getCacheObject(getCacheKey(username)); + + if (retryCount == null) + { + retryCount = 0; + } + + /*if (retryCount >= Integer.valueOf(maxRetryCount).intValue()) + { + String errMsg = String.format("瀵嗙爜杈撳叆閿欒%s娆★紝甯愭埛閿佸畾%s鍒嗛挓", maxRetryCount, lockTime); + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL,errMsg); + throw new ServiceException(errMsg); + }*/ + + if (!matches(user, password)) + { + retryCount = retryCount + 1; + recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, String.format("瀵嗙爜杈撳叆閿欒%s娆�", retryCount)); + redisService.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES); + throw new ServiceException("鐢ㄦ埛涓嶅瓨鍦�/瀵嗙爜閿欒"); + } + else + { + clearLoginRecordCache(username); + } + } + + public boolean matches(SysUser user, String rawPassword) + { + return SecurityUtils.matchesPassword(rawPassword, user.getPassword()); + } + + public void clearLoginRecordCache(String loginName) + { + if (redisService.hasKey(getCacheKey(loginName))) + { + redisService.deleteObject(getCacheKey(loginName)); + } + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/service/SysRecordLogService.java b/se-modules/se-system/src/main/java/com/se/system/service/SysRecordLogService.java new file mode 100644 index 0000000..2913f2e --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/SysRecordLogService.java @@ -0,0 +1,48 @@ +package com.se.system.service; + +import com.se.common.core.constant.Constants; +import com.se.common.core.constant.SecurityConstants; +import com.se.common.core.utils.StringUtils; +import com.se.common.core.utils.ip.IpUtils; +import com.se.system.api.RemoteLogService; +import com.se.system.api.domain.SysLogininfor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * 璁板綍鏃ュ織鏂规硶 + * + * @author admin + */ +@Component +public class SysRecordLogService +{ + @Autowired + private RemoteLogService remoteLogService; + + /** + * 璁板綍鐧诲綍淇℃伅 + * + * @param username 鐢ㄦ埛鍚� + * @param status 鐘舵�� + * @param message 娑堟伅鍐呭 + * @return + */ + public void recordLogininfor(String username, String status, String message) + { + SysLogininfor logininfor = new SysLogininfor(); + logininfor.setUserName(username); + logininfor.setIpaddr(IpUtils.getIpAddr()); + logininfor.setMsg(message); + // 鏃ュ織鐘舵�� + if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) + { + logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS); + } + else if (Constants.LOGIN_FAIL.equals(status)) + { + logininfor.setStatus(Constants.LOGIN_FAIL_STATUS); + } + remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER); + } +} -- Gitblit v1.9.3