Browse Source

代码优化

woody 8 tháng trước cách đây
mục cha
commit
7f1e92abee

+ 30 - 9
framework-base/src/main/java/com/chelvc/framework/base/context/SessionContextHolder.java

@@ -345,6 +345,15 @@ public class SessionContextHolder implements ServletRequestListener {
         return ObjectUtils.ifNull(getSession(false), Session::getVersion);
         return ObjectUtils.ifNull(getSession(false), Session::getVersion);
     }
     }
 
 
+    /**
+     * 获取请求时间戳
+     *
+     * @return 时间戳
+     */
+    public static Long getTimestamp() {
+        return ObjectUtils.ifNull(getSession(false), Session::getTimestamp);
+    }
+
     /**
     /**
      * 判断是否是审核用户
      * 判断是否是审核用户
      *
      *
@@ -368,15 +377,6 @@ public class SessionContextHolder implements ServletRequestListener {
         return StringUtils.isEmpty(getMobile());
         return StringUtils.isEmpty(getMobile());
     }
     }
 
 
-    /**
-     * 获取请求时间戳
-     *
-     * @return 时间戳
-     */
-    public static Long getTimestamp() {
-        return ObjectUtils.ifNull(getSession(false), Session::getTimestamp);
-    }
-
     /**
     /**
      * 判断是否是指定应用范围
      * 判断是否是指定应用范围
      *
      *
@@ -398,6 +398,27 @@ public class SessionContextHolder implements ServletRequestListener {
         return false;
         return false;
     }
     }
 
 
+    /**
+     * 判断是否是指定渠道
+     *
+     * @param channels 渠道数组
+     * @return true/false
+     */
+    public static boolean isChannel(@NonNull String... channels) {
+        if (ObjectUtils.isEmpty(channels)) {
+            return false;
+        }
+        String current = getChannel();
+        if (StringUtils.notEmpty(current)) {
+            for (String channel : channels) {
+                if (Objects.equals(channel, current)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     /**
     /**
      * 判断是否是指定平台
      * 判断是否是指定平台
      *
      *

+ 4 - 4
framework-security/src/main/java/com/chelvc/framework/security/config/SecurityConfigurer.java

@@ -56,10 +56,10 @@ public class SecurityConfigurer extends GlobalMethodSecurityConfiguration implem
                     Authentication authentication, MethodInvocation invocation) {
                     Authentication authentication, MethodInvocation invocation) {
                 MethodSecurityExpression root = new MethodSecurityExpression(authentication);
                 MethodSecurityExpression root = new MethodSecurityExpression(authentication);
                 root.setThis(invocation.getThis());
                 root.setThis(invocation.getThis());
-                root.setPermissionEvaluator(getPermissionEvaluator());
-                root.setTrustResolver(getTrustResolver());
-                root.setRoleHierarchy(getRoleHierarchy());
-                root.setDefaultRolePrefix(getDefaultRolePrefix());
+                root.setPermissionEvaluator(this.getPermissionEvaluator());
+                root.setTrustResolver(this.getTrustResolver());
+                root.setRoleHierarchy(this.getRoleHierarchy());
+                root.setDefaultRolePrefix(this.getDefaultRolePrefix());
                 return root;
                 return root;
             }
             }
         };
         };

+ 46 - 1
framework-security/src/main/java/com/chelvc/framework/security/interceptor/MethodSecurityExpression.java

@@ -1,5 +1,7 @@
 package com.chelvc.framework.security.interceptor;
 package com.chelvc.framework.security.interceptor;
 
 
+import java.util.Objects;
+
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.common.model.Platform;
 import com.chelvc.framework.common.model.Platform;
 import com.chelvc.framework.common.model.Terminal;
 import com.chelvc.framework.common.model.Terminal;
@@ -99,7 +101,7 @@ public class MethodSecurityExpression implements MethodSecurityExpressionOperati
 
 
     @Override
     @Override
     public boolean isAuthenticated() {
     public boolean isAuthenticated() {
-        return !this.isAnonymous();
+        return Objects.nonNull(SessionContextHolder.getId());
     }
     }
 
 
     @Override
     @Override
@@ -138,6 +140,15 @@ public class MethodSecurityExpression implements MethodSecurityExpressionOperati
         this.root.setDefaultRolePrefix(defaultRolePrefix);
         this.root.setDefaultRolePrefix(defaultRolePrefix);
     }
     }
 
 
+    /**
+     * 判断是否是审核用户
+     *
+     * @return true/false
+     */
+    public boolean isAuditor() {
+        return SessionContextHolder.isAuditor();
+    }
+
     /**
     /**
      * 判断是否是指定应用范围
      * 判断是否是指定应用范围
      *
      *
@@ -148,6 +159,16 @@ public class MethodSecurityExpression implements MethodSecurityExpressionOperati
         return SessionContextHolder.isScope(scopes);
         return SessionContextHolder.isScope(scopes);
     }
     }
 
 
+    /**
+     * 判断是否是指定渠道
+     *
+     * @param channels 渠道数组
+     * @return true/false
+     */
+    public boolean isChannel(@NonNull String... channels) {
+        return SessionContextHolder.isChannel(channels);
+    }
+
     /**
     /**
      * 判断是否是指定平台
      * 判断是否是指定平台
      *
      *
@@ -167,4 +188,28 @@ public class MethodSecurityExpression implements MethodSecurityExpressionOperati
     public boolean isTerminal(@NonNull Terminal... terminals) {
     public boolean isTerminal(@NonNull Terminal... terminals) {
         return SessionContextHolder.isTerminal(terminals);
         return SessionContextHolder.isTerminal(terminals);
     }
     }
+
+    /**
+     * 判断是否在指定版本之前
+     *
+     * @param version  指定版本
+     * @param contains 是否包含当前版本号
+     * @return true/false
+     */
+    public boolean isBeforeVersion(@NonNull String version, boolean contains) {
+        Terminal terminal = SessionContextHolder.getTerminal();
+        return terminal != null && SessionContextHolder.isBeforeVersion(terminal, version, contains);
+    }
+
+    /**
+     * 判断是否在指定版本之后
+     *
+     * @param version  指定版本
+     * @param contains 是否包含当前版本号
+     * @return true/false
+     */
+    public boolean isAfterVersion(@NonNull String version, boolean contains) {
+        Terminal terminal = SessionContextHolder.getTerminal();
+        return terminal != null && SessionContextHolder.isAfterVersion(terminal, version, contains);
+    }
 }
 }