优化jwk.json读取
parent
39de934f89
commit
547432e194
|
@ -7,7 +7,6 @@ import com.genersoft.iot.vmp.service.IUserService;
|
||||||
import com.genersoft.iot.vmp.storager.dao.dto.User;
|
import com.genersoft.iot.vmp.storager.dao.dto.User;
|
||||||
import com.genersoft.iot.vmp.storager.dao.dto.UserApiKey;
|
import com.genersoft.iot.vmp.storager.dao.dto.UserApiKey;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
import org.jose4j.jwk.JsonWebKey;
|
import org.jose4j.jwk.JsonWebKey;
|
||||||
import org.jose4j.jwk.JsonWebKeySet;
|
import org.jose4j.jwk.JsonWebKeySet;
|
||||||
import org.jose4j.jwk.RsaJsonWebKey;
|
import org.jose4j.jwk.RsaJsonWebKey;
|
||||||
|
@ -22,12 +21,16 @@ import org.jose4j.jwt.consumer.JwtConsumer;
|
||||||
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
|
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
|
||||||
import org.jose4j.lang.JoseException;
|
import org.jose4j.lang.JoseException;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.ResourceUtils;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -95,8 +98,44 @@ public class JwtUtils implements InitializingBean {
|
||||||
RsaJsonWebKey rsaJsonWebKey = null;
|
RsaJsonWebKey rsaJsonWebKey = null;
|
||||||
try {
|
try {
|
||||||
String jwkFile = userSetting.getJwkFile();
|
String jwkFile = userSetting.getJwkFile();
|
||||||
File file = ResourceUtils.getFile(jwkFile);
|
InputStream inputStream = null;
|
||||||
String jwkJson = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
if (jwkFile.startsWith("classpath:")){
|
||||||
|
String filePath = jwkFile.substring("classpath:".length());
|
||||||
|
ClassPathResource civilCodeFile = new ClassPathResource(filePath);
|
||||||
|
if (civilCodeFile.exists()) {
|
||||||
|
inputStream = civilCodeFile.getInputStream();
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
File civilCodeFile = new File(userSetting.getCivilCodeFile());
|
||||||
|
if (civilCodeFile.exists()) {
|
||||||
|
inputStream = Files.newInputStream(civilCodeFile.toPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (inputStream == null ) {
|
||||||
|
log.warn("[API AUTH] 读取jwk.json失败,文件不存在,将使用新生成的随机RSA密钥对");
|
||||||
|
// 生成一个RSA密钥对,该密钥对将用于JWT的签名和验证,包装在JWK中
|
||||||
|
rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||||
|
// 给JWK一个密钥ID
|
||||||
|
rsaJsonWebKey.setKeyId(keyId);
|
||||||
|
return rsaJsonWebKey;
|
||||||
|
}
|
||||||
|
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
||||||
|
int index = -1;
|
||||||
|
String line;
|
||||||
|
StringBuilder content = new StringBuilder();
|
||||||
|
while ((line = inputStreamReader.readLine()) != null) {
|
||||||
|
content.append(line);
|
||||||
|
index ++;
|
||||||
|
if (index == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inputStreamReader.close();
|
||||||
|
inputStream.close();
|
||||||
|
|
||||||
|
|
||||||
|
String jwkJson = content.toString();
|
||||||
JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(jwkJson);
|
JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(jwkJson);
|
||||||
List<JsonWebKey> jsonWebKeys = jsonWebKeySet.getJsonWebKeys();
|
List<JsonWebKey> jsonWebKeys = jsonWebKeySet.getJsonWebKeys();
|
||||||
if (!jsonWebKeys.isEmpty()) {
|
if (!jsonWebKeys.isEmpty()) {
|
||||||
|
@ -105,15 +144,15 @@ public class JwtUtils implements InitializingBean {
|
||||||
rsaJsonWebKey = (RsaJsonWebKey) jsonWebKey;
|
rsaJsonWebKey = (RsaJsonWebKey) jsonWebKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignore) {}
|
||||||
// ignored
|
|
||||||
}
|
|
||||||
if (rsaJsonWebKey == null) {
|
if (rsaJsonWebKey == null) {
|
||||||
log.warn("[API AUTH] 读取jwk.json失败,将使用新生成的随机RSA密钥对");
|
log.warn("[API AUTH] 读取jwk.json失败,获取内容失败,将使用新生成的随机RSA密钥对");
|
||||||
// 生成一个RSA密钥对,该密钥对将用于JWT的签名和验证,包装在JWK中
|
// 生成一个RSA密钥对,该密钥对将用于JWT的签名和验证,包装在JWK中
|
||||||
rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||||
// 给JWK一个密钥ID
|
// 给JWK一个密钥ID
|
||||||
rsaJsonWebKey.setKeyId(keyId);
|
rsaJsonWebKey.setKeyId(keyId);
|
||||||
|
}else {
|
||||||
|
log.info("[API AUTH] 读取jwk.json成功");
|
||||||
}
|
}
|
||||||
return rsaJsonWebKey;
|
return rsaJsonWebKey;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue