Selaa lähdekoodia

优化OAuth认证逻辑;优化数据库公共字段默认值处理逻辑;

woody 9 kuukautta sitten
vanhempi
commit
fcf72df077

+ 29 - 7
framework-base/src/main/java/com/chelvc/framework/base/context/SessionContextHolder.java

@@ -5,6 +5,7 @@ import java.io.OutputStream;
 import java.util.ArrayDeque;
 import java.util.Collections;
 import java.util.Deque;
+import java.util.Objects;
 import java.util.Set;
 import java.util.function.Function;
 import javax.servlet.ServletRequestEvent;
@@ -377,7 +378,28 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前平台是否是指定平台
+     * 判断是否是指定应用范围
+     *
+     * @param scopes 应用范围数组
+     * @return true/false
+     */
+    public static boolean isScope(@NonNull String... scopes) {
+        if (ObjectUtils.isEmpty(scopes)) {
+            return false;
+        }
+        String current = getScope();
+        if (current != null) {
+            for (String scope : scopes) {
+                if (Objects.equals(scope, current)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 判断是否是指定平台
      *
      * @param platforms 平台数组
      * @return true/false
@@ -398,7 +420,7 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前终端是否是指定终端
+     * 判断是否是指定终端
      *
      * @param terminals 终端数组
      * @return true/false
@@ -419,7 +441,7 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前版本是否是指定版本
+     * 判断是否是指定版本
      *
      * @param terminal 指定终端
      * @param version  指定版本
@@ -432,7 +454,7 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前版本是否在指定版本之前
+     * 判断是否在指定版本之前
      *
      * @param terminal 指定终端
      * @param version  指定版本
@@ -443,7 +465,7 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前版本是否在指定版本之前
+     * 判断是否在指定版本之前
      *
      * @param terminal 指定终端
      * @param version  指定版本
@@ -457,7 +479,7 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前版本是否在指定版本之后
+     * 判断是否在指定版本之后
      *
      * @param terminal 指定终端
      * @param version  指定版本
@@ -468,7 +490,7 @@ public class SessionContextHolder implements ServletRequestListener {
     }
 
     /**
-     * 判断当前版本是否在指定版本之后
+     * 判断是否在指定版本之后
      *
      * @param terminal 指定终端
      * @param version  指定版本

+ 5 - 9
framework-database/src/main/java/com/chelvc/framework/database/interceptor/DynamicInvokeInterceptor.java

@@ -759,9 +759,7 @@ public class DynamicInvokeInterceptor implements Interceptor {
         boolean rebuild = false;
         Class<?> model = DatabaseContextHolder.getTableModel(table.getName());
         if (model != null && Updatable.class.isAssignableFrom(model)) {
-            if (operator != null) {
-                rebuild = this.initializeDefaultValue(bound, update, table, Expressions.UPDATER_COLUMN, operator);
-            }
+            rebuild = this.initializeDefaultValue(bound, update, table, Expressions.UPDATER_COLUMN, operator);
             rebuild |= this.initializeDefaultValue(bound, update, table, Expressions.UPDATE_TIME_COLUMN, datetime);
         }
         return rebuild;
@@ -898,17 +896,15 @@ public class DynamicInvokeInterceptor implements Interceptor {
         }
         Pair<?, Expression> operator = null, datetime = null;
         if (Creatable.class.isAssignableFrom(model)) {
-            if ((operator = Expressions.operator()) != null) {
-                rebuild = this.initializeDefaultValue(bound, insert, Expressions.CREATOR_COLUMN, operator);
-            }
+            operator = Expressions.operator();
             datetime = Expressions.datetime();
+            rebuild = this.initializeDefaultValue(bound, insert, Expressions.CREATOR_COLUMN, operator);
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.CREATE_TIME_COLUMN, datetime);
         }
         if (Updatable.class.isAssignableFrom(model)) {
-            if (operator != null || (operator = Expressions.operator()) != null) {
-                rebuild |= this.initializeDefaultValue(bound, insert, Expressions.UPDATER_COLUMN, operator);
-            }
+            operator = ObjectUtils.ifNull(operator, Expressions::operator);
             datetime = ObjectUtils.ifNull(datetime, Expressions::datetime);
+            rebuild |= this.initializeDefaultValue(bound, insert, Expressions.UPDATER_COLUMN, operator);
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.UPDATE_TIME_COLUMN, datetime);
         }
         if (Environmental.class.isAssignableFrom(model) && Expressions.env().getLeft() != null) {

+ 2 - 2
framework-database/src/main/java/com/chelvc/framework/database/interceptor/Expressions.java

@@ -188,8 +188,8 @@ final class Expressions {
      * @return 操作人/表达式
      */
     public static Pair<Long, Expression> operator() {
-        Long operator = SessionContextHolder.getId();
-        return operator == null ? null : Pair.of(operator, new LongValue(operator));
+        Long operator = ObjectUtils.ifNull(SessionContextHolder.getId(), 0L);
+        return Pair.of(operator, new LongValue(operator));
     }
 
     /**

+ 28 - 0
framework-oauth/src/main/java/com/chelvc/framework/oauth/context/OAuthSessionFactory.java

@@ -0,0 +1,28 @@
+package com.chelvc.framework.oauth.context;
+
+import java.util.Collections;
+import javax.servlet.http.HttpServletRequest;
+
+import com.chelvc.framework.base.context.DefaultSessionFactory;
+import com.chelvc.framework.base.context.Session;
+import com.chelvc.framework.base.context.SessionFactory;
+import com.chelvc.framework.base.context.Using;
+import lombok.NonNull;
+import org.springframework.stereotype.Component;
+
+/**
+ * OAuth认证会话工厂
+ *
+ * @author Woody
+ * @date 2024/7/7
+ */
+@Component
+public class OAuthSessionFactory extends DefaultSessionFactory implements SessionFactory {
+    @Override
+    public Session build(@NonNull HttpServletRequest request) {
+        return Session.builder().using(Using.NORMAL).host(this.getHost(request)).device(this.getDevice(request))
+                .channel(this.getChannel(request)).platform(this.getPlatform(request))
+                .terminal(this.getTerminal(request)).version(this.getVersion(request))
+                .authorities(Collections.emptySet()).timestamp(this.getTimestamp(request)).build();
+    }
+}

+ 16 - 19
framework-security/src/main/java/com/chelvc/framework/security/interceptor/MethodSecurityExpression.java

@@ -1,12 +1,9 @@
 package com.chelvc.framework.security.interceptor;
 
-import java.util.Objects;
-import java.util.stream.Stream;
-
 import com.chelvc.framework.base.context.SessionContextHolder;
+import com.chelvc.framework.common.model.Platform;
 import com.chelvc.framework.common.model.Terminal;
-import com.chelvc.framework.common.util.ObjectUtils;
-import com.chelvc.framework.common.util.StringUtils;
+import lombok.NonNull;
 import org.springframework.security.access.PermissionEvaluator;
 import org.springframework.security.access.expression.SecurityExpressionRoot;
 import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
@@ -147,13 +144,18 @@ public class MethodSecurityExpression implements MethodSecurityExpressionOperati
      * @param scopes 应用范围数组
      * @return true/false
      */
-    public boolean isScope(String... scopes) {
-        if (ObjectUtils.isEmpty(scopes)) {
-            return false;
-        }
-        String current = SessionContextHolder.getScope();
-        return StringUtils.notEmpty(current) && Stream.of(scopes).filter(StringUtils::notEmpty)
-                .anyMatch(type -> Objects.equals(type, current));
+    public boolean isScope(@NonNull String... scopes) {
+        return SessionContextHolder.isScope(scopes);
+    }
+
+    /**
+     * 判断是否是指定平台
+     *
+     * @param platforms 平台数组
+     * @return true/false
+     */
+    public boolean isPlatform(@NonNull Platform... platforms) {
+        return SessionContextHolder.isPlatform(platforms);
     }
 
     /**
@@ -162,12 +164,7 @@ public class MethodSecurityExpression implements MethodSecurityExpressionOperati
      * @param terminals 终端数组
      * @return true/false
      */
-    public boolean isTerminal(Terminal... terminals) {
-        if (ObjectUtils.isEmpty(terminals)) {
-            return false;
-        }
-        Terminal current = SessionContextHolder.getTerminal();
-        return current != null && Stream.of(terminals).filter(StringUtils::notEmpty)
-                .anyMatch(terminal -> Objects.equals(terminal, current));
+    public boolean isTerminal(@NonNull Terminal... terminals) {
+        return SessionContextHolder.isTerminal(terminals);
     }
 }