新增jwt和rsa工具类

This commit is contained in:
zlt
2019-07-22 10:39:50 +08:00
parent 323aed2521
commit cbfd528f88
5 changed files with 235 additions and 3 deletions
@@ -1,6 +1,7 @@
package com.central.oauth2.common.store;
import cn.hutool.core.util.StrUtil;
import com.central.common.constant.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.context.annotation.Bean;
@@ -28,8 +29,6 @@ import java.util.stream.Collectors;
* @date 2018/8/20 9:25
*/
public class ResJwtTokenStore {
private static final String PUBLIC_KEY = "pubkey.txt";
@Autowired
private ResourceServerProperties resource;
@@ -50,7 +49,7 @@ public class ResJwtTokenStore {
* @return 公钥 Key
*/
private String getPubKey() {
Resource res = new ClassPathResource(ResJwtTokenStore.PUBLIC_KEY);
Resource res = new ClassPathResource(SecurityConstants.RSA_PUBLIC_KEY);
try (BufferedReader br = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
return br.lines().collect(Collectors.joining("\n"));
} catch (IOException ioe) {
@@ -0,0 +1,86 @@
package com.central.oauth2.common.util;
import com.alibaba.fastjson.JSONObject;
import com.central.common.constant.SecurityConstants;
import com.central.common.utils.RsaUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.jwt.crypto.sign.SignatureVerifier;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.interfaces.RSAPublicKey;
import java.util.stream.Collectors;
/**
* jwt工具类
*
* @author zlt
* @date 2019/7/21
*/
public class JwtUtils {
private static final String PUBKEY_START = "-----BEGIN PUBLIC KEY-----";
private static final String PUBKEY_END = "-----END PUBLIC KEY-----";
/**
* 通过classpath获取公钥值
*/
public static RSAPublicKey getPubKeyObj() {
Resource res = new ClassPathResource(SecurityConstants.RSA_PUBLIC_KEY);
try (BufferedReader br = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
String pubKey = br.lines().collect(Collectors.joining("\n"));
pubKey = pubKey.substring(PUBKEY_START.length(), pubKey.indexOf(PUBKEY_END));
return RsaUtils.getPublicKey(pubKey);
} catch (Exception ioe) {
ioe.printStackTrace();
}
return null;
}
/**
* {"exp":1563256084,"user_name":"admin","authorities":["ADMIN"],"jti":"4ce02f54-3d1c-4461-8af1-73f0841a35df","client_id":"webApp","scope":["app"]}
* @param jwtToken token值
* @param rsaPublicKey 公钥
* @return
*/
public static JSONObject decodeAndVerify(String jwtToken, RSAPublicKey rsaPublicKey) {
SignatureVerifier rsaVerifier = new RsaVerifier(rsaPublicKey);
Jwt jwt = JwtHelper.decodeAndVerify(jwtToken, rsaVerifier);
return JSONObject.parseObject(jwt.getClaims());
}
/**
* {"exp":1563256084,"user_name":"admin","authorities":["ADMIN"],"jti":"4ce02f54-3d1c-4461-8af1-73f0841a35df","client_id":"webApp","scope":["app"]}
* @param jwtToken token值
* @return
*/
public static JSONObject decodeAndVerify(String jwtToken) {
return decodeAndVerify(jwtToken, getPubKeyObj());
}
/**
* 判断jwt是否过期
* @param claims jwt内容
* @param currTime 当前时间
* @return 未过期:true,已过期:false
*/
public static boolean checkExp(JSONObject claims, long currTime) {
long exp = claims.getLong("exp");
if (exp < currTime) {
return false;
}
return true;
}
/**
* 判断jwt是否过期
* @param claims jwt内容
* @return 未过期:true,已过期:false
*/
public static boolean checkExp(JSONObject claims) {
return checkExp(claims, System.currentTimeMillis());
}
}