Spring 属性 与 Proguard 绑定

Spring property binding with Proguard

我使用 Proguard 混淆了以下 Spring 配置 class。我在下面给出了我使用的 Proguard 选项。 class 应该绑定到 属性 文件中名为 http.cache.timeToLiveInDays 的 属性。

@ConfigurationProperties(prefix = "onboarding", ignoreUnknownFields = false)
public class OnboardingProperties {
    private final Http http = new Http();

    public Http getHttp() {
        return http;
    }

    public static class Http {
        private final Cache cache = new Cache();

        public Cache getCache() {
            return cache;
        }

        public static class Cache {
            private int timeToLiveInDays = 1461;

            public int getTimeToLiveInDays() {
                return timeToLiveInDays;
            }
            public void setTimeToLiveInDays(final int timeToLiveInDays) {
                this.timeToLiveInDays = timeToLiveInDays;
            }
        }
    }
}

属性 文件 application.yml 包含以下内容,

onboarding:
    http:
        cache:
            timeToLiveInDays: 1234

Proguard 配置,

-injars ../$FINAL_NAME$/WEB-INF/lib/$FINAL_NAME$.jar

-outjars ./

-dontoptimize
# -dontshrink
# -dontusemixedcaseclassnames
# -dontpreverify
-verbose
-printseeds seeds.txt

-optimizations !class/marking/final

-adaptresourcefilenames **.properties
-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF

-keep public class com.test.blah.OnboardingApp {
  public static void main(java.lang.String[]);
}
-keep public class * extends org.springframework.boot.web.support.SpringBootServletInitializer

-keep class com.test.blah**.dto.** {
  void set*(***);
  *** get*();
}

-keep public class com.test.blah.config.OnboardingProperties
-keep public class com.test.blah.config.OnboardingProperties$* {
  *;
}

-keep class org.springframework.**
-keep class liquibase.**
-keep interface org.springframework.**
-keep interface liquibase.**

-keepclassmembers class * {
  @org.springframework.beans.factory.annotation.Autowired *;
  @org.springframework.beans.factory.annotation.Qualifier *;
  @org.springframework.beans.factory.annotation.Value *;
  @org.springframework.beans.factory.annotation.Required *;
  @org.springframework.context.annotation.Bean *;
  @org.springframework.context.annotation.Primary *;
  @org.springframework.boot.context.properties.ConfigurationProperties *;
  @javax.inject.Inject *;
  @javax.annotation.PostConstruct *;
  @javax.annotation.PreDestroy *;
}

-keep @org.springframework.stereotype.Service class *
-keep @org.springframework.stereotype.Controller class *
-keep @org.springframework.stereotype.Component class *
-keep @org.springframework.stereotype.Repository class *
-keep @org.springframework.context.annotation.Configuration class *
-keep @org.springframework.web.bind.annotation.ControllerAdvice class *
-keep @org.springframework.boot.context.properties.ConfigurationProperties class *
-keep @org.springframework.boot.autoconfigure.SpringBootApplication class *

-allowaccessmodification
-keepattributes Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,Synthetic,EnclosingMethod
-keepattributes *Annotation*
-keepattributes Signature
-keepattributes Exceptions
-keepattributes InnerClasses

-keepparameternames
-keepdirectories

-keepclassmembernames class * {
  java.lang.Class class$(java.lang.String);
  java.lang.Class class$(java.lang.String, boolean);
}

-keepclassmembers enum * {
  public static **[] values();
  public static ** valueOf(java.lang.String);
  public static ** fromValue(java.lang.String);
}

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
  static final long serialVersionUID;
  private static final java.io.ObjectStreamField[] serialPersistentFields;
  !static !transient <fields>;
  !private <fields>;
  !private <methods>;
  private void writeObject(java.io.ObjectOutputStream);
  private void readObject(java.io.ObjectInputStream);
  java.lang.Object writeReplace();
  java.lang.Object readResolve();
}

不幸的是,当它试图绑定到 属性.

时,我似乎遇到了以下错误
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'http[cache][timeToLiveInDays]' of bean class [com.test.blah.config.OnboardingProperties]: Cannot access indexed value in property referenced in indexed property path 'http[cache][timeToLiveInDays]'; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'http[cache][timeToLiveInDays]' of bean class [com.test.blah.config.OnboardingProperties]: Bean property 'http[cache][timeToLiveInDays]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHoldingValue(AbstractNestablePropertyAccessor.java:405)
    at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:298)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:289)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280)
    at org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanWrapper.setPropertyValue(RelaxedDataBinder.java:699)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:859)
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:755)
    at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128)
    at org.springframework.validation.DataBinder.bind(DataBinder.java:740)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:272)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:329)
    ... 70 more
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'http[cache][timeToLiveInDays]' of bean class [com.test.blah.config.OnboardingProperties]: Bean property 'http[cache][timeToLiveInDays]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:633)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHoldingValue(AbstractNestablePropertyAccessor.java:402)
    ... 82 more

有什么办法可以解决这个问题吗?

我设法解决了这个问题,

-keep public class com.test.blah.config.OnboardingProperties {
  private <fields>;
  *** get*();
}
-keep class com.test.blah.config.OnboardingProperties$* {
  private <fields>;
  void set*(***);
  *** get*();
}

而不是,

-keep public class com.test.blah.config.OnboardingProperties
-keep public class com.test.blah.config.OnboardingProperties$* {
  *;
}