1

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?

2 Answers2

2

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
Jose Luis
  • 254
  • 2
  • 11
  • Thanks, but user and password are encrypted in files, and i need two different files for user and password. – Alex Vasilescu Feb 01 '19 at 12:49
  • In that case I guess you will need to create your coustom proper source. You have to create a class extending PropertySource and implement the method public Object getProperty(String name). In this class you will be able to access the encrypted files, decrypt them and parse them to retrieve de properties you need. You will need as well to register the property source in the env doing env.getPropertySources().addFirst(new YourCustomPropertySource(param)); example here: https://stackoverflow.com/questions/14416005/spring-environment-property-source-configuration – Jose Luis Feb 01 '19 at 13:04
  • Thanks, i will try this! – Alex Vasilescu Feb 01 '19 at 13:09
  • Hope it helps. :) – Jose Luis Feb 01 '19 at 16:50
0

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.

Simon
  • 925
  • 3
  • 13
  • 30