|  |  |  | 
|---|
|  |  |  | package com.doumee.config.jwt; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.core.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDictData; | 
|---|
|  |  |  | import com.doumee.service.business.third.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import com.doumee.core.utils.HttpsUtil; | 
|---|
|  |  |  | import com.doumee.dao.system.SystemUserMapper; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemUser; | 
|---|
|  |  |  | import io.jsonwebtoken.Jwts; | 
|---|
|  |  |  | import io.jsonwebtoken.SignatureAlgorithm; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.TimeUnit; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | 
|---|
|  |  |  | private JwtProperties jwtProperties; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemUserMapper systemUserMapper; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 生成token令牌 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | 
|---|
|  |  |  | //                this.hkLoginOut(url+"?token="+loginUserInfo.getHkMenuToken()); | 
|---|
|  |  |  | HttpsUtil.get(url+"?token="+loginUserInfo.getHkMenuToken(),true); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//删除老的token | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //删除老的token | 
|---|
|  |  |  | redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void logoutForH5(String token) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //登出海康系统数据 | 
|---|
|  |  |  | LoginUserInfo loginUserInfo = this.getUserInfoByToken(token); | 
|---|
|  |  |  | //删除老的token | 
|---|
|  |  |  | redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token); | 
|---|
|  |  |  | systemUserMapper.update(null,new UpdateWrapper<SystemUser>().lambda().set(SystemUser::getOpenid,null) | 
|---|
|  |  |  | .eq(SystemUser::getId,loginUserInfo.getId())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public void hkLoginOut(String url){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 创建HttpClient对象 | 
|---|
|  |  |  | 
|---|
|  |  |  | .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) | 
|---|
|  |  |  | .compact(); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String userTokenJsonStr = (String) redisTemplate.opsForValue().get(Constants.REDIS_USER_KEY+userInfo.getId()); | 
|---|
|  |  |  | if(StringUtils.isEmpty(userTokenJsonStr)){ | 
|---|
|  |  |  | redisTemplate.opsForValue().set(Constants.REDIS_USER_KEY+userInfo.getId(),Constants.REDIS_TOKEN_KEY+token); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | List<String> list  = Arrays.asList(userTokenJsonStr.split(",")); | 
|---|
|  |  |  | SystemDictData jointAccount = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.OPEN_JOINT_ACCOUNT); | 
|---|
|  |  |  | //关闭共用账户 需要清空其他token信息 | 
|---|
|  |  |  | if(Objects.nonNull(jointAccount)&&Constants.equalsInteger(Integer.valueOf(jointAccount.getCode()),Constants.ONE)){ | 
|---|
|  |  |  | for (String s:list) { | 
|---|
|  |  |  | redisTemplate.delete(s); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisTemplate.delete(Constants.REDIS_USER_KEY+userInfo.getId()); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(Constants.REDIS_USER_KEY+userInfo.getId(),Constants.REDIS_TOKEN_KEY+token); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | Boolean isHave = false; | 
|---|
|  |  |  | for (String s:list) { | 
|---|
|  |  |  | if(s.equals(Constants.REDIS_TOKEN_KEY+token)){ | 
|---|
|  |  |  | isHave = true; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!isHave){ | 
|---|
|  |  |  | redisTemplate.opsForValue().set(Constants.REDIS_USER_KEY+userInfo.getId(),userTokenJsonStr + "," + Constants.REDIS_TOKEN_KEY+token); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return token; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token); | 
|---|
|  |  |  | claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),LoginUserInfo.class); | 
|---|
|  |  |  | refreshTokenTime(token); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | claims = null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return claims; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向后延伸有效期保持会话继续 | 
|---|
|  |  |  | * @param token | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void refreshTokenTime(String token ) { | 
|---|
|  |  |  | log.error("===============开始刷新登录token"+token); | 
|---|
|  |  |  | redisTemplate.expire(Constants.REDIS_TOKEN_KEY+token,jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); | 
|---|
|  |  |  | log.error("===============结束刷新登录token"+token); | 
|---|
|  |  |  | //        redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,usrerInfo,jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|