|
|
|
@ -1,48 +1,45 @@
|
|
|
|
|
package com.ruoyi.framework.web.service;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
import com.ruoyi.common.enums.UserStatus;
|
|
|
|
|
import com.ruoyi.common.utils.*;
|
|
|
|
|
import com.ruoyi.system.domain.SysLogininfor;
|
|
|
|
|
import com.ruoyi.system.service.ISysLogininforService;
|
|
|
|
|
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.CacheConstants;
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.constant.UserConstants;
|
|
|
|
|
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.enums.UserStatus;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import com.ruoyi.common.exception.user.BlackListException;
|
|
|
|
|
import com.ruoyi.common.exception.user.CaptchaException;
|
|
|
|
|
import com.ruoyi.common.exception.user.CaptchaExpireException;
|
|
|
|
|
import com.ruoyi.common.exception.user.UserNotExistsException;
|
|
|
|
|
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.security.context.AuthenticationContextHolder;
|
|
|
|
|
import com.ruoyi.system.domain.SysLogininfor;
|
|
|
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
|
|
|
import com.ruoyi.system.service.ISysLogininforService;
|
|
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
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 javax.annotation.Resource;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录校验方法
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class SysLoginService
|
|
|
|
|
{
|
|
|
|
|
public class SysLoginService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
|
|
|
@ -51,7 +48,7 @@ public class SysLoginService
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
|
|
|
@ -67,45 +64,37 @@ public class SysLoginService
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SysPasswordService sysPasswordService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录验证
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
* @param password 密码
|
|
|
|
|
* @param code 验证码
|
|
|
|
|
* @param uuid 唯一标识
|
|
|
|
|
* @param code 验证码
|
|
|
|
|
* @param uuid 唯一标识
|
|
|
|
|
* @return 结果
|
|
|
|
|
*/
|
|
|
|
|
public Map<String, Object> login(String username, String password, String code, String uuid,String userType)
|
|
|
|
|
{
|
|
|
|
|
public Map<String, Object> login(String username, String password, String code, String uuid, String userType) {
|
|
|
|
|
// 验证码校验
|
|
|
|
|
validateCaptcha(username, code, uuid);
|
|
|
|
|
// 登录前置校验
|
|
|
|
|
loginPreCheck(username, password);
|
|
|
|
|
// 用户验证
|
|
|
|
|
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")));
|
|
|
|
@ -118,8 +107,8 @@ public class SysLoginService
|
|
|
|
|
|
|
|
|
|
SysUser user = loginUser.getUser();
|
|
|
|
|
// 判断用户类型
|
|
|
|
|
if (!"admin".equals(username)){
|
|
|
|
|
if (!userType.equals(user.getUserType())){
|
|
|
|
|
if (!"admin".equals(username)) {
|
|
|
|
|
if (!userType.equals(user.getUserType())) {
|
|
|
|
|
throw new ServiceException("该用户类型错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -132,7 +121,7 @@ public class SysLoginService
|
|
|
|
|
addRecord(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
|
|
|
|
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
|
|
|
|
|
}
|
|
|
|
|
sysPasswordService.validate(user);
|
|
|
|
|
// sysPasswordService.validate(user);
|
|
|
|
|
addRecord(username, Constants.LOGIN_SUCCESS, "登录成功");
|
|
|
|
|
// 生成token
|
|
|
|
|
return tokenService.createToken(loginUser);
|
|
|
|
@ -140,27 +129,23 @@ public class SysLoginService
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 校验验证码
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @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) {
|
|
|
|
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
|
|
|
if (captchaEnabled)
|
|
|
|
|
{
|
|
|
|
|
if (captchaEnabled) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
@ -169,11 +154,11 @@ public class SysLoginService
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录前置校验
|
|
|
|
|
*
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
* @param password 用户密码
|
|
|
|
|
*/
|
|
|
|
|
public void loginPreCheck(String username, String password)
|
|
|
|
|
{
|
|
|
|
|
public void loginPreCheck(String username, String password) {
|
|
|
|
|
// 用户名或密码为空 错误
|
|
|
|
|
if (StringUtils.isAnyBlank(username, password)) {
|
|
|
|
|
this.addRecord(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
|
|
|
@ -206,8 +191,7 @@ public class SysLoginService
|
|
|
|
|
*
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
*/
|
|
|
|
|
public void recordLoginInfo(Long userId)
|
|
|
|
|
{
|
|
|
|
|
public void recordLoginInfo(Long userId) {
|
|
|
|
|
SysUser sysUser = new SysUser();
|
|
|
|
|
sysUser.setUserId(userId);
|
|
|
|
|
sysUser.setLoginIp(IpUtils.getIpAddr());
|
|
|
|
@ -219,8 +203,8 @@ public class SysLoginService
|
|
|
|
|
* 记录登录信息
|
|
|
|
|
*
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
* @param status 状态
|
|
|
|
|
* @param message 消息内容
|
|
|
|
|
* @param status 状态
|
|
|
|
|
* @param message 消息内容
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public void addRecord(String username, String status, String message) {
|
|
|
|
|