0

I am trying to figure out a way to provide user and password without hardcoding them on the same server. I need to fetch them through an api.


            <!-- MySQL -->
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mysql://localhost:3306/mydatabase"/>

            <!-- Credentials -->
            <property name="javax.persistence.jdbc.user"
                      value="this is user"/>
            <property name="javax.persistence.jdbc.password"
                      value="this is password"/>

Above is how it is being done right now but I want to wire these so anyone who has code access still can't see the user and password. How can I achieve that? I don't have JPA in this project.

lkatiforis
  • 5,703
  • 2
  • 16
  • 35

2 Answers2

0

I’m not sure precisely what you’re requirements are, but a very flexible option is to implement a RectiveConnectionPool.

https://hibernate.org/reactive/documentation/1.1/reference/html_single/#_custom_connection_management_and_multitenancy

Gavin King
  • 3,182
  • 1
  • 13
  • 11
0

You can do it using this method of javax.persistence.Persistence:

/**
     * Create and return an EntityManagerFactory for the named persistence unit
     * using the given properties.
     * 
     * @param persistenceUnitName
     *            the name of the persistence unit
     * @param properties
     *            Additional properties to use when creating the factory. 
     *            These properties may include properties to control
     *            schema generation.  The values of these properties override 
     *            any values that may have been configured elsewhere.
     * @return the factory that creates EntityManagers configured according to
     *         the specified persistence unit.
     */
    public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {

Fetch the username and password from your API and store them in a Map like this:

    var props = new HashMap<String, String>();
    fetch("javax.persistence.jdbc.url", props);
    fetch("javax.persistence.jdbc.password", props);
    fetch("javax.persistence.jdbc.user", props);

and then call:

    emf = Persistence
        .createEntityManagerFactory("hibernate-config", props)
morpheus
  • 18,676
  • 24
  • 96
  • 159