zoukankan      html  css  js  c++  java
  • UserTokenManager JwtHelper

    package org.linlinjava.litemall.wx.service;
    
    import org.linlinjava.litemall.wx.util.JwtHelper;
    
    /**
     * 维护用户token
     */
    public class UserTokenManager {
        public static String generateToken(Integer id) {
            JwtHelper jwtHelper = new JwtHelper();
            return jwtHelper.createToken(id);
        }
        public static Integer getUserId(String token) {
            JwtHelper jwtHelper = new JwtHelper();
            Integer userId = jwtHelper.verifyTokenAndGetUserId(token);
            if(userId == null || userId == 0){
                return null;
            }
            return userId;
        }
    }
    package org.linlinjava.litemall.wx.util;
    
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.auth0.jwt.JWT;
    import com.auth0.jwt.JWTVerifier;
    import com.auth0.jwt.algorithms.Algorithm;
    import com.auth0.jwt.exceptions.JWTCreationException;
    import com.auth0.jwt.exceptions.JWTVerificationException;
    import com.auth0.jwt.interfaces.Claim;
    import com.auth0.jwt.interfaces.DecodedJWT;
    
    public class JwtHelper {
        // 秘钥
        static final String SECRET = "X-Litemall-Token";
        // 签名是有谁生成
        static final String ISSUSER = "LITEMALL";
        // 签名的主题
        static final String SUBJECT = "this is litemall token";
        // 签名的观众
        static final String AUDIENCE = "MINIAPP";
        
        
        public String createToken(Integer userId){
            try {
                Algorithm algorithm = Algorithm.HMAC256(SECRET);
                Map<String, Object> map = new HashMap<String, Object>();
                Date nowDate = new Date();
                // 过期时间:2小时
                Date expireDate = getAfterDate(nowDate,0,0,0,2,0,0);
                map.put("alg", "HS256");
                map.put("typ", "JWT");
                String token = JWT.create()
                    // 设置头部信息 Header
                    .withHeader(map)
                    // 设置 载荷 Payload
                    .withClaim("userId", userId)
                    .withIssuer(ISSUSER)
                    .withSubject(SUBJECT)
                    .withAudience(AUDIENCE)
                    // 生成签名的时间 
                    .withIssuedAt(nowDate)
                    // 签名过期的时间 
                    .withExpiresAt(expireDate)
                    // 签名 Signature
                    .sign(algorithm);
                return token;
            } catch (JWTCreationException exception){
                exception.printStackTrace();
            }
            return null;
        }
        
        public Integer verifyTokenAndGetUserId(String token) {
            try {
                Algorithm algorithm = Algorithm.HMAC256(SECRET);
                JWTVerifier verifier = JWT.require(algorithm)
                    .withIssuer(ISSUSER)
                    .build();
                DecodedJWT jwt = verifier.verify(token);
                Map<String, Claim> claims = jwt.getClaims();
                Claim claim = claims.get("userId");
                return claim.asInt();
            } catch (JWTVerificationException exception){
    //            exception.printStackTrace();
            }
            
            return 0;
        }
        
        public  Date getAfterDate(Date date, int year, int month, int day, int hour, int minute, int second){
            if(date == null){
                date = new Date();
            }
            
            Calendar cal = new GregorianCalendar();
            
            cal.setTime(date);
            if(year != 0){
                cal.add(Calendar.YEAR, year);
            }
            if(month != 0){
                cal.add(Calendar.MONTH, month);
            }
            if(day != 0){
                cal.add(Calendar.DATE, day);
            }
            if(hour != 0){
                cal.add(Calendar.HOUR_OF_DAY, hour);
            }
            if(minute != 0){
                cal.add(Calendar.MINUTE, minute);
            }
            if(second != 0){
                cal.add(Calendar.SECOND, second);
            }
            return cal.getTime();
        }
        
    }
    <dependency>
                <groupId>com.auth0</groupId>
                <artifactId>java-jwt</artifactId>
                <version>3.4.1</version>
            </dependency>

     http://www.leftso.com/blog/221.html

  • 相关阅读:
    0x01 虚拟环境搭建
    python操作mysql8windows环境
    Navicat 导入sql文件执行失败问题的处理
    mysql8.0.16免安装教程
    zend studio 9.0.3 注册码
    oneplus8手机蓝牙连接tws耳机无法双击退出语音助手
    竞品分析
    源码阅读方法
    Tomcat内核1
    Asp.NetCore3.1开源项目升级为.Net6.0
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11335822.html
Copyright © 2011-2022 走看看