This content has been marked as final.
Show 1 reply
-
1. Re: JBoss 7 CDI in a custom login module..
dastraub Mar 13, 2012 4:38 PM (in response to andersbohn)I've implemented something similar (today ) :
- I created a JBoss Extension that dynamically installs/register login modules (I found them via a custom annotation). But you can do the same stuff also in a CDI-Extension. For creating and registering login modules, see source code of jboss-as-security.
So my login module looks like this :
@LoginDomain(name = "test1", principal = TestPrincipal.class) @LoginModuleDescription(flag = FlagType.REQUIRED) public class Test1LoginModule implements LoginModule { @Inject BeanForLoginModule beanForLoginModule; @Override public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) { ...
- for the Loginmodule-Class itself I use the delegate pattern, that means instead of registering the login module I register a wrapper.
- in the "initalize" method of the wrapper I create the original login module bean progammatically, and get a full intialized instance from cdi :
public class CdiDelegatingLoginModule implements LoginModule { private LoginModule delegate; public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) { String lmClassName = (String) options.get("code"); delegate = createCdiInstance(lmClassName); delegate.initialize(subject, callbackHandler, sharedState, options); } private LoginModule createCdiInstance(String className) { try { Class<?> loginModuleClass = Thread.currentThread().getContextClassLoader().loadClass(className); BeanManager beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager"); Bean<LoginModule> loginModuleBean = (Bean<LoginModule>) beanManager.getBeans(loginModuleClass).iterator().next(); return (LoginModule) beanManager.getReference(loginModuleBean, loginModuleClass, beanManager.createCreationalContext(loginModuleBean)); } catch (Exception x) { throw new IllegalStateException(x); } } ...
It's currently in draft status ...
But your solution looks good for everything what's need injection, thanks for the hint !