2010-05-05 3 views
2

커스텀 영역과 loginModule을 작성하는 방법에 대한 좋은 힌트를 발견했습니다. 사용자 정의 loginModule 내에서 원격 EJB에 액세스 할 수 있는지 궁금합니다.사용자 정의 LoginModule에서 원격 EJB에 액세스 할 수 있습니까?

필자의 경우, JPA를 통해 사용자 엔터티에 대한 액세스를 제공하는 원격 EJB가 있습니다. @EJB 주석을 통해 사용할 수 있습니까?

답변

2

좋아, 대답은 나 자신을 발견 : 잘 작동! InitialContext를 통해 원격 SLSB에 대한 참조를 얻을 수 있습니다.

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); 
    } 

} 

트릭은 WAR에서 콩에 대한 인터페이스를 분리하는 것입니다 :

여기에 코드입니다. 모든 인터페이스와 공통 엔티티를 별도의 OSGi 모듈에 묶어 asadmin --type osgi으로 배포합니다. 결과적으로 사용자 정의 UserLoginModule은 클래스를로드 할 수 있습니다.

+0

예. 나는 당신의 코드를보고 싶다. 나는이 문제를 해결할 필요가있다. 감사. –

+0

코드가 추가되었습니다. 지금까지는 예상대로 작동했지만 더 이상이 접근법을 사용하지 않습니다. 그러나 클러스터 된 환경에서 배포가 너무 복잡하다는 사실을 발견했습니다. 대신, 지금은 인증이 필요한 REST 리소스 앞에 간단한 'ResourceFilter'를 사용하고 있습니다. 동일한 WAR에서 훨씬 더 간단합니다. – Hank

관련 문제