| | |
| | | package com.ruoyi.framework.web.service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import com.ruoyi.rongcloud.service.RongCloudService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | | import org.springframework.security.authentication.BadCredentialsException; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | |
| | | |
| | | /** |
| | | * 登录校验方法 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Component |
| | | public class SysLoginService |
| | | { |
| | | public class SysLoginService { |
| | | |
| | | // 是否允许账户多终端同时登录(true允许 false不允许) |
| | | @Value("${token.soloLogin}") |
| | | private boolean soloLogin; |
| | | |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | |
| | | @Resource |
| | | RongCloudService rongCloudService; |
| | | |
| | | /** |
| | | * 登录验证 |
| | | * |
| | | * |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @return 结果 |
| | | */ |
| | | public String login(String username, String password, String code, String uuid) |
| | | { |
| | | public String login(String username, String password, String code, String uuid) { |
| | | |
| | | boolean captchaEnabled = configService.selectCaptchaEnabled(); |
| | | // 验证码开关 |
| | | if (captchaEnabled) |
| | | { |
| | | if (captchaEnabled) { |
| | | validateCaptcha(username, code, uuid); |
| | | } |
| | | // 用户验证 |
| | | Authentication authentication = null; |
| | | try |
| | | { |
| | | try { |
| | | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); |
| | | AuthenticationContextHolder.setContext(authenticationToken); |
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername |
| | | authentication = authenticationManager.authenticate(authenticationToken); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (e instanceof BadCredentialsException) |
| | | { |
| | | } catch (Exception e) { |
| | | if (e instanceof BadCredentialsException) { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); |
| | | throw new UserPasswordNotMatchException(); |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage())); |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | finally |
| | | { |
| | | } finally { |
| | | AuthenticationContextHolder.clearContext(); |
| | | } |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| | | LoginUser loginUser = (LoginUser) authentication.getPrincipal(); |
| | | recordLoginInfo(loginUser.getUserId()); |
| | | if (!soloLogin) |
| | | { |
| | | // 如果用户不允许多终端同时登录,清除缓存信息 |
| | | String userIdKey = Constants.LOGIN_USERID_KEY + loginUser.getUser().getUserId(); |
| | | String userKey = redisCache.getCacheObject(userIdKey); |
| | | if (StringUtils.isNotEmpty(userKey)) |
| | | { |
| | | redisCache.deleteObject(userIdKey); |
| | | redisCache.deleteObject(userKey); |
| | | } |
| | | } |
| | | getRongCloudToken(loginUser); |
| | | // 生成token |
| | | return tokenService.createToken(loginUser); |
| | | String token = tokenService.createToken(loginUser); |
| | | return token; |
| | | } |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * |
| | | * |
| | | * @param username 用户名 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @return 结果 |
| | | */ |
| | | public void validateCaptcha(String username, String code, String uuid) |
| | | { |
| | | public void validateCaptcha(String username, String code, String uuid) { |
| | | String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, ""); |
| | | String captcha = redisCache.getCacheObject(verifyKey); |
| | | redisCache.deleteObject(verifyKey); |
| | | if (captcha == null) |
| | | { |
| | | if (captcha == null) { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"))); |
| | | throw new CaptchaExpireException(); |
| | | } |
| | | if (!code.equalsIgnoreCase(captcha)) |
| | | { |
| | | if (!code.equalsIgnoreCase(captcha)) { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"))); |
| | | throw new CaptchaException(); |
| | | } |
| | |
| | | * |
| | | * @param userId 用户ID |
| | | */ |
| | | public void recordLoginInfo(String userId) |
| | | { |
| | | public void recordLoginInfo(String userId) { |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setUserId(userId); |
| | | sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest())); |
| | | sysUser.setLoginDate(DateUtils.getNowDate()); |
| | | userService.updateUserProfile(sysUser); |
| | | } |
| | | /** |
| | | * 获取并记录融云token |
| | | * |
| | | * @param loginUser 登录用户 |
| | | */ |
| | | @Async |
| | | public String getRongCloudToken(LoginUser loginUser) { |
| | | try { |
| | | String userId = loginUser.getUserId(); |
| | | String nickName = loginUser.getUser().getNickName(); |
| | | String token = rongCloudService.getToken(userId, nickName); |
| | | loginUser.setRongCloudToken(token); |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setUserId(userId); |
| | | sysUser.setRongCloudToken(token); |
| | | userService.updateUserProfile(sysUser); |
| | | return token; |
| | | } catch (Exception ex) { |
| | | throw ex; |
| | | } |
| | | } |
| | | } |