Spring-Boot 升级到版本 3.4.0 使 gcp Secretmanager 无法工作

问题描述 投票:0回答:1

背景

让我们开始将 Spring-Boot 从

3.3.6
升级到
3.4.0

构建.gradle

//build.gradle
//old
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.6'
    id 'io.spring.dependency-management' version '1.1.6'
    id 'org.sonarqube' version '5.1.0.4882'
    id 'jacoco'
    id 'com.gorylenko.gradle-git-properties' version '2.4.2'
    id 'project-report'
}
ext {
    set('springCloudGcpVersion', "5.8.0")
    set('springCloudVersion', "2023.0.4")
}
dependencies {
    implementation platform('com.google.cloud:spring-cloud-gcp-dependencies:5.8.0')
    implementation platform('org.springframework.cloud:spring-cloud-dependencies:2023.0.4')
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation 'org.bouncycastle:bcprov-jdk18on:1.79'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'com.google.cloud:spring-cloud-gcp-starter-secretmanager'
    implementation 'com.google.cloud:spring-cloud-gcp-starter-storage'
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
    implementation 'org.apache.commons:commons-collections4:4.4'
    implementation 'org.mapstruct:mapstruct:1.6.3'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3'
    annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.postgresql:postgresql'
}

dependencyManagement {
    imports {
        mavenBom "com.google.cloud:spring-cloud-gcp-dependencies:${springCloudGcpVersion}"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
//build.gradle
//new
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.4.0'
    id 'io.spring.dependency-management' version '1.1.6'
    id 'org.sonarqube' version '5.1.0.4882'
    id 'jacoco'
    id 'com.gorylenko.gradle-git-properties' version '2.4.2'
    id 'project-report'
}
ext {
    set('springCloudGcpVersion', "5.9.0")
    set('springCloudVersion', "2024.0.0")
}
dependencies {
    implementation platform('com.google.cloud:spring-cloud-gcp-dependencies:5.9.0')
    implementation platform('org.springframework.cloud:spring-cloud-dependencies:2024.0.0')
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation 'org.bouncycastle:bcprov-jdk18on:1.79'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'com.google.cloud:spring-cloud-gcp-starter-secretmanager'
    implementation 'com.google.cloud:spring-cloud-gcp-starter-storage'
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'
    implementation 'org.apache.commons:commons-collections4:4.4'
    implementation 'org.mapstruct:mapstruct:1.6.3'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3'
    annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.postgresql:postgresql'
}

dependencyManagement {
    imports {
        mavenBom "com.google.cloud:spring-cloud-gcp-dependencies:${springCloudGcpVersion}"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

lib版本

  • org.springframework.boot:3.3.6 -> 3.4.0
  • com.google.cloud:spring-cloud-gcp-dependencie:5.8.0 -> 5.9.0
  • org.springframework.cloud:spring-cloud-dependency:2023.0.4 -> 2024.0.0
  • org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0 -> 2.7.0

问题

现在,数据源连接失败,因为使用

secretmanager
注册数据库连接信息。

问题详情及测试

# application.yml
spring:
  config:
    import: "sm://"
  datasource:
    url: jdbc:postgresql://localhost:5432/cehr?currentSchema=XXXX
    username: ${sm://psql-username}
    password: ${sm://psql-password}
@Service
public class XXXService extends CommonService {

    @Autowired
    private SecretManagerTemplate secretManagerTemplate;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${sm://psql-username}")
    private String username2;

    @Value("${sm://psql-password}")
    private String password2;

    public void execute() {

        System.out.println("username = " + username);
        System.out.println("password = " + password);

        System.out.println("username2 = " + username2);
        System.out.println("password2 = " + password2);

        System.out.println("psql-username = " + secretManagerTemplate.getSecretString("sm://psql-username"));
        System.out.println("psql-password = " + secretManagerTemplate.getSecretString("sm://psql-password"));
    }
}
# output
username = //psql-username
password = //psql-password
username2 = //psql-username
password2 = //psql-password
psql-username = ********** (correct)
psql-password = ********** (correct)

问题分析

  • secretmanager
    使用
    SecretManagerTemplate
  • 工作
  • secretmanager
    请勿使用
    application.yml
  • 进行工作
java spring spring-boot google-secret-manager
1个回答
0
投票

解决方案

思维轨迹

# example
server:
  port: ${SERVER_PORT:8080}
  • ':' 是关于
    special characters
    Expression Language ( ${ } )
    之一。 使用此字符设置
    value
    default value
  • 在此示例中,如果设置
    Environment Variable
    SERVER_PORT,则项目使用您设置的端口,否则项目使用默认端口 8080。
  • 如果不想被视为
    special character
    ,必须添加
    Escape Character

解决方案详情

# application.yml
spring:
  config:
    import: "sm://"
  datasource:
    url: jdbc:postgresql://localhost:5432/cehr?currentSchema=XXXX
    username: ${sm\://psql-username}
    password: ${sm\://psql-password}
@Service
public class XXXService extends CommonService {

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${sm://psql-username}")
    private String username2;

    @Value("${sm://psql-password}")
    private String password2;

    @Value("${sm\\://psql-username}")
    private String username3;

    @Value("${sm\\://psql-password}")
    private String password3;

    public void execute() {

        System.out.println("username = " + username);
        System.out.println("password = " + password);

        System.out.println("username2 = " + username2);
        System.out.println("password2 = " + password2);

        System.out.println("username3 = " + username3);
        System.out.println("password3 = " + password3);

        System.out.println("psql-username = " + secretManagerTemplate.getSecretString("sm://psql-username"));
        System.out.println("psql-password = " + secretManagerTemplate.getSecretString("sm://psql-password"));
    }
}
# output
username = ********** (correct)
password = ********** (correct)
username2 = //psql-username
password2 = //psql-password
username3 = ********** (correct)
password3 = ********** (correct)
psql-username = ********** (correct)
psql-password = ********** (correct)

结论

在这篇文章中,我们发现了Spring-Boot升级到3.4.0版本后如何使用gcp Secretmanager。我认为这是一个奢侈的麻烦。 另请参阅:官方网站。 享受它吧。

© www.soinside.com 2019 - 2024. All rights reserved.