perf:代码调整
This commit is contained in:
@@ -17,7 +17,10 @@ public class ServletKit extends ServletUtil {
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes)
|
||||
RequestContextHolder.getRequestAttributes();
|
||||
return attributes.getRequest();
|
||||
if (attributes != null) {
|
||||
return attributes.getRequest();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,6 +30,9 @@ public class ServletKit extends ServletUtil {
|
||||
*/
|
||||
public static String getClientIP() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request == null) {
|
||||
return "";
|
||||
}
|
||||
return getClientIP(request);
|
||||
}
|
||||
|
||||
|
||||
@@ -384,7 +384,7 @@ public class JWT implements RegisteredPayload<JWT> {
|
||||
* @since 5.7.4
|
||||
*/
|
||||
public boolean validate(long leeway) {
|
||||
if (false == verify()) {
|
||||
if (!verify()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -160,13 +160,13 @@ public class JWTValidator {
|
||||
}
|
||||
|
||||
final String algorithmIdInSigner = signer.getAlgorithmId();
|
||||
if (false == StrUtil.equals(algorithmId, algorithmIdInSigner)) {
|
||||
if (!StrUtil.equals(algorithmId, algorithmIdInSigner)) {
|
||||
throw new ValidateException("Algorithm [{}] defined in header doesn't match to [{}]!"
|
||||
, algorithmId, algorithmIdInSigner);
|
||||
}
|
||||
|
||||
// 通过算法验证签名是否正确
|
||||
if (false == jwt.verify(signer)) {
|
||||
if (!jwt.verify(signer)) {
|
||||
throw new ValidateException("Signature verification failed!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,9 +83,11 @@ public class OkHttpUtil {
|
||||
}
|
||||
|
||||
public static String get(String urlString, int timeout) {
|
||||
try {
|
||||
return ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofGet(urlString)).execute().body().string();
|
||||
try (Response execute = ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofGet(urlString)).execute()) {
|
||||
if (execute.body() != null) {
|
||||
return execute.body().string();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return null;
|
||||
@@ -117,9 +119,11 @@ public class OkHttpUtil {
|
||||
}
|
||||
|
||||
public static String post(String urlString, RequestBody body, int timeout) {
|
||||
try {
|
||||
return ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofPost(urlString, body)).execute().body().string();
|
||||
try (Response response = ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofPost(urlString, body)).execute()) {
|
||||
if (response.body() != null) {
|
||||
return response.body().string();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return null;
|
||||
@@ -135,11 +139,10 @@ public class OkHttpUtil {
|
||||
}
|
||||
|
||||
public static File downloadFile(String url, File destFile) {
|
||||
try {
|
||||
Response response = ofHttpClient()
|
||||
.connectTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofGet(url)).execute();
|
||||
try (Response response = ofHttpClient()
|
||||
.connectTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofGet(url)).execute()) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
FileUtil.writeFromStream(response.body().byteStream(), destFile);
|
||||
}
|
||||
@@ -150,11 +153,10 @@ public class OkHttpUtil {
|
||||
}
|
||||
|
||||
public static byte[] downloadBytes(String url) {
|
||||
try {
|
||||
Response response = ofHttpClient()
|
||||
.connectTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofGet(url)).execute();
|
||||
try (Response response = ofHttpClient()
|
||||
.connectTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
.build().newCall(ofGet(url)).execute()) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
return response.body().bytes();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user