123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package com.bizmatics.service.system.impl;
- import com.bizmatics.common.core.exception.BusinessException;
- import com.bizmatics.common.mvc.utils.IpUtils;
- import com.bizmatics.common.mvc.utils.ServletUtils;
- import com.bizmatics.common.spring.config.redis.RedisHelper;
- import com.bizmatics.model.constants.Constants;
- import com.bizmatics.model.system.SysUser;
- import com.bizmatics.service.system.ISysConfigService;
- import com.bizmatics.service.system.ISysUserService;
- import com.bizmatics.service.config.security.LoginUser;
- import com.bizmatics.service.util.manager.AsyncManager;
- import com.bizmatics.service.util.manager.MessageUtils;
- import com.bizmatics.service.util.manager.factory.AsyncFactory;
- 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.Date;
- /**
- * 登录校验方法
- *
- * @author ruoyi
- */
- @Component
- public class SysLoginService
- {
- @Autowired
- private TokenService tokenService;
- @Resource
- private AuthenticationManager authenticationManager;
- @Autowired
- private RedisHelper redisHelper;
-
- @Autowired
- private ISysUserService userService;
- @Autowired
- private ISysConfigService configService;
- /**
- * 登录验证
- *
- * @param username 用户名
- * @param password 密码
- * @param code 验证码
- * @param uuid 唯一标识
- * @return 结果
- */
- public String login(String username, String password, String code, String uuid)
- {
- 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")));
- throw new BusinessException("添加异常");
- }
- else
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
- throw new BusinessException(e.getMessage());
- }
- }
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
- recordLoginInfo(loginUser.getUser());
- // 生成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 = redisHelper.get(verifyKey).toString();
- redisHelper.delete(verifyKey);
- if (captcha == null)
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
- throw new BusinessException("发送异常");
- }
- if (!code.equalsIgnoreCase(captcha))
- {
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
- throw new BusinessException("发送异常");
- }
- }
- /**
- * 记录登录信息
- */
- public void recordLoginInfo(SysUser user)
- {
- user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
- user.setLoginDate(new Date());
- userService.updateUserProfile(user);
- }
- }
|