Search code examples
spring-bootlogbackjasypt

logback.xml springproperty: How to decrpyt password


We are using spring-boot and logback and store database username & encrypted password in spring application.yaml. How we can get the decrypted password in logback.xml so dbAppender can connect to database?

Thank you for help.

application.yaml

datasource:
        username: root
        password: ENC(xxxxxxxxxxxxxxxxx)

logback.xml

    <springProperty scope="context" name="db.user" source="datasource.username"/>
    <springProperty scope="context" name="db.password" source="datasource.password"/>
    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
      <driverClass>com.mysql.jdbc.Driver</driverClass>
      <url>jdbc:mysql://host_name:3306/datebase_name</url>
      <username>${db.user}</username>
      <password>${db.password}</password>
    </connectionSource>
  </appender>

Solution

  • For encrypting property, please use Encryption and Decryption.

    For using properties in logback please see Logback extensions.

    However, you can't actually make it in logback-spring.xml. Cause it's initialized too early and the encrypted properties are not ready. Please see issue.