package com.doumee.config.jwt; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; 
 | 
import com.doumee.biz.system.SystemDictDataBiz; 
 | 
import com.doumee.core.constants.Constants; 
 | 
import com.doumee.core.utils.HttpsUtil; 
 | 
import com.doumee.dao.business.model.Member; 
 | 
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.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.data.redis.core.RedisTemplate; 
 | 
import org.springframework.stereotype.Component; 
 | 
  
 | 
import javax.annotation.Resource; 
 | 
import java.io.IOException; 
 | 
import java.util.Date; 
 | 
import java.util.HashMap; 
 | 
import java.util.Map; 
 | 
import java.util.Objects; 
 | 
import java.util.concurrent.TimeUnit; 
 | 
  
 | 
@Component 
 | 
@Slf4j 
 | 
public class JwtTokenUtil { 
 | 
  
 | 
    public static final String HEADER_KEY = "token"; 
 | 
  
 | 
    public static final String MEMBER_ID = "MEMBER_ID"; 
 | 
  
 | 
    public static final String MEMBER_INFO = "MEMBER_INFO"; 
 | 
  
 | 
    @Resource 
 | 
    private RedisTemplate<String,Object> redisTemplate; 
 | 
  
 | 
    @Resource 
 | 
    private JwtProperties jwtProperties; 
 | 
  
 | 
    /** 
 | 
     * 生成token令牌 
 | 
     * 
 | 
     * @param member 令牌中携带的附加信息 
 | 
     * @return 令token牌 
 | 
     */ 
 | 
    public String generateToken(Member member) { 
 | 
        if(member == null){ 
 | 
            return  null; 
 | 
        } 
 | 
        Map<String,Object> map = new HashMap<>(); 
 | 
        map.put("id",member.getId()); 
 | 
        return generateTokenDo(member); 
 | 
    } 
 | 
  
 | 
  
 | 
    public Member getUserInfoByToken(String token) { 
 | 
        try { 
 | 
            Member member = getClaimsFromToken(token); 
 | 
            return member; 
 | 
        } catch (Exception e) { 
 | 
           e.printStackTrace(); 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 刷新令牌 
 | 
     * 
 | 
     * @param token 原令牌 
 | 
     * @return 新令牌 
 | 
     */ 
 | 
    public void refreshToken(String token,Member member) { 
 | 
        try { 
 | 
            if(Objects.nonNull(member)){ 
 | 
                redisTemplate.expire(Constants.REDIS_TOKEN_KEY+token,jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); 
 | 
//                redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token); 
 | 
//                redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(member),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); 
 | 
            } 
 | 
        } catch (Exception e) { 
 | 
  
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 退出登陆 
 | 
     * 
 | 
     * @param token 原令牌 
 | 
     * @return 新令牌 
 | 
     */ 
 | 
    public void logoutForH5(String token) { 
 | 
        try { 
 | 
            //删除老的token 
 | 
            redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token); 
 | 
        } catch (Exception e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 从claims生成令牌,如果看不懂就看谁调用它 
 | 
     * 
 | 
     * @return 令牌 
 | 
     */ 
 | 
    private String generateTokenDo(Member member) { 
 | 
        Map<String, Object> claims = new HashMap<>(); 
 | 
        claims.put("id",member.getId()); 
 | 
        Date expirationDate = new Date(System.currentTimeMillis() + jwtProperties.getExpiration()); 
 | 
        String token = Jwts.builder().setClaims(claims) 
 | 
                .setExpiration(expirationDate) 
 | 
                .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) 
 | 
                .compact(); 
 | 
        redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(member), 
 | 
                jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); 
 | 
        return token; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 从令牌中获取数据声明,验证JWT签名 
 | 
     * 
 | 
     * @param token 令牌 
 | 
     * @return 数据声明 
 | 
     */ 
 | 
    private Member getClaimsFromToken(String token) { 
 | 
        Member claims; 
 | 
        try { 
 | 
            String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token); 
 | 
            claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),Member.class); 
 | 
        } catch (Exception e) { 
 | 
            claims = null; 
 | 
        } 
 | 
        return claims; 
 | 
    } 
 | 
  
 | 
} 
 |