Prechádzať zdrojové kódy

修复对象弱引用导致空指针异常问题;优化OAuth JwtDecoder初始化逻辑;

woody 3 mesiacov pred
rodič
commit
ec2be098f6

+ 12 - 14
framework-base/src/main/java/com/chelvc/framework/base/context/ApplicationContextHolder.java

@@ -795,22 +795,20 @@ public class ApplicationContextHolder implements ApplicationContextAware, Proper
      */
     @SuppressWarnings("unchecked")
     public static <T> T getProperty(@NonNull String key, @NonNull Function<String, T> function) {
-        Object object = ObjectUtils.ifNull(PROPERTY_OBJECT_MAPPING.get(key), WeakReference::get);
-        if (object == null) {
-            object = PROPERTY_OBJECT_MAPPING.computeIfAbsent(key, k -> {
-                Object value = null;
-                Environment environment = getEnvironment(false);
-                if (environment != null) {
-                    try {
-                        value = function.apply(environment.getProperty(key));
-                    } catch (Exception e) {
-                        log.error("Get property value failed: {}", key, e);
-                    }
+        Object value = ObjectUtils.ifNull(PROPERTY_OBJECT_MAPPING.get(key), WeakReference::get);
+        if (value == null) {
+            Environment environment = getEnvironment(false);
+            if (environment != null) {
+                try {
+                    value = function.apply(environment.getProperty(key));
+                } catch (Exception e) {
+                    log.error("Get property value failed: {}", key, e);
                 }
-                return new WeakReference<>(ObjectUtils.ifNull(value, ObjectUtils.EMPTY));
-            }).get();
+            }
+            value = ObjectUtils.ifNull(value, ObjectUtils.EMPTY);
+            PROPERTY_OBJECT_MAPPING.put(key, new WeakReference<>(value));
         }
-        return object == ObjectUtils.EMPTY ? null : (T) object;
+        return value == ObjectUtils.EMPTY ? null : (T) value;
     }
 
     /**

+ 21 - 13
framework-common/src/main/java/com/chelvc/framework/common/util/ObjectUtils.java

@@ -450,9 +450,11 @@ public final class ObjectUtils {
      */
     public static Map<String, Field> getClassFields(@NonNull Class<?> clazz) {
         Map<String, Field> fields = ObjectUtils.ifNull(CLASS_FIELD_MAPPING.get(clazz), WeakReference::get);
-        return fields != null ? fields : CLASS_FIELD_MAPPING.computeIfAbsent(
-                clazz, k -> new WeakReference<>(findClassFields(clazz))
-        ).get();
+        if (fields == null) {
+            fields = findClassFields(clazz);
+            CLASS_FIELD_MAPPING.put(clazz, new WeakReference<>(fields));
+        }
+        return fields;
     }
 
     /**
@@ -874,9 +876,11 @@ public final class ObjectUtils {
     @SuppressWarnings("unchecked")
     private static <M, T extends Schema<M>> T getProtostuffSchema(@NonNull Class<M> clazz) {
         T schema = (T) ObjectUtils.ifNull(PROTOSTUFF_SCHEMA_MAPPING.get(clazz), WeakReference::get);
-        return schema != null ? schema : (T) PROTOSTUFF_SCHEMA_MAPPING.computeIfAbsent(
-                clazz, k -> new WeakReference<>(RuntimeSchema.getSchema(clazz))
-        ).get();
+        if (schema == null) {
+            schema = (T) RuntimeSchema.getSchema(clazz);
+            PROTOSTUFF_SCHEMA_MAPPING.put(clazz, new WeakReference<>(schema));
+        }
+        return schema;
     }
 
     /**
@@ -925,15 +929,17 @@ public final class ObjectUtils {
     public static List<PropertyDescriptor> getPropertyDescriptors(@NonNull Class<?> clazz) {
         List<PropertyDescriptor> descriptors =
                 ObjectUtils.ifNull(PROPERTY_DESCRIPTOR_MAPPING.get(clazz), WeakReference::get);
-        return descriptors != null ? descriptors : PROPERTY_DESCRIPTOR_MAPPING.computeIfAbsent(clazz, k -> {
+        if (descriptors == null) {
             BeanInfo bean;
             try {
                 bean = Introspector.getBeanInfo(clazz, Object.class);
             } catch (IntrospectionException e) {
                 throw new RuntimeException(e);
             }
-            return new WeakReference<>(Arrays.asList(bean.getPropertyDescriptors()));
-        }).get();
+            descriptors = Arrays.asList(bean.getPropertyDescriptors());
+            PROPERTY_DESCRIPTOR_MAPPING.put(clazz, new WeakReference<>(descriptors));
+        }
+        return descriptors;
     }
 
     /**
@@ -957,15 +963,17 @@ public final class ObjectUtils {
     private static SerializedLambda lookupFunctionLambda(Serializable function) {
         Class<?> clazz = function.getClass();
         SerializedLambda lambda = ObjectUtils.ifNull(LAMBDA_FUNCTION_MAPPING.get(clazz), WeakReference::get);
-        return lambda != null ? lambda : LAMBDA_FUNCTION_MAPPING.computeIfAbsent(clazz, k -> {
+        if (lambda == null) {
             try {
-                Method method = k.getDeclaredMethod("writeReplace");
+                Method method = clazz.getDeclaredMethod("writeReplace");
                 method.setAccessible(true);
-                return new WeakReference<>((SerializedLambda) method.invoke(function));
+                lambda = (SerializedLambda) method.invoke(function);
             } catch (ReflectiveOperationException e) {
                 throw new RuntimeException(e);
             }
-        }).get();
+            LAMBDA_FUNCTION_MAPPING.put(clazz, new WeakReference<>(lambda));
+        }
+        return lambda;
     }
 
     /**

+ 5 - 3
framework-common/src/main/java/com/chelvc/framework/common/util/StringUtils.java

@@ -342,9 +342,11 @@ public final class StringUtils {
      */
     public static Pattern getPattern(@NonNull String regex) {
         Pattern pattern = ObjectUtils.ifNull(REGEX_PATTERN_MAPPING.get(regex), WeakReference::get);
-        return pattern != null ? pattern : REGEX_PATTERN_MAPPING.computeIfAbsent(
-                regex, k -> new WeakReference<>(Pattern.compile(regex))
-        ).get();
+        if (pattern == null) {
+            pattern = Pattern.compile(regex);
+            REGEX_PATTERN_MAPPING.put(regex, new WeakReference<>(pattern));
+        }
+        return pattern;
     }
 
     /**

+ 7 - 3
framework-security/src/main/java/com/chelvc/framework/security/config/OAuthConfigurer.java

@@ -17,7 +17,6 @@ import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.util.HttpUtils;
 import com.chelvc.framework.base.util.SpringUtils;
 import com.chelvc.framework.common.util.AESUtils;
-import com.chelvc.framework.common.util.AssertUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.security.annotation.Authorize;
@@ -81,9 +80,14 @@ public class OAuthConfigurer extends WebSecurityConfigurerAdapter {
 
     @Bean
     public JwtDecoder jwtDecoder() {
-        // 构建JWT解码器实例
         String secret = this.properties.getOauth().getSecret();
-        AssertUtils.check(StringUtils.notEmpty(secret), () -> "Jwt secret is missing");
+        if (StringUtils.isEmpty(secret)) {
+            return token -> {
+                throw new IllegalStateException("Jwt secret is missing");
+            };
+        }
+
+        // 构建JWT解码器实例
         SecretKey key = new SecretKeySpec(secret.getBytes(), AESUtils.ALGORITHM);
         NimbusJwtDecoder decoder = NimbusJwtDecoder.withSecretKey(key).build();