refactor: 代码质量优化 + 安全修复 + 性能提升
P0 安全修复: - 修复 DatabaseFieldAdder.java 硬编码密码 → 改为环境变量 - 修复 11 个文件空 catch 块 → 添加日志记录 - 修复 40 个文件 System.out → 改为 SLF4J Logger P1 性能优化: - 启用 Spring Boot Actuator 健康检查 (health/info/metrics) - 为字典数据查询添加 @Cacheable 缓存 P2 测试: - 添加 Convert 工具类单元测试 (10 个测试用例) - 添加 spring-boot-starter-test 依赖 P3 版本升级: - hutool: 5.8.35 → 5.8.36 - httpclient 5.x (跳过, 改动量大) 验证: 编译通过 / 测试通过
This commit is contained in:
@@ -197,6 +197,12 @@
|
||||
<artifactId>gson</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1040,7 +1040,8 @@ public class NewExcelUtil<T> {
|
||||
try {
|
||||
temp = Double.valueOf(text);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
log.debug("Caught expected exception: {}", e.getMessage());
|
||||
}
|
||||
statistics.put(index, statistics.get(index) + temp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.core.common.utils.html;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.core.common.utils.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -8,6 +11,7 @@ import com.core.common.utils.StringUtils;
|
||||
* @author system
|
||||
*/
|
||||
public class EscapeUtil {
|
||||
private static final Logger log = LoggerFactory.getLogger(EscapeUtil.class);
|
||||
public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
|
||||
|
||||
private static final char[][] TEXT = new char[64][];
|
||||
@@ -133,8 +137,8 @@ public class EscapeUtil {
|
||||
// String html = "<scr<script>ipt>alert(\"XSS\")</scr<script>ipt>";
|
||||
// String html = "<123";
|
||||
// String html = "123>";
|
||||
System.out.println("clean: " + EscapeUtil.clean(html));
|
||||
System.out.println("escape: " + escape);
|
||||
System.out.println("unescape: " + EscapeUtil.unescape(escape));
|
||||
log.info("clean: " + EscapeUtil.clean(html));
|
||||
log.info("escape: " + escape);
|
||||
log.info("unescape: " + EscapeUtil.unescape(escape));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.core.common.utils.ip;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.core.common.utils.ServletUtils;
|
||||
import com.core.common.utils.StringUtils;
|
||||
|
||||
@@ -13,6 +16,7 @@ import java.net.UnknownHostException;
|
||||
* @author system
|
||||
*/
|
||||
public class IpUtils {
|
||||
private static final Logger log = LoggerFactory.getLogger(IpUtils.class);
|
||||
public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
|
||||
// 匹配 ip
|
||||
public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
|
||||
@@ -193,7 +197,8 @@ public class IpUtils {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
log.debug("Caught expected exception: {}", e.getMessage());
|
||||
}
|
||||
return "127.0.0.1";
|
||||
}
|
||||
|
||||
@@ -206,7 +211,8 @@ public class IpUtils {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
log.debug("Caught expected exception: {}", e.getMessage());
|
||||
}
|
||||
return "未知";
|
||||
}
|
||||
|
||||
|
||||
@@ -1165,7 +1165,8 @@ public class ExcelUtil<T> {
|
||||
try {
|
||||
temp = Double.valueOf(text);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
log.debug("Caught expected exception: {}", e.getMessage());
|
||||
}
|
||||
statistics.put(index, statistics.get(index) + temp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.core.common.core.text;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Convert 工具类单元测试
|
||||
*/
|
||||
class ConvertTest {
|
||||
|
||||
@Test
|
||||
void testToStr() {
|
||||
assertEquals("hello", Convert.toStr("hello"));
|
||||
assertEquals("123", Convert.toStr(123));
|
||||
assertEquals("true", Convert.toStr(true));
|
||||
assertNull(Convert.toStr(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToInt() {
|
||||
assertEquals(123, Convert.toInt("123"));
|
||||
assertEquals(123, Convert.toInt(123));
|
||||
assertNull(Convert.toInt("invalid"));
|
||||
assertNull(Convert.toInt(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToLong() {
|
||||
assertEquals(123L, Convert.toLong("123"));
|
||||
assertEquals(123L, Convert.toLong(123L));
|
||||
assertNull(Convert.toLong("invalid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToDouble() {
|
||||
assertEquals(1.23, Convert.toDouble("1.23"), 0.001);
|
||||
assertEquals(1.23, Convert.toDouble(1.23), 0.001);
|
||||
assertNull(Convert.toDouble("invalid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToFloat() {
|
||||
assertEquals(1.23f, Convert.toFloat("1.23"), 0.001);
|
||||
assertEquals(1.23f, Convert.toFloat(1.23f), 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToBool() {
|
||||
assertTrue(Convert.toBool("true"));
|
||||
assertTrue(Convert.toBool(true));
|
||||
assertFalse(Convert.toBool("false"));
|
||||
assertFalse(Convert.toBool(false));
|
||||
assertNull(Convert.toBool("invalid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToByte() {
|
||||
assertEquals((byte) 123, Convert.toByte("123"));
|
||||
assertEquals((byte) 123, Convert.toByte((byte) 123));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToShort() {
|
||||
assertEquals((short) 123, Convert.toShort("123"));
|
||||
assertEquals((short) 123, Convert.toShort((short) 123));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToBigDecimal() {
|
||||
assertEquals(0, new BigDecimal("1.23").compareTo(Convert.toBigDecimal("1.23")));
|
||||
assertEquals(0, new BigDecimal("1.23").compareTo(Convert.toBigDecimal(1.23)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToBigInteger() {
|
||||
assertEquals(0, new BigInteger("123").compareTo(Convert.toBigInteger("123")));
|
||||
assertEquals(0, new BigInteger("123").compareTo(Convert.toBigInteger(123)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user