Browse Source

java 反射 静态方法不创建实例 登录错误密码提示 删除日志文件 获取列名添加limit

andy 1 year ago
parent
commit
6c53ca0815

+ 12 - 3
src/main/java/com/scbfkj/uni/library/script/JavaScriptEngineUtil.java

@@ -12,6 +12,7 @@ import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
 import java.io.File;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.Map;
@@ -41,18 +42,26 @@ public class JavaScriptEngineUtil {
                 }
 
                 Class<?> classExample = classLoader.loadClass(className.asText()); //获取类实例
-//                Object classInstance = classExample.getConstructor().newInstance();//类实例接口 无参数构造
                 Method javaMethod = null;
                 Method closeMethod = null;
                 for (Method currentMethod : classExample.getMethods()) {//循环所有方法
                     String methodName = currentMethod.getName();
                     if (methodName.equals(method.asText())) {
                         javaMethod = currentMethod;
-                    } else if (methodName.equals("close")) {
+                    } else if ("close".equals(methodName)) {
                         closeMethod = currentMethod;
                     }
                 }
-                return new JavaApply(null, javaMethod, closeMethod);
+                Object classInstance = null;
+                if (javaMethod != null) {
+                    int modifiers = javaMethod.getModifiers();
+                    boolean aStatic = Modifier.isStatic(modifiers);
+                    if (!aStatic) {
+//                    不是静态方法 需要实例化一个对象
+                        classInstance = classExample.getConstructor().newInstance();//类实例接口 无参数构造
+                    }
+                }
+                return new JavaApply(classInstance, javaMethod, closeMethod);
             } finally {
                 if (Objects.nonNull(classLoader)) {
                     if (classLoader instanceof URLClassLoader cl) {

+ 1 - 1
src/main/java/com/scbfkj/uni/process/DataBase.java

@@ -275,7 +275,7 @@ public class DataBase {
         }
         ResultSet resultSet;
         try (Connection conn = getDataSourcePool(connection).getConnection();
-             PreparedStatement preparedStatement = conn.prepareStatement("select * from %s".formatted(tabelName))) {
+             PreparedStatement preparedStatement = conn.prepareStatement("select * from %s limit 0,1".formatted(tabelName))) {
             resultSet = preparedStatement.executeQuery();
         } catch (Exception e) {
             throw new RuntimeException(e);

+ 18 - 0
src/main/java/com/scbfkj/uni/service/LoggerService.java

@@ -9,6 +9,7 @@ import com.scbfkj.uni.system.Config;
 import com.zaxxer.hikari.pool.HikariPool;
 
 import java.io.File;
+import java.sql.SQLException;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -186,6 +187,23 @@ public class LoggerService {
                 } else {
                     System.out.println(UniReturnUtil.getMessage(e));
                 }
+                if (e instanceof SQLException sqlException) {
+                    if (sqlException.getMessage().contains("no such table: logs")) {
+                        HikariPool hikariPool = DataBase.dataSourcePools.remove(connectionStr);
+                        if (Objects.nonNull(hikariPool)) {
+                            try {
+                                hikariPool.shutdown();
+                            } catch (InterruptedException ex) {
+                                if (Config.isDebug()) {
+                                    ex.printStackTrace();
+                                } else {
+                                    System.out.println(UniReturnUtil.getMessage(ex));
+                                }
+                            }
+                        }
+                        f.delete();
+                    }
+                }
             } finally {
                 removeDataSource(connectionStr);
             }

+ 5 - 1
src/main/java/com/scbfkj/uni/service/SecurityService.java

@@ -252,7 +252,11 @@ public class SecurityService {
         } else {
             userInfo = userInfoList.get(0);
             String userPassword = DataFormatUtil.toString(userInfo.get("userpassword"));
-            userPassword = DataEncryptionUtil.decryptRSAByPrivateKey(userPassword);
+            try {
+                userPassword = DataEncryptionUtil.decryptRSAByPrivateKey(userPassword);
+            } catch (Exception e) {
+                return UniReturnUtil.fail("密码错误");
+            }
             if (!Objects.equals(userPassword, loginPassword)) {
                 return UniReturnUtil.fail("用户名密码错误");
             }