79284064

Date: 2024-12-16 08:40:39
Score: 1
Natty:
Report link

Solution

Thinking track

# example
server:
  port: ${SERVER_PORT:8080}

Solution detail

# 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)

Conclusion

In this article, we found that how to use gcp secretmanager after Spring-Boot upgrade to version 3.4.0. I thing it's a luxurious trouble. Also see: Official website. Enjoy it.

Reasons:
  • Blacklisted phrase (1): this article
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Raichu