具有Jasypt加密的Spring Boot外部化配置(引导问题)

时间:2019-03-09 12:55:49

标签: spring-boot jasypt

在我的Spring启动应用程序中,当我尝试使用具有Jasypt加密密码的uri模式连接到MongoDB时,出现Auth异常。在调试时,我确实看到使用URI时Jasypt解密没有发生,而使用单独的mongo配置(请参见下文)则连接成功。在日志中,加密的字符串ENC(Qev1vJ3DummyPSUApeQfgdgfd==)将按原样打印而没有解密。整个配置通过命令行使用SPRING_APPLICATION_JSON

传递

使用uri :(不起作用)

java -Dspring.application.json =“ {低于json}” -jar myapp.jar

{
    "spring": {
        "data": {
            "mongodb": {
                "uri": "mongodb://myusername:${spring.data.mongodb.password}@localhost:27017/mydb",
                "password": "${mongodb.password}"
            }
        }
    },
    "jasypt": {
        "encryptor": {
            "password": "myjasypt-password"
        }
    },
    "mongodb": {
        "password": "ENC(Qev1vJ3DummyPSUApeQfgdgfd==)"
    }
}

With Individual property setting :(工作正常)

java -Dspring.application.json =“ {低于json}” -jar myapp.jar

{
    "spring": {
        "data": {
            "mongodb": {
                "host": "localhost",
                "port": "27017",
                "database": "mydb",
                "username": "myusername",
                "password": "${mongodb.password}"
            }
        }
    },
    "jasypt": {
        "encryptor": {
            "password": "myjasypt-password"
        }
    },
    "mongodb": {
        "password": "ENC(Qev1vJ3DummyPSUApeQfgdgfd==)"
    }
}

Spring Boot:1.5.2。发布| MongoDB:3.4.2 |茉莉花:1.14

例外:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName=myusername, source=mydb, password=<hidden>, mechanismProperties={}}

在uri中使用Jasypt解密时似乎没有发生。我不确定这是否与配置自举有关。任何指针都会有所帮助。

2 个答案:

答案 0 :(得分:0)

我建议像这样直接指定加密属性

{
    "spring": {
        "data": {
            "mongodb": {
                "uri": "mongodb://myusername:${mongodb.password}@localhost:27017/mydb"
            }
        }
    },
    "jasypt": {
        "encryptor": {
            "password": "myjasypt-password"
        }
    },
    "mongodb": {
        "password": "ENC(Qev1vJ3DummyPSUApeQfgdgfd==)"
    }
}

在我的书中,这两个示例是等效的

答案 1 :(得分:0)

找到了答案。看来该问题存在于Jasypt 1.14中,并已在1.17版及更高版本中得到解决。

如果无法更改1.14版本,请在 resources / META-INF / spring.factories

中添加以下条目
    @media screen and (min-width: 768px) {
      .filters-group-wrap {
        display: flex;
        justify-content: space-between;
      }
    }

    .btn {
      width: 60px;
      height: 60px;
      background-size: cover;
      display: inline-block;
      border-radius: 30px;
      -webkit-border-radius: 30px;
      -moz-border-radius: 30px;
      padding: 0px;
      padding-top: 5px;
      text-align: center;
      line-height: 130px;
      margin: 10px;
      margin-top: 0px;
      margin-bottom: 35px;
      color: #888888;
      font-size: 1em;
      transition: .2s ease-out;
      cursor: pointer;
      border: none;
      font-size: .8em;
    }

    @media (-moz-touch-enabled:0),(pointer: fine) {
      .btn:hover {
        color:#CCCCCC;
          }
    }

    .btn:focus {
      outline: none;
      box-shadow: 2px 1px 4px 1px rgba(75,75,75,1);
    }

    @media screen and (max-width: 767px) {
      .btn {
      }
    }

在版本1.17中,Jasypt的 <div class="filter-options" data-toggle="buttons"> <button id="showA" class="btn" data-group="groupA" style="background-image: url('A.jpg')">A</button> <button id="showB" class="btn" data-group="groupB" style="background-image: url('B.jpg')">B</button> <button id="showC" class="btn" data-group="groupC" style="background-image: url('C.jpg')">C</button> </div>使用Applicationcontextinitializer自定义org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ulisesbocchio.jasyptspringboot.JasyptSpringBootAutoConfiguration