springboot解密JNDItomcat密码

Springboot decrypt JNDI tomcat password

我在 tomcat 中对 JNDI 进行了以下配置。我在 server.xml

中加密了我的密码
<Resource auth="Container" 
          driverClassName="oracle.jdbc.driver.OracleDriver" 
          global="jdbc/dbsource" 
          maxIdle="30" 
          maxTotal="1000" 
          maxWaitMillis="100000" 
          name="jdbc/dbsource" 
          password=<<unencrypted password>> 
          type="javax.sql.DataSource" 
          url=<<dburl>> 
          username="user" />

我是运行一个spring引导应用程序,我在application.properties中配置了JNDI名称如下

spring.datasource.jndi-name=java:comp/env/jdbc/dbsource

我直接将 JDBCTemplate 自动装配到我的 Bean class 以连接到 Oracle 数据库。

我想在 tomcat server.xml 中加密我的密码。如何覆盖 spring 启动时的自动配置以解密密码?

您有很多选择。下面是两个。

如果您有权访问 server.xml(假设这是 Tomcat)文件,您可以指定您自己的 BasicDataSourceFactory 实现 - 因此这将是初始化的工厂数据源。在您的自定义实现中,您可以利用解密代码服务来处理加密密码。

如果您无权访问 server.xml,一种方法是使用 UserCredentialsDataSourceAdapter.

代理目标 JNDI 数据源

An adapter for a target JDBC DataSource, applying the specified user credentials to every standard getConnection() call, implicitly invoking getConnection(username, password) on the target. All other methods simply delegate to the corresponding methods of the target DataSource.

所以基本上这可能是您的流程:

1) 加载 JNDI 数据源

2) 将 JNDI 数据源转换为 Tomcat DataSource to be able to call getUsername() and getPassword()

3) 在 UserCredentialsDataSourceAdapter 中,在调用 getConnection(username, password) 之前解密密码 - 所有其他数据源调用将委托给原始数据源,只有getConnection(username, password) 将被代理。