Merge pull request #4650 from KomachiSion/2.0.0-sync-dev

Sync dev code to 2.0.0
This commit is contained in:
杨翊 SionYang 2021-01-07 17:23:24 +08:00 committed by GitHub
commit a21b40ef00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 131 additions and 251 deletions

View File

@ -53,7 +53,7 @@ public class AuthConfigs {
/**
* Token validity time(seconds).
*/
@Value("${nacos.core.auth.default.token.expire.seconds:1800}")
@Value("${nacos.core.auth.default.token.expire.seconds:18000}")
private long tokenValidityInSeconds;
/**

View File

@ -164,7 +164,7 @@ public class ConfigHttpClientManager implements Closeable {
}
@Override
public void close() throws IOException {
public void close() {
}
}

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.config.utils;
import com.alibaba.nacos.common.utils.IoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -89,7 +90,7 @@ public class ConcurrentDiskUtil {
rlock = null;
}
if (fis != null) {
fis.close();
IoUtils.closeQuietly(fis);
fis = null;
}
}

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.client.naming.cache;
import com.alibaba.nacos.common.utils.IoUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -88,7 +90,7 @@ public class ConcurrentDiskUtil {
rlock = null;
}
if (fis != null) {
fis.close();
IoUtils.closeQuietly(fis);
fis = null;
}
}

View File

@ -24,12 +24,14 @@ import com.alibaba.nacos.common.http.client.response.JdkHttpClientResponse;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.MediaType;
import com.alibaba.nacos.common.model.RequestHttpEntity;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.HashMap;
@ -101,9 +103,10 @@ public class JdkHttpClientRequest implements HttpClientRequest {
conn.setDoOutput(true);
byte[] b = bodyStr.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
conn.getOutputStream().write(b, 0, b.length);
conn.getOutputStream().flush();
conn.getOutputStream().close();
OutputStream outputStream = conn.getOutputStream();
outputStream.write(b, 0, b.length);
outputStream.flush();
IoUtils.closeQuietly(outputStream);
}
}
conn.connect();

View File

@ -62,9 +62,7 @@ public interface HttpClientResponse extends Closeable {
/**
* close response InputStream.
*
* @throws IOException ex
*/
@Override
void close() throws IOException;
void close();
}

View File

@ -64,12 +64,8 @@ public class IoUtils {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
if (gis != null) {
gis.close();
}
closeQuietly(out);
closeQuietly(gis);
}
return null;
@ -95,12 +91,8 @@ public class IoUtils {
IoUtils.copy(gis, out);
return out.toByteArray();
} finally {
if (out != null) {
out.close();
}
if (gis != null) {
gis.close();
}
closeQuietly(out);
closeQuietly(gis);
}
}
@ -146,9 +138,7 @@ public class IoUtils {
os.write(data.getBytes(encoding));
os.flush();
} finally {
if (null != os) {
os.close();
}
closeQuietly(os);
}
}
@ -335,12 +325,8 @@ public class IoUtils {
sc = new FileInputStream(sf).getChannel();
sc.transferTo(0, sc.size(), tc);
} finally {
if (null != sc) {
sc.close();
}
if (null != tc) {
tc.close();
}
closeQuietly(sc);
closeQuietly(tc);
}
}

View File

