package cn.mb.cloud.auth.util;
|
|
import cn.mb.cloud.auth.warpper.ParseToken;
|
import com.alibaba.fastjson.JSON;
|
import io.jsonwebtoken.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
public class JWTTokenUtil {
|
|
private static final String SigningKey = "Bo3XdHukD8FE62ki";
|
|
/**
|
* 解析token
|
* @param token
|
* @return
|
*/
|
public static ParseToken parseToken(String token) throws TokenException{
|
//解析Token,获取Claims对象
|
Claims claims = null;
|
try {
|
claims = Jwts.parser()
|
.setSigningKey(SigningKey.getBytes(StandardCharsets.UTF_8))
|
.parseClaimsJws(token)
|
.getBody();
|
}catch (SignatureException e){//解析失败
|
throw new TokenException(10001, e.getMessage());
|
}catch (MalformedJwtException e){//无效的token
|
throw new TokenException(10002, "无效的票据");
|
}catch (ExpiredJwtException e){
|
throw new TokenException(10003, "票据已过期");
|
}catch (Exception e){
|
e.printStackTrace();
|
throw new TokenException(10010, e.getMessage());
|
}
|
String string = JSON.toJSONString(claims);
|
ParseToken parseToken = JSON.parseObject(string, ParseToken.class);
|
return parseToken;
|
}
|
|
|
// public static void main(String[] ages){
|
// try {
|
// ParseToken s = JWTTokenUtil.parseToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ");
|
// System.err.println(s.toString());
|
// } catch (TokenException e) {
|
// if(e.getCode() == 10001){
|
// e.printStackTrace();
|
// }
|
// if(e.getCode() == 10002){
|
// e.printStackTrace();
|
// }
|
// }
|
// }
|
}
|