From ed19109e80a10fd4def53bae13a59f8a917c8fa1 Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期三, 28 九月 2022 16:49:21 +0800 Subject: [PATCH] 导入更新用户数据前校验数据权限 --- src/main/java/com/ruoyi/framework/security/service/SysLoginService.java | 36 ++++++++++++++++++++++++------------ 1 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/ruoyi/framework/security/service/SysLoginService.java b/src/main/java/com/ruoyi/framework/security/service/SysLoginService.java index 4bfa2be..9b7380e 100644 --- a/src/main/java/com/ruoyi/framework/security/service/SysLoginService.java +++ b/src/main/java/com/ruoyi/framework/security/service/SysLoginService.java @@ -7,19 +7,22 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Component; +import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.constant.Constants; -import com.ruoyi.common.exception.CustomException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.exception.user.CaptchaException; import com.ruoyi.common.exception.user.CaptchaExpireException; import com.ruoyi.common.exception.user.UserPasswordNotMatchException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.ServletUtils; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.ip.IpUtils; import com.ruoyi.framework.manager.AsyncManager; import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.framework.redis.RedisCache; import com.ruoyi.framework.security.LoginUser; +import com.ruoyi.framework.security.context.AuthenticationContextHolder; import com.ruoyi.project.system.domain.SysUser; import com.ruoyi.project.system.service.ISysConfigService; import com.ruoyi.project.system.service.ISysUserService; @@ -58,9 +61,9 @@ */ public String login(String username, String password, String code, String uuid) { - boolean captchaOnOff = configService.selectCaptchaOnOff(); + boolean captchaEnabled = configService.selectCaptchaEnabled(); // 楠岃瘉鐮佸紑鍏� - if (captchaOnOff) + if (captchaEnabled) { validateCaptcha(username, code, uuid); } @@ -68,9 +71,10 @@ Authentication authentication = null; try { + UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); + AuthenticationContextHolder.setContext(authenticationToken); // 璇ユ柟娉曚細鍘昏皟鐢║serDetailsServiceImpl.loadUserByUsername - authentication = authenticationManager - .authenticate(new UsernamePasswordAuthenticationToken(username, password)); + authentication = authenticationManager.authenticate(authenticationToken); } catch (Exception e) { @@ -82,12 +86,16 @@ else { AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage())); - throw new CustomException(e.getMessage()); + throw new ServiceException(e.getMessage()); } + } + finally + { + AuthenticationContextHolder.clearContext(); } AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); LoginUser loginUser = (LoginUser) authentication.getPrincipal(); - recordLoginInfo(loginUser.getUser()); + recordLoginInfo(loginUser.getUserId()); // 鐢熸垚token return tokenService.createToken(loginUser); } @@ -102,7 +110,7 @@ */ public void validateCaptcha(String username, String code, String uuid) { - String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; + String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, ""); String captcha = redisCache.getCacheObject(verifyKey); redisCache.deleteObject(verifyKey); if (captcha == null) @@ -119,11 +127,15 @@ /** * 璁板綍鐧诲綍淇℃伅 + * + * @param userId 鐢ㄦ埛ID */ - public void recordLoginInfo(SysUser user) + public void recordLoginInfo(Long userId) { - user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest())); - user.setLoginDate(DateUtils.getNowDate()); - userService.updateUserProfile(user); + SysUser sysUser = new SysUser(); + sysUser.setUserId(userId); + sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest())); + sysUser.setLoginDate(DateUtils.getNowDate()); + userService.updateUserProfile(sysUser); } } -- Gitblit v1.9.3