|  |  | 
 |  |  | package com.doumee.config.jwt; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSONArray; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.doumee.core.model.LoginUserInfo; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
 |  |  | 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 org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.apache.http.HttpEntity; | 
 |  |  | import org.apache.http.HttpResponse; | 
 |  |  | import org.apache.http.client.HttpClient; | 
 |  |  | import org.apache.http.client.methods.HttpGet; | 
 |  |  | import org.apache.http.impl.client.HttpClientBuilder; | 
 |  |  | import org.apache.http.util.EntityUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.concurrent.TimeUnit; | 
 |  |  |  | 
 |  |  | @Component | 
 |  |  | @Slf4j | 
 |  |  | public class JwtTokenUtil { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RedisTemplate<String,Object> redisTemplate; | 
 |  |  |     @Resource | 
 |  |  |     private JwtProperties jwtProperties; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz ; | 
 |  |  |     @Autowired | 
 |  |  |     private SystemUserMapper systemUserMapper; | 
 |  |  |     /** | 
 |  |  |      * 生成token令牌 | 
 |  |  |      * | 
 |  |  | 
 |  |  |      */ | 
 |  |  |     public void logout(String token) { | 
 |  |  |         try { | 
 |  |  |             redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//删除老的token | 
 |  |  |             //登出海康系统数据 | 
 |  |  |             LoginUserInfo loginUserInfo = this.getUserInfoByToken(token); | 
 |  |  |             String url = systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_HTTPS).getCode() + | 
 |  |  |                     systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_HOST).getCode() + | 
 |  |  |                     systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.LOGIN_OUT_URL).getCode(); | 
 |  |  |             if(StringUtils.isNotBlank(loginUserInfo.getHkMenuToken())){ | 
 |  |  |                 log.info("调起海康退出登录=======================>"+url+"?token="+loginUserInfo.getHkMenuToken()); | 
 |  |  | //                this.hkLoginOut(url+"?token="+loginUserInfo.getHkMenuToken()); | 
 |  |  |                 HttpsUtil.get(url+"?token="+loginUserInfo.getHkMenuToken(),true); | 
 |  |  |             } | 
 |  |  |             //删除老的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对象 | 
 |  |  |             HttpClient httpClient = HttpClientBuilder.create().build(); | 
 |  |  |             // 创建HttpGet对象,指定要访问的URL地址 | 
 |  |  |             HttpGet httpGet = new HttpGet(url); | 
 |  |  |             // 发送GET请求,获取响应 | 
 |  |  |             HttpResponse response = httpClient.execute(httpGet); | 
 |  |  |             // 获取响应状态码 | 
 |  |  |             int statusCode = response.getStatusLine().getStatusCode(); | 
 |  |  |             // 判断请求是否成功 | 
 |  |  |             if (statusCode == 200) { | 
 |  |  |                 // 获取响应内容 | 
 |  |  |                 HttpEntity entity = response.getEntity(); | 
 |  |  |                 String responseContent = EntityUtils.toString(entity, "UTF-8"); | 
 |  |  |                 System.out.println(responseContent); | 
 |  |  |                 log.info("调起海康退出登录返回信息=======================>"+responseContent); | 
 |  |  |             } else { | 
 |  |  |                 System.out.println("请求失败,响应码:" + statusCode); | 
 |  |  |             } | 
 |  |  |         } catch (IOException e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 验证令牌 | 
 |  |  | 
 |  |  |                 .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); | 
 |  |  |     } | 
 |  |  | } |