[code quality] [nacos-client] [identity] simple the judge logic, constant export, chinese javadoc translate (#6005)

This commit is contained in:
brotherlu-xcq 2021-06-09 09:56:55 +08:00 committed by GitHub
parent c025dc2c18
commit bec96c1f19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 21 deletions

View File

@ -43,7 +43,7 @@ public final class CredentialService implements SpasCredentialLoader {
private CredentialService(String appName) {
if (appName == null) {
String value = System.getProperty("project.name");
String value = System.getProperty(IdentifyConstants.PROJECT_NAME_PROPERTY);
if (StringUtils.isNotEmpty(value)) {
appName = value;
}
@ -59,13 +59,14 @@ public final class CredentialService implements SpasCredentialLoader {
public static CredentialService getInstance(String appName) {
String key = appName != null ? appName : IdentifyConstants.NO_APP_NAME;
CredentialService instance = INSTANCES.get(key);
if (instance == null) {
if (instance != null) {
return instance;
}
instance = new CredentialService(appName);
CredentialService previous = INSTANCES.putIfAbsent(key, instance);
if (previous != null) {
instance = previous;
}
}
return instance;
}

View File

@ -106,7 +106,6 @@ public class CredentialWatcher {
}
private void loadCredential(boolean init) {
boolean logWarn = init;
if (propertyPath == null) {
URL url = ClassLoader.getSystemResource(IdentifyConstants.PROPERTIES_FILENAME);
if (url != null) {
@ -123,12 +122,12 @@ public class CredentialWatcher {
IdentifyConstants.CREDENTIAL_PATH + (appName == null ? IdentifyConstants.CREDENTIAL_DEFAULT
: appName);
} else {
if (logWarn) {
if (init) {
SPAS_LOGGER.info("[{}] Defined credential file: -Dspas.identity={}", appName, propertyPath);
}
}
} else {
if (logWarn) {
if (init) {
SPAS_LOGGER.info("[{}] Load credential file from classpath: {}", appName,
IdentifyConstants.PROPERTIES_FILENAME);
}
@ -161,7 +160,7 @@ public class CredentialWatcher {
accessKey = System.getenv(IdentifyConstants.ENV_ACCESS_KEY);
secretKey = System.getenv(IdentifyConstants.ENV_SECRET_KEY);
if (accessKey == null && secretKey == null) {
if (logWarn) {
if (init) {
SPAS_LOGGER.info("{} No credential found", appName);
}
return;
@ -184,7 +183,7 @@ public class CredentialWatcher {
}
}
if (logWarn) {
if (init) {
SPAS_LOGGER.info("[{}] Load credential file {}", appName, propertyPath);
}

View File

@ -51,4 +51,15 @@ public class IdentifyConstants {
public static final String NO_APP_NAME = "";
public static final String PROJECT_NAME_PROPERTY = "project.name";
public static final String RAM_ROLE_NAME_PROPERTY = "ram.role.name";
public static final String REFRESH_TIME_PROPERTY = "time.to.refresh.in.millisecond";
public static final String SECURITY_PROPERTY = "security.credentials";
public static final String SECURITY_URL_PROPERTY = "security.credentials.url";
public static final String SECURITY_CACHE_PROPERTY = "cache.security.credentials";
}

View File

@ -30,22 +30,23 @@ public class StsConfig {
private String ramRoleName;
/**
* STS 临时凭证有效期剩余多少时开始刷新允许本地时间比 STS 服务时间最多慢多久.
* The STS temporary certificate will be refreshed when the validity period of
* the temporary certificate is left (allow the local time to be at most slower than the STS service time).
*/
private int timeToRefreshInMillisecond = 3 * 60 * 1000;
/**
* 获取 STS 临时凭证的元数据接口包含角色名称.
* Metadata interface for obtaining STS temporary credentials (including role name).
*/
private String securityCredentialsUrl;
/**
* 设定 STS 临时凭证不再通过元数据接口获取.
* Set the STS temporary certificate and no longer obtain it through the metadata interface.
*/
private String securityCredentials;
/**
* 是否缓存.
* Whether to cache.
*/
private boolean cacheSecurityCredentials = true;
@ -55,27 +56,27 @@ public class StsConfig {
}
private StsConfig() {
String ramRoleName = System.getProperty("ram.role.name");
String ramRoleName = System.getProperty(IdentifyConstants.RAM_ROLE_NAME_PROPERTY);
if (!StringUtils.isBlank(ramRoleName)) {
setRamRoleName(ramRoleName);
}
String timeToRefreshInMillisecond = System.getProperty("time.to.refresh.in.millisecond");
String timeToRefreshInMillisecond = System.getProperty(IdentifyConstants.REFRESH_TIME_PROPERTY);
if (!StringUtils.isBlank(timeToRefreshInMillisecond)) {
setTimeToRefreshInMillisecond(Integer.parseInt(timeToRefreshInMillisecond));
}
String securityCredentials = System.getProperty("security.credentials");
String securityCredentials = System.getProperty(IdentifyConstants.SECURITY_PROPERTY);
if (!StringUtils.isBlank(securityCredentials)) {
setSecurityCredentials(securityCredentials);
}
String securityCredentialsUrl = System.getProperty("security.credentials.url");
String securityCredentialsUrl = System.getProperty(IdentifyConstants.SECURITY_URL_PROPERTY);
if (!StringUtils.isBlank(securityCredentialsUrl)) {
setSecurityCredentialsUrl(securityCredentialsUrl);
}
String cacheSecurityCredentials = System.getProperty("cache.security.credentials");
String cacheSecurityCredentials = System.getProperty(IdentifyConstants.SECURITY_CACHE_PROPERTY);
if (!StringUtils.isBlank(cacheSecurityCredentials)) {
setCacheSecurityCredentials(Boolean.parseBoolean(cacheSecurityCredentials));
}