如何从部署到Websphere 6.1的EJB访问身份验证别名

时间:2011-01-11 22:43:55

标签: java security websphere jaas websphere-6.1

我需要在EJB中为密钥库提供密码,但我不希望它对开发人员可见。我的想法是在Websphere Console中创建身份验证别名,然后查找MY_ALIAS并从别名获取密码。 我找到了一些与主题相关的讨论: http://www.coderanch.com/t/79439/Websphere/Authentication-Data

有人知道可以别名吗?你建议的方法是什么来实现我的目标?

非常感谢!

1 个答案:

答案 0 :(得分:7)

您可以使用以下代码从J2C身份验证数据条目中获取凭据:

import com.ibm.wsspi.security.auth.callback.Constants;
import com.ibm.wsspi.security.auth.callback.WSMappingCallbackHandlerFactory;
import javax.resource.spi.security.PasswordCredential;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginContext;

Map map = new HashMap();
map.put(Constants.MAPPING_ALIAS, "YOUR_J2C_DATA_ALIAS");
CallbackHandler callbackHandler = WSMappingCallbackHandlerFactory.getInstance().getCallbackHandler(map, null);

LoginContext loginContext = new LoginContext("DefaultPrincipalMapping", callbackHandler);
loginContext.login();

Subject subject = loginContext.getSubject();
Set credentials = subject.getPrivateCredentials();

PasswordCredential passwordCredential = (PasswordCredential) credentials.iterator().next();

String user = passwordCredential.getUserName();
String password = new String(passwordCredential.getPassword());