update 跨服务获取token
This commit is contained in:
+89
@@ -0,0 +1,89 @@
|
||||
package com.jero.common.util;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 跨服务调用时验证身份
|
||||
*/
|
||||
@Component
|
||||
public class AuthenticationUtils {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
private static String URL;
|
||||
|
||||
private static String APPLICATION_TOKEN;
|
||||
|
||||
@Value("${standards.url}")
|
||||
public void setURL(String url) {
|
||||
AuthenticationUtils.URL = url;
|
||||
}
|
||||
@Value("${standards.token}")
|
||||
public void setApplicationToken(String applicationToken) {
|
||||
APPLICATION_TOKEN = applicationToken;
|
||||
}
|
||||
/**
|
||||
* 获取公钥
|
||||
* @return
|
||||
*/
|
||||
public String getPublicKey() {
|
||||
RSA rsa = new RSA();
|
||||
//获得私钥
|
||||
String privateKey = rsa.getPrivateKeyBase64();
|
||||
//获得公钥
|
||||
String publicKey = rsa.getPublicKeyBase64();
|
||||
|
||||
//私钥存缓存,key是公钥
|
||||
redisUtil.set(publicKey, privateKey, 60L);
|
||||
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用公钥获取私钥校验token
|
||||
* @return
|
||||
*/
|
||||
public void check(String token,String publicKey) {
|
||||
//从redis拿到私钥
|
||||
String privateKey = String.valueOf(redisUtil.get(publicKey));
|
||||
RSA rsa = new RSA(privateKey,null);
|
||||
|
||||
String decryptToken = rsa.decryptStr(token, KeyType.PrivateKey, StandardCharsets.UTF_8);
|
||||
if(!APPLICATION_TOKEN.equals(decryptToken)){
|
||||
throw new JeroBootException("身份校验失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公钥并使用公钥加密token
|
||||
*/
|
||||
public static Map<String,String> encryptByRsaPriKey() {
|
||||
|
||||
Map<String,String> map= new HashMap<>();
|
||||
//从标协项目获取公钥
|
||||
String publicKey=HttpUtil.get(URL+"/sys/getStandardsRSAPublicKey");
|
||||
|
||||
RSA rsa = new RSA(null,publicKey);
|
||||
|
||||
byte[] token = rsa.encrypt(StrUtil.bytes(APPLICATION_TOKEN, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
String tokenStr = Base64.getEncoder().encodeToString(token);
|
||||
|
||||
map.put("publicKey",publicKey);
|
||||
map.put("token",tokenStr);
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -74,6 +74,7 @@ public class ShiroConfig {
|
||||
filterChainDefinitionMap.put("/sys/randomImage/**", "anon"); //登录验证码接口排除
|
||||
filterChainDefinitionMap.put("/sys/checkCaptcha", "anon"); //登录验证码接口排除
|
||||
filterChainDefinitionMap.put("/sys/getRSAPublicKey", "anon"); //获取RSA公钥接口排除
|
||||
filterChainDefinitionMap.put("/sys/getStandardsRSAPublicKey","anon");//获取给外部系统的RSA公钥
|
||||
filterChainDefinitionMap.put("/sys/login", "anon"); //登录接口排除
|
||||
filterChainDefinitionMap.put("/sys/mLogin", "anon"); //登录接口排除
|
||||
filterChainDefinitionMap.put("/sys/logout", "anon"); //登出接口排除
|
||||
|
||||
Reference in New Issue
Block a user