|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class SysLoginService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 是否允许账户多终端同时登录(true允许 false不允许) | 
|---|
|  |  |  | @Value("${token.soloLogin}") | 
|---|
|  |  |  | private boolean soloLogin; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private TokenService tokenService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ISysConfigService configService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | RongCloudService rongCloudService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return 结果 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public String login(String username, String password, String code, String uuid) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | boolean captchaEnabled = configService.selectCaptchaEnabled(); | 
|---|
|  |  |  | // 验证码开关 | 
|---|
|  |  |  | if (captchaEnabled) { | 
|---|
|  |  |  | 
|---|
|  |  |  | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); | 
|---|
|  |  |  | LoginUser loginUser = (LoginUser) authentication.getPrincipal(); | 
|---|
|  |  |  | recordLoginInfo(loginUser.getUserId()); | 
|---|
|  |  |  | // 生成token | 
|---|
|  |  |  | String token = tokenService.createToken(loginUser); | 
|---|
|  |  |  | //获取融云token | 
|---|
|  |  |  | getRongCloudToken(loginUser); | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public String login(String username, String password, String code, String uuid,Boolean isApp) { | 
|---|
|  |  |  | boolean captchaEnabled = configService.selectCaptchaEnabled(); | 
|---|
|  |  |  | // 验证码开关 | 
|---|
|  |  |  | if (captchaEnabled) { | 
|---|
|  |  |  | validateCaptcha(username, code, uuid); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 用户验证 | 
|---|
|  |  |  | Authentication authentication = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); | 
|---|
|  |  |  | AuthenticationContextHolder.setContext(authenticationToken); | 
|---|
|  |  |  | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername | 
|---|
|  |  |  | authentication = authenticationManager.authenticate(authenticationToken); | 
|---|
|  |  |  | } 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 { | 
|---|
|  |  |  | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, 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.getUserId()); | 
|---|
|  |  |  | // 生成token | 
|---|
|  |  |  | if(StringUtils.isNotNull(isApp)) | 
|---|
|  |  |  | if (!soloLogin) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | loginUser.setIsApp(isApp); | 
|---|
|  |  |  | // 如果用户不允许多终端同时登录,清除缓存信息 | 
|---|
|  |  |  | String userIdKey = Constants.LOGIN_USERID_KEY + loginUser.getUser().getUserId(); | 
|---|
|  |  |  | String userKey = redisCache.getCacheObject(userIdKey); | 
|---|
|  |  |  | if (StringUtils.isNotEmpty(userKey)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | redisCache.deleteObject(userIdKey); | 
|---|
|  |  |  | redisCache.deleteObject(userKey); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String token = tokenService.createToken(loginUser); | 
|---|
|  |  |  | //获取融云token | 
|---|
|  |  |  | getRongCloudToken(loginUser); | 
|---|
|  |  |  | // 生成token | 
|---|
|  |  |  | String token = tokenService.createToken(loginUser); | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param loginUser 登录用户 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void getRongCloudToken(LoginUser 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; | 
|---|
|  |  |  | } | 
|---|