java.lang.IllegalStateException: Cannot decrypt: key=spring.datasource.password

问题描述:

Spring Cloud Config Client 客户端服务启动报错,如下所示:

2020-04-25 10:10:22.089  INFO 4028 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@26275bef: startup date [Sat Apr 25 10:10:22 CST 2020]; root of context hierarchy
2020-04-25 10:10:22.330  INFO 4028 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$ec992a79] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.4.RELEASE)

2020-04-25 10:10:23.854  INFO 4028 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
2020-04-25 10:10:33.699  INFO 4028 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=licensingservice, profiles=[default], label=master, version=56d63a8c0c3dcb0c5c93db1f00cf71856371db8b, state=null
2020-04-25 10:10:33.699  INFO 4028 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='configClient'], MapPropertySource [name='https://github.com/carnellj/config-repo/licensingservice/licensingservice.yml']]]
2020-04-25 10:10:33.711  WARN 4028 --- [           main] o.s.boot.SpringApplication               : Error handling failed (ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4bff64c2: startup date [Thu Jan 01 08:00:00 CST 1970]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@26275bef)
2020-04-25 10:10:33.711 ERROR 4028 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Cannot decrypt: key=spring.datasource.password
	at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:201) ~[spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
	at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:165) ~[spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
	at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.initialize(EnvironmentDecryptApplicationInitializer.java:95) ~[spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
	at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:636) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:350) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
	at com.thoughtmechanix.licenses.Application.main(Application.java:12) [classes/:na]
Caused by: java.lang.UnsupportedOperationException: No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly?
	at org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration$FailsafeTextEncryptor.decrypt(EncryptionBootstrapConfiguration.java:152) ~[spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
	at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:193) ~[spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
	... 8 common frames omitted

解决办法:

1、在本地安装Postgres数据库;

2、修改Spring Cloud Config Server 服务端 Postgres数据库连接配置文件licensingservice-dev.yml,如下所示:

spring.jpa.database: "POSTGRESQL"
spring.datasource.platform:  "postgres"
spring.jpa.show-sql: "false"
spring.database.driverClassName: "org.postgresql.Driver"
# spring.datasource.url: "jdbc:postgresql://database:5432/eagle_eye_dev"
spring.datasource.url: "jdbc:postgresql://localhost:5432/eagle_eye_dev"
# spring.datasource.username: "postgres_dev"
spring.datasource.username: "postgres"
# spring.datasource.password: "{cipher}d495ce8603af958b2526967648aa9620b7e834c4eaff66014aa805450736e119"
spring.datasource.password: "{cipher}2c644c00b51dbdd03116edd77be62774fc94b3c646f1c553015102680e1d1868"
spring.datasource.testWhileIdle: "true"
spring.datasource.validationQuery: "SELECT 1"
spring.jpa.properties.hibernate.dialect: "org.hibernate.dialect.PostgreSQLDialect"
redis.server: "redis"
redis.port: "6379"
signing.key: "345345fsdfsf5345"

3、修改Spring Cloud Config Server 服务端属性配置文件application.yml,如下所示:

#
#   This is the core configuration for a configuration service.  I have two examples here:
#
#       -  A configuration service which completelty loads the configuration for the services
#          from the local file server of the config service.  This should only be used for
#          demonstration purposes or for a small application with a limited number of services.
#
#       -  A configuraton service that uses a git-based repository to read the files from
#


####################################
# server:
#   port: 8888
# spring:
#   cloud:
#     config:
#       server:
#         encrypt.enabled: false
#         git:
#           uri: https://github.com/carnellj/config-repo/
#           searchPaths: licensingservice,organizationservice
#           username: native-cloud-apps
#           password: 0ffended


####
#Classpath and file-based solution
####

server:
   port: 8888
spring:
  profiles:
    active: native
  cloud:
     config:
       server:
           encrypt:
              enabled: false
           native:
#              searchLocations: file:///confsvr/src/main/resources/config/licensingservice,
#                               file://confsvr/src/main/resources/config/organizationservice
               searchLocations: classpath:config/,classpath:config/licensingservice

4、修改Spring Cloud Config Server 服务端属性配置文件bootstrap.yml,如下所示:

spring:
  application:
    name: configserver
encrypt:
  key: IMSYMMETRIC

5、修改Spring Cloud Config Client 客户端属性配置文件bootstrap.yml,如下所示:

spring:
  application:
    name: licensingservice
  profiles:
    active:
      dev
  cloud:
    config:
      uri: http://localhost:8888

6、访问http://localhost:8888/licensingservice/dev,返回结果如下所示:

{
    "name": "licensingservice",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "classpath:config/licensingservice/licensingservice-dev.yml",
            "source": {
                "spring.jpa.database": "POSTGRESQL",
                "spring.datasource.platform": "postgres",
                "spring.jpa.show-sql": "false",
                "spring.database.driverClassName": "org.postgresql.Driver",
                "spring.datasource.url": "jdbc:postgresql://localhost:5432/eagle_eye_dev",
                "spring.datasource.username": "postgres",
                "spring.datasource.password": "{cipher}2c644c00b51dbdd03116edd77be62774fc94b3c646f1c553015102680e1d1868",
                "spring.datasource.testWhileIdle": "true",
                "spring.datasource.validationQuery": "SELECT 1",
                "spring.jpa.properties.hibernate.dialect": "org.hibernate.dialect.PostgreSQLDialect",
                "redis.server": "redis",
                "redis.port": "6379",
                "signing.key": "345345fsdfsf5345"
            }
        },
        {
            "name": "classpath:config/licensingservice/licensingservice.yml",
            "source": {
                "example.property": "I AM IN THE DEFAULT",
                "spring.jpa.database": "POSTGRESQL",
                "spring.datasource.platform": "postgres",
                "spring.jpa.show-sql": "true",
                "spring.database.driverClassName": "org.postgresql.Driver",
                "spring.datasource.url": "jdbc:postgresql://database:5432/eagle_eye_local",
                "spring.datasource.username": "postgres",
                "spring.datasource.password": "{cipher}4788dfe1ccbe6485934aec2ffeddb06163ea3d616df5fd75be96aadd4df1da91",
                "spring.datasource.testWhileIdle": "true",
                "spring.datasource.validationQuery": "SELECT 1",
                "spring.jpa.properties.hibernate.dialect": "org.hibernate.dialect.PostgreSQLDialect",
                "redis.server": "redis",
                "redis.port": "6379",
                "signing.key": "345345fsdfsf5345"
            }
        }
    ]
}

参考:https://blog.csdn.net/daqiang012/article/details/105465230

你可能感兴趣的:(Spring,Cloud,java,spring,后端)