update 跨服务获取token

This commit is contained in:
lijiarao
2021-09-15 13:57:21 +08:00
parent 93aad30c16
commit 2057c0476e
3 changed files with 51 additions and 3 deletions
@@ -1,16 +1,16 @@
package com.jero.util;
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.alibaba.fastjson.JSONObject;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.RestUtil;
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;
@@ -20,6 +20,8 @@ import java.util.Map;
*/
@Component
public class AuthenticationUtils {
@Resource
private RedisUtil redisUtil;
private static String URL;
@@ -33,6 +35,37 @@ public class AuthenticationUtils {
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
@@ -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"); //登出接口排除
@@ -61,6 +61,8 @@ public class LoginController {
private ISysDictService sysDictService;
@Resource
private BaseCommonService baseCommonService;
@Resource
private AuthenticationUtils authenticationUtils;
private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";
/**
@@ -571,4 +573,16 @@ public class LoginController {
result.setResult(publicKeyBase64);
return result;
}
/**
* 给其他系统返回RSA公钥
* 私钥存redis
*
* @return
*/
@ApiOperation(value = "获取公钥2")
@GetMapping("/getStandardsRSAPublicKey")
public String getStandardsRSAPublicKey() {
return authenticationUtils.getPublicKey();
}
}