@ -143,7 +143,7 @@ public class ResourceUtils {
Properties props = new Properties();
InputStream in = getResourceAsStream(loader, resource);
props.load(in);
in.close();
IoUtils.closeQuietly(in);
return props;
}

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.enums.FileTypeEnum;
import com.alibaba.nacos.config.server.model.CacheItem;
@ -140,17 +141,12 @@ public class ConfigServletInner {
isBeta = true;
}
}
final String configType =
(null != cacheItem.getType()) ? cacheItem.getType() : FileTypeEnum.TEXT.getFileType();
response.setHeader("Config-Type", configType);
String contentTypeHeader;
try {
contentTypeHeader = FileTypeEnum.valueOf(configType.toUpperCase()).getContentType();
} catch (IllegalArgumentException ex) {
contentTypeHeader = FileTypeEnum.TEXT.getContentType();
}
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(configType);
String contentTypeHeader = fileTypeEnum.getContentType();
response.setHeader(HttpHeaderConsts.CONTENT_TYPE, contentTypeHeader);
}
File file = null;
@ -278,9 +274,7 @@ public class ConfigServletInner {
} finally {
releaseConfigReadLock(groupKey);
if (null != fis) {
fis.close();
}
IoUtils.closeQuietly(fis);
}
} else if (lockResult == 0) {

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.enums;
import com.alibaba.nacos.common.http.param.MediaType;
import com.alibaba.nacos.common.utils.StringUtils;
/**
* Config file type enum.
@ -98,4 +99,24 @@ public enum FileTypeEnum {
public String getContentType() {
return contentType;
}
/**
* Get the corresponding FileTypeEnum by file extension or fileType. If not found FileTypeEnum.TEXT is returned
*
* @param extOrFileType file extension or fileType
* @return
*/
public static FileTypeEnum getFileTypeEnumByFileExtensionOrFileType(String extOrFileType) {
if (StringUtils.isNotBlank(extOrFileType)) {
String upperExtName = extOrFileType.trim().toUpperCase();
for (FileTypeEnum value : VALUES) {
if (value.name().equals(upperExtName)) {
return value;
}
}
}
return FileTypeEnum.TEXT;
}
private static final FileTypeEnum[] VALUES = FileTypeEnum.values();
}

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.config.server.service.datasource;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
@ -235,9 +236,7 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
} catch (Exception ex) {
throw new Exception(ex.getMessage());
} finally {
if (sqlFileIn != null) {
sqlFileIn.close();
}
IoUtils.closeQuietly(sqlFileIn);
}
}
@ -249,10 +248,8 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
* @throws Exception Exception.
*/
private void execute(Connection conn, String sqlFile) throws Exception {
Statement stmt = null;
try {
try (Statement stmt = conn.createStatement()) {
List<String> sqlList = loadSql(sqlFile);
stmt = conn.createStatement();
for (String sql : sqlList) {
try {
stmt.execute(sql);
@ -260,10 +257,6 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
LogUtil.DEFAULT_LOG.warn(e.getMessage());
}
}
} finally {
if (stmt != null) {
stmt.close();
}
}
}

View File

@ -1125,8 +1125,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
Page<Map<String, Object>> pageList = helper
.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, MAP_ROW_MAPPER);
return pageList.getPageItems().stream()
.map(map -> String.valueOf(map.get("TENANT_ID")))
return pageList.getPageItems().stream().map(map -> String.valueOf(map.get("TENANT_ID")))
.collect(Collectors.toList());
}
@ -1139,8 +1138,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
Page<Map<String, Object>> pageList = helper
.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, MAP_ROW_MAPPER);
return pageList.getPageItems().stream()
.map(map -> String.valueOf(map.get("GROUP_ID")))
return pageList.getPageItems().stream().map(map -> String.valueOf(map.get("GROUP_ID")))
.collect(Collectors.toList());
}
@ -2344,13 +2342,9 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
if (StringUtils.isBlank(type)) {
// simple judgment of file type based on suffix
if (configInfo.getDataId().contains(SPOT)) {
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1)
.toUpperCase();
try {
type = FileTypeEnum.valueOf(extName.toUpperCase()).getFileType();
} catch (Throwable ex) {
type = FileTypeEnum.TEXT.getFileType();
}
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
type = fileTypeEnum.getFileType();
}
}
if (configAdvanceInfo == null) {

View File

@ -2590,13 +2590,9 @@ public class ExternalStoragePersistServiceImpl implements PersistService {
if (StringUtils.isBlank(type)) {
// simple judgment of file type based on suffix
if (configInfo.getDataId().contains(SPOT)) {
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1)
.toUpperCase();
try {
type = FileTypeEnum.valueOf(extName.toUpperCase()).getFileType();
} catch (Exception ex) {
type = FileTypeEnum.TEXT.getFileType();
}
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
type = fileTypeEnum.getFileType();
}
}
if (configAdvanceInfo == null) {

View File

@ -28,12 +28,12 @@ import com.alibaba.nacos.common.utils.Objects;
import com.alibaba.nacos.config.server.auth.RoleInfo;
import com.alibaba.nacos.config.server.model.User;
import com.alibaba.nacos.config.server.utils.RequestUtil;
import com.alibaba.nacos.console.security.nacos.JwtTokenManager;
import com.alibaba.nacos.console.security.nacos.NacosAuthConfig;
import com.alibaba.nacos.console.security.nacos.NacosAuthManager;
import com.alibaba.nacos.console.security.nacos.roles.NacosRoleServiceImpl;
import com.alibaba.nacos.console.security.nacos.users.NacosUser;
import com.alibaba.nacos.console.security.nacos.users.NacosUserDetailsServiceImpl;
import com.alibaba.nacos.console.utils.JwtTokenUtils;
import com.alibaba.nacos.console.utils.PasswordEncoderUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
@ -67,7 +67,7 @@ import java.util.List;
public class UserController {
@Autowired
private JwtTokenUtils jwtTokenUtils;
private JwtTokenManager jwtTokenManager;
@Autowired
private AuthenticationManager authenticationManager;
@ -228,7 +228,7 @@ public class UserController {
// Authentication 绑定到 SecurityContext
SecurityContextHolder.getContext().setAuthentication(authentication);
//生成Token
String token = jwtTokenUtils.createToken(authentication);
String token = jwtTokenManager.createToken(authentication);
//将Token写入到Http头部
response.addHeader(NacosAuthConfig.AUTHORIZATION_HEADER, "Bearer " + token);
rr.setCode(200);

View File

@ -1,166 +0,0 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.console.utils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SecurityException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.crypto.SecretKey;
import java.util.Date;
import java.util.List;
/**
* Jwt token tool.
*
* @author wfnuser
*/
@Component
public class JwtTokenUtils {
private final Logger log = LoggerFactory.getLogger(JwtTokenUtils.class);
private static final String AUTHORITIES_KEY = "auth";
/**
* minimum SHA_256 secretKey string length.
*/
private static final int SHA_256_SECRET_CHAR_SIZE = 256 / 8;
/**
* default SHA_256 secretKey flag.
*/
private static final String DEFAULT_SECRET_FLAG = "default";
/**
* custom SHA_256 secretKey from config property.
*/
@Value("${nacos.security.token.secret-key:default}")
private String customSecretKeyStr;
/**
* secret key.
*/
private SecretKey secretKey;
/**
* Token validity time(ms).
*/
private long tokenValidityInMilliseconds;
/**
* Init.
*/
@PostConstruct
public void init() {
//use default secretKey for SHA-256
if (customSecretKeyStr == null || DEFAULT_SECRET_FLAG.equals(customSecretKeyStr)) {
this.secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);
} else {
//use custom secretKey
int size = customSecretKeyStr.length();
int left = SHA_256_SECRET_CHAR_SIZE - size;
if (left > 0) {
//character for padding
StringBuilder stringBuilder = new StringBuilder(customSecretKeyStr);
for (int i = 0; i < left; i++) {
stringBuilder.append(i % 10);
}
this.secretKey = Keys.hmacShaKeyFor(stringBuilder.toString().getBytes());
} else {
this.secretKey = Keys.hmacShaKeyFor(customSecretKeyStr.getBytes());
}
}
this.tokenValidityInMilliseconds = 1000 * 60 * 30L;
}
/**
* Create token.
*
* @param authentication auth info
* @return token
*/
public String createToken(Authentication authentication) {
long now = System.currentTimeMillis();
Date validity = new Date(now + this.tokenValidityInMilliseconds);
return Jwts.builder().setSubject(authentication.getName()).claim(AUTHORITIES_KEY, "").setExpiration(validity)
.signWith(secretKey, SignatureAlgorithm.HS256).compact();
}
/**
* Get auth Info.
*
* @param token token
* @return auth info
*/
public Authentication getAuthentication(String token) {
Claims claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody();
List<GrantedAuthority> authorities = AuthorityUtils
.commaSeparatedStringToAuthorityList((String) claims.get(AUTHORITIES_KEY));
User principal = new User(claims.getSubject(), "", authorities);
return new UsernamePasswordAuthenticationToken(principal, "", authorities);
}
/**
* validate token.
*
* @param token token
* @return whether valid
*/
public boolean validateToken(String token) {
try {
Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token);
return true;
} catch (SecurityException e) {
log.info("Invalid JWT signature.");
log.trace("Invalid JWT signature trace: {}", e);
} catch (MalformedJwtException e) {
log.info("Invalid JWT token.");
log.trace("Invalid JWT token trace: {}", e);
} catch (ExpiredJwtException e) {
log.info("Expired JWT token.");
log.trace("Expired JWT token trace: {}", e);
} catch (UnsupportedJwtException e) {
log.info("Unsupported JWT token.");
log.trace("Unsupported JWT token trace: {}", e);
} catch (IllegalArgumentException e) {
log.info("JWT token compact of handler are invalid.");
log.trace("JWT token compact of handler are invalid trace: {}", e);
}
return false;
}
}

View File

@ -78,7 +78,9 @@ public class Member implements Comparable<Member>, Cloneable {
}
public void setExtendInfo(Map<String, Object> extendInfo) {
this.extendInfo.putAll(extendInfo);
Map<String, Object> newExtendInfo = Collections.synchronizedMap(new TreeMap<>());
newExtendInfo.putAll(extendInfo);
this.extendInfo = newExtendInfo;
}
public String getIp() {

View File

@ -73,8 +73,8 @@ public class JRaftUtils {
registry.registerResponseInstance(Log.class.getName(), Response.getDefaultInstance());
registry.registerResponseInstance(GetRequest.class.getName(), Response.getDefaultInstance());
registry.registerResponseInstance(WriteRequest.class.getName(), WriteRequest.getDefaultInstance());
registry.registerResponseInstance(ReadRequest.class.getName(), ReadRequest.getDefaultInstance());
registry.registerResponseInstance(WriteRequest.class.getName(), Response.getDefaultInstance());
registry.registerResponseInstance(ReadRequest.class.getName(), Response.getDefaultInstance());
final RpcServer rpcServer = raftRpcFactory.createRpcServer(peerId.getEndpoint());
RaftRpcServerFactory.addRaftRequestProcessors(rpcServer, RaftExecutor.getRaftCoreExecutor(),

View File

@ -261,22 +261,16 @@ public class RaftStore implements Closeable {
throw new IllegalStateException("can not make cache file: " + cacheFile.getName());
}
FileChannel fc = null;
ByteBuffer data;
data = ByteBuffer.wrap(JacksonUtils.toJson(datum).getBytes(StandardCharsets.UTF_8));
try {
fc = new FileOutputStream(cacheFile, false).getChannel();
try (FileChannel fc = new FileOutputStream(cacheFile, false).getChannel()) {
fc.write(data, data.position());
fc.force(true);
} catch (Exception e) {
MetricsMonitor.getDiskException().increment();
throw e;
} finally {
if (fc != null) {
fc.close();
}
}
// remove old format file:

View File

@ -0,0 +1,62 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.test.common;
import com.alibaba.nacos.config.server.enums.FileTypeEnum;
import org.junit.Assert;
import org.junit.Test;
/**
* @author by jiangmin.wu on 2020/12/30
*/
public class FileTypeEnum_ITCase {
@Test
public void fileTypeEnum_test1() {
for (FileTypeEnum value : FileTypeEnum.values()) {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(value.name());
Assert.assertEquals(fileTypeEnum, value);
}
}
@Test
public void fileTypeEnum_test2() {
for (FileTypeEnum value : FileTypeEnum.values()) {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(value.getFileType());
Assert.assertNotNull(fileTypeEnum);
}
}
@Test
public void fileTypeEnum_test3() {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("t");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);
fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);
fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(".");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);
fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("1");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);
fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(null);
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);
}
}

View File

@ -91,7 +91,7 @@ public class NacosRestTemplate_Interceptors_ITCase {
}
@Override
public void close() throws IOException {
public void close() {
}
};