简体   繁体   中英

Can i load username and password by file in spring boot + hibernate?

I want to use spring boot, oracle db and hibernate. I want to load username and password for oracle db schema by two files( user.txt and password.txt). Can i use hibernate? I don't know how to load the username and the password from file in application.properties for hibernate. (spring.datasource.username = load from user.txt, spring.datasource.password = load from password.txt). User and password are encrypted in files.

Does anyone have an idea how i do that?

There are several ways to do it but using property files, not txt files. If you are willing to follow this approach your options are:

  1. @PorpertySources in your configuration class. An example is

     @PropertySources({ @PropertySource("/user.properties"), @PropertySource("/password.properties") }). @Configuration public configClass{ @Value("{db.user}") private String user; @Value("{db.password}") private String password; ... } 

db.user and db.password must be defined in your config files. With these data you can define your data source (you will need the connection url as well )

  1. If you are using hibernate configuration file you can find an example in this answer: How to read database configuration parameter using properties file in hibernate

Just add another application.properties outside of the .jar, which includes the database information. See spring docs -> https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Place your application.properties outside of your packaged jar file. Spring Boot will look in the same directory for properties and the values can be accessed as if the file was inside the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM