All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.haoxuer.discover.user.utils.UserUtils Maven / Gradle / Ivy

There is a newer version: 3.3.18-20230117
Show newest version
package com.haoxuer.discover.user.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.compression.CompressionCodecs;
import sun.security.rsa.RSAKeyPairGenerator;

import java.util.Calendar;

public class UserUtils {

  private String slat;

  public UserUtils(String slat) {
    this.slat = slat;
  }
  public  Long member(String token) {
    Long result = null;
    try {
      Jws c = Jwts.parser().setSigningKey(slat).parseClaimsJws(token);
      result = Long.parseLong(c.getBody().getSubject());
    } catch (Exception e) {
    }
    return result;

  }
  public  String token(Long member) {
    String result = "";
    try {
      result = Jwts.builder().setSubject("" + member).signWith(SignatureAlgorithm.HS512, slat)
          .compressWith(CompressionCodecs.GZIP).compact();
    } catch (Exception e) {
    }
    return result;

  }
  private static String base64key = "ekVxD03S0ddY9MKH+4dWMtY3ol4Lbk73aXRAb2ygHwE4ZshtAIOuOeiqqBz4 N27G6oqF5h9eVMLw3V4//27O3g==";
  
  public static Long getMember(String token) {
    Long result = null;
    try {
      Jws c = Jwts.parser().setSigningKey(base64key).parseClaimsJws(token);
      result = Long.parseLong(c.getBody().getSubject());
    } catch (Exception e) {
    }
    return result;
    
  }
  
  public static Long getApp(String token) {
    Long result = null;
    try {
      if (token == null) {
        throw new TokenInvalidException();
      }
      Jws c = Jwts.parser().setSigningKey(base64key).parseClaimsJws(token);
      result = Long.parseLong(c.getBody().getSubject());
    } catch (Exception e) {
      throw new TokenInvalidException();
    }
    return result;
    
  }
  
  public static String getToken(Long member) {
    String result = "";
    try {
      result = Jwts.builder().setSubject("" + member).signWith(SignatureAlgorithm.HS512, base64key)
          .compressWith(CompressionCodecs.GZIP).compact();
    } catch (Exception e) {
    }
    return result;
    
  }
  
  public static String getAppToken(Long member) {
    String result = "";
    try {
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.YEAR, 1);
      result = Jwts.builder().setSubject("" + member).signWith(SignatureAlgorithm.HS512, base64key).setExpiration(calendar.getTime())
          .compressWith(CompressionCodecs.GZIP).compact();
    } catch (Exception e) {
    }
    return result;
    
  }
  
  public static String refreshAppToken(String token) {
    String result = "";
    try {
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.YEAR, 1);
      Long member = getApp(token);
      result = Jwts.builder().setSubject("" + member).signWith(SignatureAlgorithm.HS512, base64key).setExpiration(calendar.getTime())
          .compressWith(CompressionCodecs.GZIP).compact();
    } catch (Exception e) {
      throw new RefreshException();
    }
    return result;
    
  }
  
  public static class RefreshException extends RuntimeException {

  }
  
  public static class TokenInvalidException extends RuntimeException {

  }

  public static void main(String[] args) {
    UserUtils userUtils=new UserUtils("3433434");
    System.out.println(userUtils.token(1L));
    System.out.println(userUtils.member(userUtils.token(1L)));
    Jwts.builder().signWith(SignatureAlgorithm.RS512, base64key);

    RSAKeyPairGenerator rsaKeyPairGenerator=new RSAKeyPairGenerator();
    rsaKeyPairGenerator.generateKeyPair();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy