package com.ruoyi.framework.web.service;
|
|
import javax.annotation.Resource;
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.exception.base.BaseException;
|
import com.ruoyi.common.exception.user.WxOpenIDNotFindException;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.framework.wxopenidConfig.WxOpenIDAuthenticationToken;
|
import com.ruoyi.system.mapper.SysUserMapper;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.core.Authentication;
|
import org.springframework.stereotype.Component;
|
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
import com.ruoyi.common.core.redis.RedisCache;
|
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.ip.IpUtils;
|
import com.ruoyi.framework.manager.AsyncManager;
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
import com.ruoyi.system.service.ISysConfigService;
|
import com.ruoyi.system.service.ISysUserService;
|
|
/**
|
* 登录校验方法
|
*
|
* @author ruoyi
|
*/
|
@Component
|
public class SysLoginService {
|
@Autowired
|
private TokenService tokenService;
|
|
@Resource
|
private AuthenticationManager authenticationManager;
|
|
@Autowired
|
private RedisCache redisCache;
|
|
@Autowired
|
private ISysUserService userService;
|
|
@Autowired
|
private ISysConfigService configService;
|
|
|
@Autowired
|
private SysUserMapper sysUserMapper;
|
|
/**
|
* 登录验证
|
*
|
* @param username 用户名
|
* @param password 密码
|
* @param code 验证码
|
* @param uuid 唯一标识
|
* @return 结果
|
*/
|
public String login(String username, String password, String code, String uuid, String openId, String dingUserId) {
|
//免登
|
if (StringUtils.isNotEmpty(dingUserId)) {
|
SysUser sysUser = sysUserMapper.selectUserByDingUserId(dingUserId);
|
LoginUser loginUser = new LoginUser();
|
loginUser.setUserId(sysUser.getUserId());
|
loginUser.setDeptId(sysUser.getDeptId());
|
loginUser.setUser(sysUser);
|
return tokenService.createToken(loginUser);
|
}
|
|
SysUser sysUser = sysUserMapper.selectUserByUserName(username);
|
if (sysUser.getErrorNums() >= 3) {
|
//校验失败次数
|
throw new BaseException("你的密码输入错误次数过多,请联系管理员处理");
|
}
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
// 验证码开关
|
if (captchaOnOff) {
|
validateCaptcha(username, code, uuid);
|
}
|
// 用户验证
|
Authentication authentication = null;
|
try {
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
} catch (Exception e) {
|
if (e instanceof BadCredentialsException) {
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
sysUserMapper.updateErrorNums(sysUser);
|
throw new UserPasswordNotMatchException();
|
} else {
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
throw new ServiceException(e.getMessage());
|
}
|
}
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
SysUser user = loginUser.getUser();
|
//更新openid
|
String userOpenId = user.getOpenId();
|
|
if (StringUtils.isEmpty(openId)) {
|
recordLoginInfo(loginUser.getUserId());
|
} else {
|
if (StringUtils.isNotEmpty(userOpenId)) {
|
//当本次登录的微信OPENID不等于第一次绑定的微信OPENID就不允许登录,防止同一个账号多微信登录
|
if (!userOpenId.equals(openId)) {
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "该账户已经绑定其他设备,须先解除绑定再登录,请跟系统管理员联系!"));
|
throw new ServiceException("该账户已经绑定其他设备,须先解除绑定再登录,请跟系统管理员联系!");
|
} else {
|
recordLoginInfo(loginUser.getUserId());
|
}
|
} else {
|
recordLoginInfo(loginUser.getUserId(), openId);
|
}
|
}
|
//清除失败次数
|
sysUserMapper.cleanErrorNums(username);
|
// 生成token
|
return tokenService.createToken(loginUser);
|
}
|
|
/**
|
* 校验验证码
|
*
|
* @param username 用户名
|
* @param code 验证码
|
* @param uuid 唯一标识
|
* @return 结果
|
*/
|
public void validateCaptcha(String username, String code, String uuid) {
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
String captcha = redisCache.getCacheObject(verifyKey);
|
redisCache.deleteObject(verifyKey);
|
if (captcha == null) {
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
throw new CaptchaExpireException();
|
}
|
if (!code.equalsIgnoreCase(captcha)) {
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
throw new CaptchaException();
|
}
|
}
|
|
/**
|
* 微信OpenID登录验证
|
*
|
* @param openid 微信OpenID
|
* @return 结果
|
*/
|
|
public AjaxResult wxopenidLogin(String openid) {
|
|
// 用户验证
|
Authentication authentication = null;
|
try {
|
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
authentication = authenticationManager.authenticate(new WxOpenIDAuthenticationToken(openid));
|
} catch (Exception e) {
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(openid, Constants.LOGIN_FAIL, e.getMessage()));
|
throw new WxOpenIDNotFindException();
|
|
}
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(openid, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
AjaxResult ajax = AjaxResult.success();
|
|
// 生成token
|
String token = tokenService.createToken(loginUser);
|
ajax.put(Constants.TOKEN, token);
|
return ajax;
|
}
|
|
/**
|
* 记录登录信息
|
*
|
* @param userId 用户ID
|
*/
|
public void recordLoginInfo(Long userId) {
|
SysUser sysUser = new SysUser();
|
sysUser.setUserId(userId);
|
sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
userService.updateUserProfile(sysUser);
|
}
|
|
|
public void recordLoginInfo(Long userId, String openId) {
|
SysUser sysUser = new SysUser();
|
sysUser.setUserId(userId);
|
sysUser.setOpenId(openId);
|
sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
userService.updateUserProfile(sysUser);
|
}
|
|
}
|