| | |
| | | package com.ruoyi.system.service; |
| | | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.secure.BCrypt; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.ruoyi.common.constant.CacheConstants; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.dto.RoleDTO; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | |
| | | */ |
| | | public String login(String username, String password, String code, String uuid) { |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | boolean captchaOnOff = configService.selectCaptchaOnOff(); |
| | | boolean captchaEnabled = configService.selectCaptchaEnabled(); |
| | | // 验证码开关 |
| | | if (captchaOnOff) { |
| | | if (captchaEnabled) { |
| | | validateCaptcha(username, code, uuid, request); |
| | | } |
| | | SysUser user = loadUserByUsername(username); |
| | |
| | | return StpUtil.getTokenValue(); |
| | | } |
| | | |
| | | |
| | | public void logout(String loginName) { |
| | | asyncService.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest()); |
| | | /** |
| | | * 退出登录 |
| | | */ |
| | | public void logout() { |
| | | try { |
| | | String username = LoginHelper.getUsername(); |
| | | StpUtil.logout(); |
| | | asyncService.recordLogininfor(username, Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest()); |
| | | } catch (NotLoginException e) { |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验短信验证码 |
| | | */ |
| | | private boolean validateSmsCode(String phonenumber, String smsCode, HttpServletRequest request) { |
| | | String code = RedisUtils.getCacheObject(Constants.CAPTCHA_CODE_KEY + phonenumber); |
| | | String code = RedisUtils.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + phonenumber); |
| | | if (StringUtils.isBlank(code)) { |
| | | asyncService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request); |
| | | throw new CaptchaExpireException(); |
| | |
| | | * @param uuid 唯一标识 |
| | | */ |
| | | public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) { |
| | | String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); |
| | | String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); |
| | | String captcha = RedisUtils.getCacheObject(verifyKey); |
| | | RedisUtils.deleteObject(verifyKey); |
| | | if (captcha == null) { |
| | |
| | | */ |
| | | private void checkLogin(LoginType loginType, String username, Supplier<Boolean> supplier) { |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | String errorKey = Constants.LOGIN_ERROR + username; |
| | | String errorKey = CacheConstants.LOGIN_ERROR + username; |
| | | Integer errorLimitTime = Constants.LOGIN_ERROR_LIMIT_TIME; |
| | | Integer setErrorNumber = Constants.LOGIN_ERROR_NUMBER; |
| | | String loginFail = Constants.LOGIN_FAIL; |