diff --git a/qodana.yaml b/qodana.yaml new file mode 100644 index 0000000..08a316b --- /dev/null +++ b/qodana.yaml @@ -0,0 +1,31 @@ +#-------------------------------------------------------------------------------# +# Qodana analysis is configured by qodana.yaml file # +# https://www.jetbrains.com/help/qodana/qodana-yaml.html # +#-------------------------------------------------------------------------------# +version: "1.0" + +#Specify inspection profile for code analysis +profile: + name: qodana.starter + +#Enable inspections +#include: +# - name: + +#Disable inspections +#exclude: +# - name: +# paths: +# - + +projectJDK: 17 #(Applied in CI/CD pipeline) + +#Execute shell command before Qodana execution (Applied in CI/CD pipeline) +#bootstrap: sh ./prepare-qodana.sh + +#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline) +#plugins: +# - id: #(plugin id can be found at https://plugins.jetbrains.com) + +#Specify Qodana linter for analysis (Applied in CI/CD pipeline) +linter: jetbrains/qodana-jvm:latest diff --git a/springboot-ademo/src/main/java/com/tiesheng/demo/config/DemoWebConfigurer.java b/springboot-ademo/src/main/java/com/tiesheng/demo/config/DemoWebConfigurer.java index c5d0aff..c8efbba 100644 --- a/springboot-ademo/src/main/java/com/tiesheng/demo/config/DemoWebConfigurer.java +++ b/springboot-ademo/src/main/java/com/tiesheng/demo/config/DemoWebConfigurer.java @@ -16,10 +16,6 @@ import java.util.Objects; @Component public class DemoWebConfigurer implements TieshengWebConfigurer, TsLoginConfigurer { - @Autowired - GlobalConfig globalConfig; - - @Override public RequestUserInfo getCurrentUserName(TokenBean tokenBean) { RequestUserInfo info = new RequestUserInfo(); diff --git a/springboot-ademo/src/main/java/com/tiesheng/demo/config/TestJobConfig.java b/springboot-ademo/src/main/java/com/tiesheng/demo/config/TestJobConfig.java index 0973138..e98662a 100644 --- a/springboot-ademo/src/main/java/com/tiesheng/demo/config/TestJobConfig.java +++ b/springboot-ademo/src/main/java/com/tiesheng/demo/config/TestJobConfig.java @@ -1,8 +1,6 @@ package com.tiesheng.demo.config; -import com.tiesheng.web.service.CoreConfigService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -13,9 +11,6 @@ import javax.annotation.PostConstruct; @Component public class TestJobConfig { - @Autowired - CoreConfigService coreConfigService; - @PostConstruct public void init() { } diff --git a/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java b/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java index 590ec22..32fab77 100644 --- a/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java +++ b/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java @@ -219,8 +219,10 @@ public class TestController { "83_7xqG36kdgwuf8zzWLY3jtz7bg4ucziN-0oxbE0X9zBzwbjZ4S4Ss2RM9uHeSIcRp2K-wEp6MLzWhqo2AXj0Jpzd6IiJdUsRxqdHPvEWqAdOgt83vzZwdDf7tZBkGNGeAFASZS", paramJson); Response execute1 = OkHttpUtil.ofHttpClient().build().newCall(request).execute(); - FileUtil.writeFromStream(execute1.body().byteStream(), file.getAbsolutePath()); - execute1.close(); + if (execute1.body() != null) { + FileUtil.writeFromStream(execute1.body().byteStream(), file.getAbsolutePath()); + execute1.close(); + } } catch (Exception e) { throw new ApiException("每分钟最多生成5000个二维码,请稍后再试!"); } diff --git a/springboot-message/src/main/java/com/tiesheng/message/config/aliyun/AliyunSmsSender.java b/springboot-message/src/main/java/com/tiesheng/message/config/aliyun/AliyunSmsSender.java index 535028c..7e49727 100644 --- a/springboot-message/src/main/java/com/tiesheng/message/config/aliyun/AliyunSmsSender.java +++ b/springboot-message/src/main/java/com/tiesheng/message/config/aliyun/AliyunSmsSender.java @@ -99,7 +99,7 @@ public class AliyunSmsSender implements TsMessageSender { String signature = specialUrlEncode(digest); queryMap.put("Signature", signature); - String respBody = ""; + String respBody; try { respBody = OkHttpUtil.get(ENDPOINT + "?Signature=" + signature + sortQueryStringTmp); } catch (Exception e) { diff --git a/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java b/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java index 0f26586..b82901e 100644 --- a/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java +++ b/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java @@ -76,15 +76,16 @@ public class PlatformDingConfig { request = OkHttpUtil.ofPost(url, body); } request = request.newBuilder().header("x-acs-dingtalk-access-token", accessToken).build(); - try { - Response response = OkHttpUtil.ofHttpClient().build().newCall(request).execute(); + try (Response response = OkHttpUtil.ofHttpClient().build().newCall(request).execute()) { if (response.isSuccessful() && response.body() != null) { String rawBody = response.body().string(); DingResponse bean = JSON.parseObject(rawBody, typeReference); bean.setRawBody(rawBody); return bean; } else { - LogFactory.get().info(response.body().string()); + if (response.body() != null) { + LogFactory.get().info(response.body().string()); + } } } catch (Exception e) { LogFactory.get().error(e); diff --git a/springboot-util/src/main/java/com/tiesheng/util/ServletKit.java b/springboot-util/src/main/java/com/tiesheng/util/ServletKit.java index b5cb042..41a27f6 100644 --- a/springboot-util/src/main/java/com/tiesheng/util/ServletKit.java +++ b/springboot-util/src/main/java/com/tiesheng/util/ServletKit.java @@ -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); } diff --git a/springboot-util/src/main/java/com/tiesheng/util/jwt/JWT.java b/springboot-util/src/main/java/com/tiesheng/util/jwt/JWT.java index 5dbc37f..85e6f39 100644 --- a/springboot-util/src/main/java/com/tiesheng/util/jwt/JWT.java +++ b/springboot-util/src/main/java/com/tiesheng/util/jwt/JWT.java @@ -384,7 +384,7 @@ public class JWT implements RegisteredPayload { * @since 5.7.4 */ public boolean validate(long leeway) { - if (false == verify()) { + if (!verify()) { return false; } diff --git a/springboot-util/src/main/java/com/tiesheng/util/jwt/JWTValidator.java b/springboot-util/src/main/java/com/tiesheng/util/jwt/JWTValidator.java index 2f47cfb..9784074 100644 --- a/springboot-util/src/main/java/com/tiesheng/util/jwt/JWTValidator.java +++ b/springboot-util/src/main/java/com/tiesheng/util/jwt/JWTValidator.java @@ -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!"); } } diff --git a/springboot-util/src/main/java/com/tiesheng/util/service/http/OkHttpUtil.java b/springboot-util/src/main/java/com/tiesheng/util/service/http/OkHttpUtil.java index 44fa6bc..4f35c5f 100644 --- a/springboot-util/src/main/java/com/tiesheng/util/service/http/OkHttpUtil.java +++ b/springboot-util/src/main/java/com/tiesheng/util/service/http/OkHttpUtil.java @@ -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(); } diff --git a/springboot-web/src/main/java/com/tiesheng/web/service/OkHttpLogInterceptor.java b/springboot-web/src/main/java/com/tiesheng/web/service/OkHttpLogInterceptor.java index f785ed1..06df127 100644 --- a/springboot-web/src/main/java/com/tiesheng/web/service/OkHttpLogInterceptor.java +++ b/springboot-web/src/main/java/com/tiesheng/web/service/OkHttpLogInterceptor.java @@ -36,11 +36,14 @@ public class OkHttpLogInterceptor implements Interceptor { logApi.setResult(response.code()); // 如果是json,xml,text,则保存记录 - if (response.body() != null && StrUtil.containsAll(response.body().contentType().toString(), - "json", "xml", "text")) { - ResponseBody peekBody = response.peekBody(Long.MAX_VALUE); - logApi.setRespBody(peekBody.string()); - peekBody.close(); + if (response.body() != null) { + MediaType mediaType = response.body().contentType(); + String contentType = mediaType == null ? "" : mediaType.toString(); + if (StrUtil.containsAny(contentType, "json", "xml", "text")) { + ResponseBody peekBody = response.peekBody(Long.MAX_VALUE); + logApi.setRespBody(peekBody.string()); + peekBody.close(); + } } } catch (Exception e) {