java ee - Wildfly - set datasource password at runtime -



java ee - Wildfly - set datasource password at runtime -

i developing server application using jboss wildfly 8.1 , jpa hibernate. problem is, jpa datasource creditials have loaded @ runtime (password). when server starts, connects encrypted storage retrieves password real database. after that, should found connection real database.

i tried several things already: lookup datasource through jndi , rebind actual ds. lookup entitymanagerfactory through jndi , rebind custom entitymanager.

but none of these work. have thought how solve it?

my config:

persistence.xml:

<?xml version="1.0" encoding="utf-8"?> <persistence version="2.0"> <persistence-unit name="persistence_unit" transaction-type="jta"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <jta-data-source>java:jboss/datasources/datasource</jta-data-source> ...classes... <properties> <!-- properties hibernate --> <property name="hibernate.dialect" value="org.hibernate.dialect.mysql5innodbdialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="false"/> <property name="hibernate.format_sql" value="false"></property> <property name="hibernate.connection.useunicode" value="true"/> <property name="hibernate.connection.characterencoding" value="utf-8"/> <property name="hibernate.connection.charset" value="utf-8"/> <property name="org.hibernate.flushmode" value="commit" /> <!-- 1 of import --> <property name="jboss.entity.manager.factory.jndi.name" value="java:/entitymanagerfactory"/> <property name="jboss.entity.manager.jndi.name" value="java:/manager1"/> </properties> </persistence-unit> </persistence>

datasource (defined in standalone.xml):

<datasource jndi-name="java:jboss/datasources/datasource" pool-name="ds" enabled="true" use-java-context="true"> <connection-url>jdbc:mysql://localhost:3306/repository</connection-url> <connection-property name="usecompression"> false </connection-property> <connection-property name="logslowqueries"> false </connection-property> <connection-property name="zerodatetimebehavior"> converttonull </connection-property> <connection-property name="characterencoding"> utf8 </connection-property> <connection-property name="useunicode"> true </connection-property> <connection-property name="connectioncollation"> utf8_unicode_ci </connection-property> <driver>mysql</driver> <security> <user-name>user</user-name> <password>to_be_defined</password> </security> </datasource>

accessing entity manager:

@stateless @local public class genericdatabean { @persistencecontext(type=persistencecontexttype.transaction) private entitymanager em; ... }

a possible solution problem utilize security domain datasource. in case must create custom login module responsible load password encrypted storage. configuration should similar to.

datasource:

class="lang-xml prettyprint-override"><datasource ... > ..... <security> <security-domain>encryptedpassword</security-domain> </security> </datasource>

security donain:

class="lang-xml prettyprint-override"><security-domain name="encryptedpassword"> <authentication> <login-module code="com.example.encryptedpasswordloginmodule" flag="required"> <!-- list of options --> <module-option name="username" value="theusername"/> <module-option name="managedconnectionfactoryname" value="jboss.jca:service=localtxcm,name=ds"/> </login-module> </authentication> </security-domain>

login module implementation:

class="lang-java prettyprint-override">public class encryptedpasswordloginmodule extends abstractpasswordcredentialloginmodule{ private string username; public void initialize(subject subject, callbackhandler handler, map sharedstate, map options){ super.initialize(subject, handler, sharedstate, options); username = (string) options.get("username"); if( username == null ){ throw new illegalargumentexception("the user name required option"); } } public boolean login() throws loginexception{ if( super.login() == true ) homecoming true; super.loginok = true; homecoming true; } public boolean commit() throws loginexception{ principal principal = new simpleprincipal(username); subjectactions.addprincipals(subject, principal); sharedstate.put("javax.security.auth.login.name", username); try{ char[] password = .... //code load encrypted password; passwordcredential cred = new passwordcredential(username, password); cred.setmanagedconnectionfactory(getmcf()); subjectactions.addcredentials(subject, cred); } catch(exception e){ throw new loginexception("failed load encrypted password: "+e.getmessage()); } homecoming true; } public boolean abort(){ username = null; homecoming true; } protected principal getidentity(){ principal principal = new simpleprincipal(username); homecoming principal; } protected group[] getrolesets() throws loginexception{ group[] empty = new group[0]; homecoming empty; } }

maybe can help.

java-ee jpa jboss datasource wildfly-8

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -