| | |
| | | package com.ruoyi.framework.web.service; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | 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; |
| | | |
| | |
| | | |
| | | /** |
| | | * 登录验证 |
| | | * |
| | | * |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @return 结果 |
| | | */ |
| | | public String login(String username, String password, String code, String uuid) |
| | | { |
| | | public String login(String username, String password, String code, String uuid) { |
| | | boolean captchaOnOff = configService.selectCaptchaOnOff(); |
| | | // 验证码开关 |
| | | if (captchaOnOff) |
| | | { |
| | | if (captchaOnOff) { |
| | | validateCaptcha(username, code, uuid); |
| | | } |
| | | return socialLogin(username,password); |
| | | return socialLogin(username, password); |
| | | } |
| | | |
| | | /** |
| | | * 第三方验证后,调用登录方法 |
| | | * |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | * @return token |
| | | */ |
| | | public String socialLogin(String username, String password){ |
| | | public String socialLogin(String username, String password) { |
| | | // 用户验证 |
| | | Authentication authentication = null; |
| | | try |
| | | { |
| | | try { |
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername |
| | | authentication = authenticationManager |
| | | .authenticate(new UsernamePasswordAuthenticationToken(username, password)); |
| | | } |
| | | 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()); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 跳转登录认证接口 |
| | | * |
| | | * @param username |
| | | * @param encodePwd |
| | | * @return |
| | | */ |
| | | public String redirectLogin(String username,String encodePwd){ |
| | | UserDetails userDetails=userDetailsServiceImpl.loadUserByUsername(username); |
| | | if(!userDetails.getPassword().equals(encodePwd)){ |
| | | public String redirectLogin(String username, String encodePwd) { |
| | | UserDetails userDetails = userDetailsServiceImpl.loadUserByUsername(username); |
| | | if (!userDetails.getPassword().equals(encodePwd)) { |
| | | throw new UserPasswordNotMatchException(); |
| | | } |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| | |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * |
| | | * |
| | | * @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(); |
| | | } |
| | |
| | | * |
| | | * @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())); |