| | |
| | | import com.gkhy.assess.common.enums.ApproveStatusEnum; |
| | | import com.gkhy.assess.common.enums.UserIdentityEnum; |
| | | import com.gkhy.assess.common.enums.UserStatusEnum; |
| | | import com.gkhy.assess.common.exception.ApiException; |
| | | import com.gkhy.assess.common.utils.JwtTokenUtil; |
| | | import com.gkhy.assess.common.utils.RedisUtils; |
| | | import com.gkhy.assess.system.domain.SysUser; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.time.LocalDateTime; |
| | | |
| | | @Component |
| | |
| | | private SysPasswordService passwordService; |
| | | @Autowired |
| | | private RedisUtils redisUtils; |
| | | @Autowired |
| | | private HttpServletRequest request; |
| | | |
| | | public SysUser login(String username, String password) { |
| | | SysUser sysUser=sysUserService.getUserByUsernamePhone(username); |
| | | public SysUser login(String username, String password,Integer identity) { |
| | | SysUser sysUser=sysUserService.getUserByUsernamePhone(username,identity); |
| | | validUser(sysUser); |
| | | passwordService.validate(sysUser,password); |
| | | recordLoginInfo(sysUser.getId()); |
| | |
| | | if(sysUser==null) { |
| | | throw new AuthenticationException("用户不存在"); |
| | | } |
| | | String uri=request.getRequestURI(); |
| | | if(uri.startsWith("/api/system")){ |
| | | if(UserIdentityEnum.EXPERT.getCode().equals(sysUser.getIdentity())){ |
| | | throw new AuthenticationException("专家用户无权登录后台"); |
| | | } |
| | | }else if(uri.startsWith("/api/app/")){ |
| | | if(!UserIdentityEnum.EXPERT.getCode().equals(sysUser.getIdentity())){ |
| | | throw new AuthenticationException("只有专家用户才能登录APP"); |
| | | } |
| | | } |
| | | if(UserStatusEnum.DELETED.getCode().equals(sysUser.getDelFlag())){ |
| | | throw new AuthenticationException("用户已被删除"); |
| | | } |
| | | if(UserStatusEnum.DISABLE.getCode().equals(sysUser.getStatus())){ |
| | | throw new AuthenticationException("用户已被停用"); |
| | | } |
| | | if(UserIdentityEnum.AGENCY.getCode().equals(sysUser.getIdentity())){ |
| | | if(!ApproveStatusEnum.APPROVED.getCode().equals(sysUser.getState())){ |
| | | throw new AuthenticationException("机构账户审批还未通过"); |
| | | } |
| | | } |
| | | // if(UserIdentityEnum.AGENCY.getCode().equals(sysUser.getIdentity())){ |
| | | // if(!ApproveStatusEnum.APPROVED.getCode().equals(sysUser.getState())){ |
| | | // throw new AuthenticationException("机构账户审批还未通过"); |
| | | // } |
| | | // } |
| | | } |
| | | |
| | | public SysUser validJwtToken(String jwtToken){ |
| | | |
| | | String username= JwtTokenUtil.getUsername(jwtToken); |
| | | if(StringUtils.isEmpty(username)){ |
| | | Integer identity=JwtTokenUtil.getIdentity(jwtToken); |
| | | if(StringUtils.isEmpty(username)||identity==null){ |
| | | throw new AuthenticationException("token非法无效!"); |
| | | } |
| | | SysUser sysUser=sysUserService.getUserByUsernamePhone(username); |
| | | SysUser sysUser=sysUserService.getUserByUsernamePhone(username,identity); |
| | | validUser(sysUser); |
| | | if(!jwtTokenRefresh(jwtToken,username,sysUser.getPassword())){ |
| | | throw new AuthenticationException("Token失效,请重新登录!"); |
| | | if(!JwtTokenUtil.verify(jwtToken,username,sysUser.getPassword(),identity)){ |
| | | throw new AuthenticationException("token非法无效!"); |
| | | } |
| | | if(!jwtTokenRefresh(jwtToken,username,sysUser.getPassword(),identity)){ |
| | | throw new AuthenticationException("您的账号登录过期,请重新登录!"); |
| | | } |
| | | // setRolePermission(sysUser); |
| | | return sysUser; |
| | |
| | | * @param passWord |
| | | * @return |
| | | */ |
| | | public boolean jwtTokenRefresh(String jwtToken,String username,String passWord){ |
| | | public boolean jwtTokenRefresh(String jwtToken, String username, String passWord, Integer identity){ |
| | | String tokenKey=redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN+":"+JwtTokenUtil.md5Encode(jwtToken)); |
| | | String userKey=redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN+":"+username); |
| | | // String userKey=redisUtils.generateKey(CacheConstant.SYS_USER_TOKEN+":"+username+"_"+identity); |
| | | String cacheToken= (String) redisUtils.get(tokenKey); |
| | | if(StringUtils.isNotEmpty(cacheToken)){ |
| | | // 校验token有效性 |
| | | if(!JwtTokenUtil.verify(cacheToken,username,passWord)){ |
| | | String newToken=JwtTokenUtil.sign(username,passWord); |
| | | if(!JwtTokenUtil.isNeedUpdate(cacheToken,username,passWord,identity)){ |
| | | String newToken=JwtTokenUtil.sign(username,passWord,identity); |
| | | // 设置超时时间 |
| | | redisUtils.set(tokenKey,newToken); |
| | | redisUtils.expire(tokenKey,JwtTokenUtil.EXPIRATION*2/1000); |
| | | redisUtils.expire(userKey,(JwtTokenUtil.EXPIRATION*2/1000)+2); |
| | | redisUtils.set(tokenKey,newToken,JwtTokenUtil.EXPIRATION*2/1000); |
| | | // redisUtils.expire(userKey,(JwtTokenUtil.EXPIRATION*2/1000)+2); |
| | | } |
| | | return true; |
| | | } |