| | |
| | | package com.gkhy.exam.framework.web.service; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import com.gkhy.exam.common.constant.CacheConstant; |
| | | import com.gkhy.exam.common.constant.Constant; |
| | | import com.gkhy.exam.common.constant.UserConstant; |
| | | import com.gkhy.exam.common.domain.entity.SysUser; |
| | |
| | | import com.gkhy.exam.common.enums.LoginUserTagEnum; |
| | | import com.gkhy.exam.common.exception.ApiException; |
| | | import com.gkhy.exam.common.utils.IpUtils; |
| | | import com.gkhy.exam.common.utils.RedisUtils; |
| | | import com.gkhy.exam.framework.manager.AsyncManager; |
| | | import com.gkhy.exam.framework.manager.factory.AsyncFactory; |
| | | import com.gkhy.exam.framework.security.context.AuthenticationContextHolder; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.time.LocalDateTime; |
| | | |
| | | @Component |
| | |
| | | private TokenService tokenService; |
| | | @Autowired |
| | | private ExStudentService studentService; |
| | | @Autowired |
| | | private HttpServletRequest request; |
| | | |
| | | @Autowired |
| | | private RedisUtils redisUtils; |
| | | |
| | | |
| | | |
| | |
| | | String password=loginBody.getPassword(); |
| | | password= Base64.decodeStr(password); |
| | | //验证码校验 |
| | | //validateCaptcha(username,loginBody.code,loginBody.uuid); |
| | | validateCaptcha(username,loginBody.getCode(),loginBody.getUuid()); |
| | | loginPreCheck(username, password); |
| | | Authentication authentication=null; |
| | | try{ |
| | |
| | | authentication = authenticationManager.authenticate(authenticationToken); |
| | | LoginUserDetails loginUserDetails= (LoginUserDetails) authentication.getPrincipal(); |
| | | passwordService.validate(loginUserDetails.getUser(),password); |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功")); |
| | | // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功")); |
| | | recordLoginInfo(loginUserDetails.getUser().getId(),LoginUserTagEnum.ADMIN_USER); |
| | | return createLoginUser(loginUserDetails,LoginUserTagEnum.ADMIN_USER); |
| | | }catch (Exception e){ |
| | | if (e instanceof BadCredentialsException) |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配")); |
| | | // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配")); |
| | | throw new ApiException("用户密码不匹配"); |
| | | } |
| | | else |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage())); |
| | | // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage())); |
| | | throw new ApiException(e.getMessage()); |
| | | } |
| | | }finally { |
| | |
| | | authentication = authenticationManager.authenticate(authenticationToken); |
| | | LoginUserDetails loginUserDetails= (LoginUserDetails) authentication.getPrincipal(); |
| | | passwordService.validate(loginUserDetails.getUser(),password); |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功")); |
| | | // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_SUCCESS, "登录成功")); |
| | | recordLoginInfo(loginUserDetails.getUser().getId(),LoginUserTagEnum.STUDENT_USER); |
| | | return createLoginUser(loginUserDetails,LoginUserTagEnum.STUDENT_USER); |
| | | }catch (Exception e){ |
| | | if (e instanceof BadCredentialsException) |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配")); |
| | | // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, "用户密码不匹配")); |
| | | throw new ApiException("用户密码不匹配"); |
| | | } |
| | | else |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage())); |
| | | // AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constant.LOGIN_FAIL, e.getMessage())); |
| | | throw new ApiException(e.getMessage()); |
| | | } |
| | | }finally { |
| | |
| | | // } |
| | | } |
| | | |
| | | /** |
| | | * 校验验证码 |
| | | * |
| | | * @param username 用户名 |
| | | * @param code 验证码 |
| | | * @param uuid 唯一标识 |
| | | * @return 结果 |
| | | */ |
| | | public void validateCaptcha(String username, String code, String uuid) |
| | | { |
| | | if(StringUtils.isBlank(code)||StringUtils.isBlank(uuid)){ |
| | | throw new ApiException("验证码或验证码标识为空"); |
| | | } |
| | | String verifyKey = CacheConstant.CAPTCHA_CODE_KEY +uuid; |
| | | String captcha = (String) redisUtils.get(verifyKey); |
| | | redisUtils.del(verifyKey); |
| | | if (StringUtils.isBlank(captcha)) |
| | | { |
| | | throw new ApiException("验证码已失效"); |
| | | } |
| | | if (!code.equalsIgnoreCase(captcha)) |
| | | { |
| | | throw new ApiException("验证码不正确"); |
| | | } |
| | | } |
| | | |
| | | public void logout(){ |
| | | tokenService.delTokenCache(request); |
| | | } |
| | | |
| | | |
| | | |
| | | |