|
|
|
package com.ruoyi.system.login.service;
|
|
|
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
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.StringUtils;
|
|
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
|
|
import com.ruoyi.system.domain.entity.SysUser;
|
|
|
|
import com.ruoyi.system.login.dto.ApiLoginUser;
|
|
|
|
import com.ruoyi.system.login.dto.SysLoginUser;
|
|
|
|
import com.ruoyi.system.login.dto.Yu2leUserLogin;
|
|
|
|
import com.ruoyi.system.login.dto.Yu2leLoginToken;
|
|
|
|
import com.ruoyi.system.login.manager.AsyncManager;
|
|
|
|
import com.ruoyi.system.login.manager.factory.AsyncFactory;
|
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
import com.ruoyi.system.service.PublicService;
|
|
|
|
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.Service;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 登陆业务
|
|
|
|
*/
|
|
|
|
@Service
|
|
|
|
public class LoginService {
|
|
|
|
@Autowired
|
|
|
|
private PublicService publicService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private AuthenticationManager authenticationManager;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 管理员用户名密码登录验证
|
|
|
|
*
|
|
|
|
* @param username 用户名
|
|
|
|
* @param password 密码
|
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
|
|
|
* @return 结果
|
|
|
|
*/
|
|
|
|
public String sysUserLogin(String username, String password, String code, String uuid)
|
|
|
|
{
|
|
|
|
// boolean captchaOnOff = configService.selectCaptchaOnOff();
|
|
|
|
// // 验证码开关
|
|
|
|
// if (captchaOnOff)
|
|
|
|
// {
|
|
|
|
// validateCaptcha(username, code, uuid);
|
|
|
|
// }
|
|
|
|
// 用户验证
|
|
|
|
Authentication authentication = userPasswordVerification(username,password);
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("sysuser.login.success")));
|
|
|
|
SysLoginUser loginUser = (SysLoginUser) authentication.getPrincipal();
|
|
|
|
recordLoginInfo(loginUser.getUserId());
|
|
|
|
// 生成token
|
|
|
|
return tokenService.createToken(loginUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* api用户名密码登陆验证
|
|
|
|
* @param user
|
|
|
|
* @param pass
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String apiLoginByPass(String user,String pass) {
|
|
|
|
// 用户验证
|
|
|
|
Authentication authentication = userPasswordVerification(user,pass);
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(user, Constants.LOGIN_SUCCESS, MessageUtils.message("apiuser.login.success")));
|
|
|
|
ApiLoginUser loginUser = (ApiLoginUser) authentication.getPrincipal();
|
|
|
|
recordLoginInfo(loginUser.getUserId());
|
|
|
|
return tokenService.createToken(loginUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 刷新api端token
|
|
|
|
* @param token
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String refreshApiToken(String token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 鱼儿乐token登陆
|
|
|
|
* @param token
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String yu2leTokenLogin(String token) {
|
|
|
|
Yu2leLoginToken loginToken = new Yu2leLoginToken(token);
|
|
|
|
Yu2leUserLogin userInfo = publicService.getObjectForTableName(Yu2leUserLogin.class,"id",loginToken.getUserId()+"","`liu_yu_le`.`user_login`");
|
|
|
|
return apiLoginByPass(userInfo.getLoginName(),userInfo.getLoginPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 记录登录信息
|
|
|
|
*
|
|
|
|
* @param userId 用户ID
|
|
|
|
*/
|
|
|
|
private void recordLoginInfo(Long userId)
|
|
|
|
{
|
|
|
|
SysUser sysUser = new SysUser();
|
|
|
|
sysUser.setUserId(userId);
|
|
|
|
sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
|
userService.updateUserProfile(sysUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 校验验证码
|
|
|
|
*
|
|
|
|
* @param username 用户名
|
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
|
|
|
* @return 结果
|
|
|
|
*/
|
|
|
|
private void validateCaptcha(String username, String code, String uuid)
|
|
|
|
{
|
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.nvl(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Authentication userPasswordVerification(String username,String password)
|
|
|
|
{
|
|
|
|
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")));
|
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
|
throw new ServiceException(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return authentication;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|