wildfly配置mysql数据源,Wildfly-在运行时设置数据源密码

I am developing a server application using Jboss wildfly 8.1 and JPA Hibernate. The problem is, that the JPA datasource creditials have to be loaded at runtime (password). When the server starts, it connects to an encrypted storage where it retrieves password to real database. After that, it should establish connection to the real database.

I tried several things already:

Lookup the datasource through JNDI and rebind it with actual DS.

Lookup the entityManagerFactory through JNDI and rebind it with custom EntityManager.

but none of these work. Do you have idea how to solve it?

my config:

persistence.xml:

org.hibernate.jpa.HibernatePersistenceProvider

java:jboss/datasources/Datasource

...classes...

datasource (defined in standalone.xml):

jdbc:mysql://localhost:3306/repository

false

false

convertToNull

utf8

true

utf8_unicode_ci

mysql

user

TO_BE_DEFINED

accessing entity manager:

@Stateless

@Local

public class GenericDataBean {

@PersistenceContext(type=PersistenceContextType.TRANSACTION)

private EntityManager em;

...

}

解决方案

A possible solution for your problem is use a security domain for the datasource.

In your case you must create custom login module responsible to load password from encrypted storage.

Your configuration should be similar to.

Datasource:

.....

EncryptedPassword

Security Donain:

Login module implementation:

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 is a required option");

}

}

public boolean login() throws LoginException{

if( super.login() == true )

return true;

super.loginOk = true;

return 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 to load encrypted password;

PasswordCredential cred = new PasswordCredential(username, password);

cred.setManagedConnectionFactory(getMcf());

SubjectActions.addCredentials(subject, cred);

}

catch(Exception e){

throw new LoginException("Failed to load encrypted password: "+e.getMessage());

}

return true;

}

public boolean abort(){

username = null;

return true;

}

protected Principal getIdentity(){

Principal principal = new SimplePrincipal(username);

return principal;

}

protected Group[] getRoleSets() throws LoginException{

Group[] empty = new Group[0];

return empty;

}

}

maybe this can help.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值