| | |
| | | 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; |
| | |
| | | |
| | | /** |
| | | * 登录校验方法 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Component |
| | | public class SysLoginService |
| | | { |
| | | public class SysLoginService { |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | |
| | | |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | /** |
| | | * 登录验证 |
| | | * |
| | | * |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @return 结果 |
| | | */ |
| | | public String login(String username, String password, String code, String uuid, String openId) |
| | | { |
| | | public String login(String username, String password, String code, String uuid, String openId) { |
| | | SysUser sysUser = sysUserMapper.selectUserByUserName(username); |
| | | if (sysUser.getErrorNums() >= 3) { |
| | | //校验失败次数 |
| | | throw new BaseException("你的密码输入错误次数过多,请联系管理员处理"); |
| | | } |
| | | |
| | | boolean captchaOnOff = configService.selectCaptchaOnOff(); |
| | | // 验证码开关 |
| | | if (captchaOnOff) |
| | | { |
| | | if (captchaOnOff) { |
| | | validateCaptcha(username, code, uuid); |
| | | } |
| | | // 用户验证 |
| | | Authentication authentication = null; |
| | | try |
| | | { |
| | | try { |
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername |
| | | authentication = authenticationManager |
| | | .authenticate(new UsernamePasswordAuthenticationToken(username, password)); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | if (e instanceof BadCredentialsException) |
| | | { |
| | | 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 |
| | | { |
| | | } else { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage())); |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | |
| | | //更新openid |
| | | String userOpenId = user.getOpenId(); |
| | | |
| | | if(StringUtils.isEmpty(openId)) |
| | | { |
| | | if (StringUtils.isEmpty(openId)) { |
| | | recordLoginInfo(loginUser.getUserId()); |
| | | }else |
| | | { |
| | | if(StringUtils.isNotEmpty(userOpenId)) |
| | | { |
| | | } else { |
| | | if (StringUtils.isNotEmpty(userOpenId)) { |
| | | //当本次登录的微信OPENID不等于第一次绑定的微信OPENID就不允许登录,防止同一个账号多微信登录 |
| | | if(!userOpenId.equals(openId)) |
| | | { |
| | | if (!userOpenId.equals(openId)) { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "该账户已经绑定其他设备,须先解除绑定再登录,请跟系统管理员联系!")); |
| | | throw new ServiceException("该账户已经绑定其他设备,须先解除绑定再登录,请跟系统管理员联系!"); |
| | | }else |
| | | { |
| | | } else { |
| | | recordLoginInfo(loginUser.getUserId()); |
| | | } |
| | | }else |
| | | { |
| | | } else { |
| | | recordLoginInfo(loginUser.getUserId(), openId); |
| | | } |
| | | } |
| | | |
| | | //清除失败次数 |
| | | sysUserMapper.cleanErrorNums(username); |
| | | // 生成token |
| | | return tokenService.createToken(loginUser); |
| | | } |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * |
| | | * |
| | | * @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) { |
| | | String verifyKey = Constants.CAPTCHA_CODE_KEY + 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(); |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | |
| | | public AjaxResult wxopenidLogin(String openid) |
| | | { |
| | | public AjaxResult wxopenidLogin(String openid) { |
| | | |
| | | // 用户验证 |
| | | Authentication authentication = null; |
| | | try |
| | | { |
| | | try { |
| | | |
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername |
| | | authentication = authenticationManager |
| | | .authenticate(new WxOpenIDAuthenticationToken(openid)); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | authentication = authenticationManager.authenticate(new WxOpenIDAuthenticationToken(openid)); |
| | | } catch (Exception e) { |
| | | |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(openid, Constants.LOGIN_FAIL, e.getMessage())); |
| | | throw new WxOpenIDNotFindException(); |
| | |
| | | * |
| | | * @param userId 用户ID |
| | | */ |
| | | public void recordLoginInfo(Long userId) |
| | | { |
| | | public void recordLoginInfo(Long userId) { |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setUserId(userId); |
| | | sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest())); |
| | |
| | | } |
| | | |
| | | |
| | | public void recordLoginInfo(Long userId, String openId) |
| | | { |
| | | public void recordLoginInfo(Long userId, String openId) { |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setUserId(userId); |
| | | sysUser.setOpenId(openId); |