1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package top.marken.onlineclass.util;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import top.marken.onlineclass.model.entity.User;
import java.util.Date;
public class JWTUtil {
private static final String SUBJECT = "online_class";
private static final int EXPIRE = 60000 * 60 * 24 * 7;
private static final String SECRET = "online_class";
private static final String TOKEN_PREFIX = "online_class";
public static String generateJWT(User user) { String token = Jwts.builder() .setSubject(SUBJECT) .claim("head_img",user.getHeadImg()) .claim("id",user.getId()) .claim("name",user.getName()) .setIssuedAt(new Date()) .setExpiration(new Date(System.currentTimeMillis() + EXPIRE)) .signWith(SignatureAlgorithm.HS256,SECRET) .compact(); token = TOKEN_PREFIX + token; return token; }
public static Claims checkJWT(String token) { try{ return Jwts.parser() .setSigningKey(SECRET) .parseClaimsJws(token.replace(TOKEN_PREFIX,"")) .getBody(); }catch (Exception e){ return null; } } }
|