我正在处理命令行运行程序和web应用程序上的spring-boot。这两个应用程序都需要用oracle钱包实现,所以我实现了oracle钱包。命令行运行程序可以使用oracle数据源使用springjdbc模板连接到数据库,但相同的配置无法为数据源对象创建bean。当用数据库用户名和密码实现了相同的功能后,我就可以连接了。
我从这篇文章中获得帮助-[通过Oracle钱包身份验证从Spring jdbc连接到Oracle数据库
代码类似于,
System.setProperty("oracle.net.tns_admin", "path/to/your/tnsnames");
OracleDataSource ds = new OracleDataSource();
Properties props = new Properties();
props.put("oracle.net.wallet_location", "(source=(method=file)(method_data=(directory=path/to/your/wallet)))");
ds.setConnectionProperties( props );
ds.setURL("jdbc:oracle:thin:/@dbAlias"); //dbAlias should match what's in your tnsnames
return ds;
我从启动应用程序的application.properties中设置了所有属性,并且在创建数据源时遇到了空指针异常。
我们将非常感谢在这方面的任何指示或帮助。
在尝试之后,我可以知道当我们需要在Spring启动中包含oracle钱包时,我们需要做什么。
1. In application.properties put two properties,
A> spring.datasource.url=jdbc:oracle:thin:/@<DB_ALIAS_NAME>
B> spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
2. On boot runner/configuration class,
A> Define the dataSource bean like this,
@Bean
public DataSource dataSource() {
OracleDataSource dataSource = null;
try {
dataSource = new OracleDataSource();
Properties props = new Properties();
String oracle_net_wallet_location =
System.getProperty("oracle.net.wallet_location");
props.put("oracle.net.wallet_location", "(source=(method=file)(method_data=(directory="+oracle_net_wallet_location+")))");
dataSource.setConnectionProperties(props);
dataSource.setURL(url);
} catch(Exception e) {
e.printStackTrace();
}
return dataSource;
}
B> Define the jdbcTemplate bean as follows,
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
jdbcTemplate.setFetchSize(20000);
return jdbcTemplate;
}
3. Now we need to set jvm arguments in boot runner class like as follows,
-Doracle.net.wallet_location=<PATH_TO_WALLET_DIR> -Doracle.net.tns_admin=<PATH_TO_WALLET_DIR>
Note - <WALLET_DIR> should contain .sso, .p12 and .ora files. On external
server like tomcat, set above two variables on catalina.sh or catalina.bat
depending on your environment OS.
I hope this helps.
Thanks,
Sandip
add below in application properties
spring.datasource.url=jdbc:oracle:thin:@db202007181319_medium?TNS_ADMIN=C:/wallet/Wallet_Name
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.username=ADMIN
spring.datasource.password=yourpassword
Also pom entry as follows : -
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.6.0.0</version>
</dependency>
除了上述步骤之外,请确保在正确的版本级别添加这些依赖项:
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>osdt_cert</artifactId>
<version>21.1.0.0</version>
</dependency>
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>osdt_core</artifactId>
<version>21.1.0.0</version>
</dependency>
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>oraclepki</artifactId>
<version>21.1.0.0</version>
</dependency>