jwt
1 package com.atguigu.common.jwt; 2 3 import io.jsonwebtoken.*; 4 import org.springframework.util.StringUtils; 5 6 import java.util.Date; 7 8 //jwt工具类 9 public class JwtHelper { 10 11 private static long tokenExpiration = 60 * 60 * 24; 12 private static String tokenSignKey = "123456"; 13 14 //根据用户id和用户名称生成token字符串 15 public static String createToken(Long userId, String username) { 16 String token = Jwts.builder() 17 //分类 18 .setSubject("AUTH-USER") 19 20 //设置token有效时长 21 .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration)) 22 23 //设置主体部分 24 .claim("userId", userId) 25 .claim("username", username) 26 27 //签名部分 28 .signWith(SignatureAlgorithm.HS512, tokenSignKey) 29 .compressWith(CompressionCodecs.GZIP) 30 .compact(); 31 return token; 32 } 33 34 //从生成token字符串获取用户id 35 public static Long getUserId(String token) { 36 try { 37 if (StringUtils.isEmpty(token)) { 38 return null; 39 } 40 41 Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token); 42 Claims claims = claimsJws.getBody(); 43 Integer userId = (Integer) claims.get("userId"); 44 return userId.longValue(); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 return null; 48 } 49 } 50 51 //从生成token字符串获取用户名称 52 public static String getUsername(String token) { 53 try { 54 if (StringUtils.isEmpty(token)) return ""; 55 56 Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token); 57 Claims claims = claimsJws.getBody(); 58 return (String) claims.get("username"); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 return null; 62 } 63 } 64 65 public static void sleepUtil(int time){ 66 try { 67 Thread.sleep(time); 68 } catch (InterruptedException e) { 69 e.printStackTrace(); 70 } 71 } 72 73 public static void sleepUtil(){ 74 sleepUtil(200); 75 } 76 77 /** 78 * 判断token是否过期 79 * @return true: 过期 false: 没有过期 80 */ 81 public static boolean isExpiration(String token){ 82 83 // sleepUtil(); 84 85 86 87 if(StringUtils.isEmpty(token)){ 88 System.out.println("不是合法token"); 89 return true; 90 } 91 92 try { 93 Jws<Claims> claim = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token); 94 Claims body = claim.getBody(); 95 Date expiration = body.getExpiration(); 96 97 long expirationTime = expiration.getTime(); 98 99 long now = System.currentTimeMillis(); 100 101 if(now - expirationTime < 0){ 102 return false; 103 } 104 } catch (Exception e) { 105 System.out.println("已经过期了"); 106 return true; 107 } 108 109 return false; 110 } 111 112 public static void main(String[] args) { 113 String token = createToken(1l, "admin"); 114 115 System.out.println(isExpiration(token)); 116 } 117 }
md5 特点是单向加密,无法还原
代码
package com.atguigu.common.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final class MD5 { public static String encrypt(String strSrc) { try { char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; byte[] bytes = strSrc.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); bytes = md.digest(); int j = bytes.length; char[] chars = new char[j * 2]; int k = 0; for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; chars[k++] = hexChars[b >>> 4 & 0xf]; chars[k++] = hexChars[b & 0xf]; } return new String(chars); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("MD5加密出错!!+" + e); } } public static void main(String[] args) { System.out.println(MD5.encrypt("111111")); } }
标签:return,String,userId,JWT,token,static,封装,public,MD5 From: https://www.cnblogs.com/huoziqi/p/17419213.html