|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
package com.dsideal.dsBase.Util;
|
|
|
|
|
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.dsideal.dsBase.BaseApplication;
|
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
|
|
import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -12,10 +15,13 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class JwtUtil {
|
|
|
|
|
public static final String AUTHORIZATION_STARTER = "Bearer ";
|
|
|
|
|
public static final String SECRET = PropKit.get("SECRET");
|
|
|
|
|
public static final String SECRET = BaseApplication.PropKit.get("jwt.SECRET");
|
|
|
|
|
|
|
|
|
|
// 生成安全的密钥
|
|
|
|
|
private static final SecretKey key = Keys.hmacShaKeyFor(SECRET.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:黄海开发的生成JWT函数
|
|
|
|
|
* 功能:生成JWT函数
|
|
|
|
|
*
|
|
|
|
|
* @param identity_id 身份ID
|
|
|
|
|
* @param person_id 人员ID
|
|
|
|
@ -31,7 +37,11 @@ public class JwtUtil {
|
|
|
|
|
claims.put("identity_id", identity_id);
|
|
|
|
|
claims.put("person_id", person_id);
|
|
|
|
|
claims.put("bureau_id", bureau_id);
|
|
|
|
|
return AUTHORIZATION_STARTER + Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS256, SECRET).compact();
|
|
|
|
|
|
|
|
|
|
return AUTHORIZATION_STARTER + Jwts.builder()
|
|
|
|
|
.setClaims(claims)
|
|
|
|
|
.signWith(key, SignatureAlgorithm.HS256)
|
|
|
|
|
.compact();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Claims getClaims(String token) {
|
|
|
|
@ -40,33 +50,40 @@ public class JwtUtil {
|
|
|
|
|
}
|
|
|
|
|
Claims claims;
|
|
|
|
|
try {
|
|
|
|
|
claims = Jwts.parser()
|
|
|
|
|
.setSigningKey(SECRET)
|
|
|
|
|
claims = Jwts.parserBuilder()
|
|
|
|
|
.setSigningKey(key)
|
|
|
|
|
.build()
|
|
|
|
|
.parseClaimsJws(token)
|
|
|
|
|
.getBody();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
claims = Jwts.parser()
|
|
|
|
|
.setSigningKey(SECRET.getBytes(StandardCharsets.UTF_8))
|
|
|
|
|
.parseClaimsJws(token)
|
|
|
|
|
.getBody();
|
|
|
|
|
} catch (Exception err) {
|
|
|
|
|
claims = null;
|
|
|
|
|
}
|
|
|
|
|
claims = null;
|
|
|
|
|
}
|
|
|
|
|
return claims;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前用户信息
|
|
|
|
|
*
|
|
|
|
|
* @param token 票据
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Record getPersonInfo(String token) {
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
Claims claims = getClaims(token);
|
|
|
|
|
if (claims != null) {
|
|
|
|
|
record.set("identity_id", claims.get("identity_id"));
|
|
|
|
|
record.set("person_id", claims.get("person_id"));
|
|
|
|
|
record.set("bureau_id", claims.get("bureau_id"));
|
|
|
|
|
record.set("create_time", claims.get("create_time"));
|
|
|
|
|
}
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
//GwApplication gw = new GwApplication();
|
|
|
|
|
String token = generateToken(4, "0b64e31e-a85e-43eb-ba5f-3088d986a8da", "3f7f4c90-645a-4fb9-9902-447846cf1dcc");
|
|
|
|
|
/**
|
|
|
|
|
结论:
|
|
|
|
|
1、JWT的里面有三个关键信息,一个是identity_id,另一个是person_id,还有一个bureau_id
|
|
|
|
|
*/
|
|
|
|
|
Claims claims = getClaims(token);
|
|
|
|
|
System.out.println(claims.get("identity_id"));
|
|
|
|
|
System.out.println(claims.get("person_id"));
|
|
|
|
|
System.out.println(claims.get("bureau_id"));
|
|
|
|
|
System.out.println(token);
|
|
|
|
|
|
|
|
|
|
Record record=getPersonInfo(token);
|
|
|
|
|
System.out.println(record);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|