可以从自定义LoginModule访问远程EJB? [英] Possible to access remote EJBs from a custom LoginModule?
问题描述
我发现了一些关于如何编写自定义领域和loginModule的提示。我想知道是否可以在自定义loginModule中访问远程EJB。
在我的情况中,我有远程EJB提供对用户实体的访问(通过JPA) - 我可以使用它们(例如通过@EJB注释)吗?
好吧,我自己找到答案:工作正常!
以下是代码: public class UserLoginModule extends AppservPasswordLoginModule {
Logger log = Logger.getLogger(this.getClass()。getName());
private UserFacadeLocal userFacade;
$ b $ public UserLoginModule(){
try {
InitialContext ic = new InitialContext();
userFacade =(UserFacadeLocal)ic.lookup(java:global / MyAppServer / UserFacade!com.skalio.myapp.beans.UserFacadeLocal);
log.info(userFacade bean received);
} catch(NamingException ex){
log.warning(Unable to get userFacade Bean!);
$ b @Override
保护无效authenticateUser()抛出LoginException {
log.fine(试图验证用户'+ _username + ','+ _password +');
用户用户;
//获取领域
UserRealm userRealm =(UserRealm)_currentRealm;
尝试{
user = userFacade.authenticate(_username,_password.trim());
userFacade.detach(user);
} catch(UnauthorizedException e){
log.warning(Authentication failed:+ e.getMessage());
抛出新的LoginException(UserLogin authentication failed!);
$ b $ catch(Exception e){
throw new LoginException(UserLogin failed:+ e.getMessage());
log.fine(Authentication authentication for+ user);
//获取用户所属的组
String [] grpList = userRealm.authorize(user);
if(grpList == null){
throw new LoginException(User is not any member of any groups);
}
//将登录用户添加到主题的主体。
//这是有效的,但不幸的是,我再次无法访问用户对象
//。
Set principals = _subject.getPrincipals();
principals.add(new UserPrincipalImpl(user));
this.commitUserAuthentication(grpList);
}
}
诀窍在于将接口来自战争的豆子。我将所有接口和公共实体绑定在单独的OSGi模块中,并使用 asadmin --type osgi
进行部署。因此,自定义UserLoginModule可以对其进行分类加载。
I found some nice hints on how to write a custom realm and loginModule. I'm wondering though if it is possible to access a remote EJB within the custom loginModule.
In my case, I have remote EJBs that provide access to user-entities (via JPA) -- can I use them (e.g. via @EJB annotation)?
Ok, I found the answer myself: works fine! I can get a reference to the remote SLSB via an InitialContext.
Here's the code:
public class UserLoginModule extends AppservPasswordLoginModule {
Logger log = Logger.getLogger(this.getClass().getName());
private UserFacadeLocal userFacade;
public UserLoginModule() {
try {
InitialContext ic = new InitialContext();
userFacade = (UserFacadeLocal) ic.lookup("java:global/MyAppServer/UserFacade!com.skalio.myapp.beans.UserFacadeLocal");
log.info("userFacade bean received");
} catch (NamingException ex) {
log.warning("Unable to get userFacade Bean!");
}
}
@Override
protected void authenticateUser() throws LoginException {
log.fine("Attempting to authenticate user '"+ _username +"', '"+ _password +"'");
User user;
// get the realm
UserRealm userRealm = (UserRealm) _currentRealm;
try {
user = userFacade.authenticate(_username, _password.trim());
userFacade.detach(user);
} catch (UnauthorizedException e) {
log.warning("Authentication failed: "+ e.getMessage());
throw new LoginException("UserLogin authentication failed!");
} catch (Exception e) {
throw new LoginException("UserLogin failed: "+ e.getMessage());
}
log.fine("Authentication successful for "+ user);
// get the groups the user is a member of
String[] grpList = userRealm.authorize(user);
if (grpList == null) {
throw new LoginException("User is not member of any groups");
}
// Add the logged in user to the subject's principals.
// This works, but unfortunately, I can't reach the user object
// afterwards again.
Set principals = _subject.getPrincipals();
principals.add(new UserPrincipalImpl(user));
this.commitUserAuthentication(grpList);
}
}
The trick is to separate the interfaces for the beans from the WAR. I bundle all interfaces and common entities in a separate OSGi module and deploy it with asadmin --type osgi
. As a result, the custom UserLoginModule can classload them.
这篇关于可以从自定义LoginModule访问远程EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!