You can't use the built-in EnvironmentLoaderListener because it doesn't use
injection to build the Environment. However, you should be able to extend
it and override #createEnvironment(ServletContext) to use an injected
CdiWebEnvironment instance:

public class CdiEnvironmentLoaderListener extends EnvironmentLoaderListener
{
  @Inject
  private CdiWebEnvironment environment;

  protected WebEnvironment createEnvironment(ServletContext sc) {
// Do your initialization here
return environment;
}
}

I haven't actually tried this because I ended up writing my own
version of EnvironmentLoaderListener,
but this should work.


On Wed, May 30, 2012 at 7:55 AM, melvyndekort - [email protected] wrote:

> I don't seem to get it all to work.
> I have my own WebEnvironment and try to inject the JpaRealm into it.
> The problem seems to be that the JpaRealm is not instantiated and is not
> injected into the CdiWebEnvironment.
>
> This is basically what I currently have:
>
> ### WEB.XML SNIPPET ###
>
>  <context-param>
>    <param-name>shiroEnvironmentClass</param-name>
>    <param-value>****.shiro.CdiWebEnvironment</param-value>
>  </context-param>
>
>  <listener>
>
>
> <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
>  </listener>
>
>
> ### SHIRO.INI ###
>
> [main]
> authc = org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter
> authc.loginUrl = /login.jsf
>
> [users]
>
> [roles]
>
> [urls]
> /login.jsf = authc
> /secure/** = authc
>
>
> ### CUSTOM WEB ENVIRONMENT ###
>
> @ApplicationScoped
> public class CdiWebEnvironment extends IniWebEnvironment
> {
>  private static Logger LOG = Logger.getLogger(CdiWebEnvironment.class);
>
>  @Inject
>  private JpaRealm jpaRealm;
>
>  @Inject
>  public CdiWebEnvironment() {
>    LOG.debug("CdiWebEnvironment instantiated!");
>  }
>
>  @Override
>  protected void configure() {
>    super.configure();
> /* WHILE DEBUGGING jpaRealm IS NULL AT THIS POINT! */
>
>
>    RealmSecurityManager securityManager = (RealmSecurityManager)
> getSecurityManager();
>    securityManager.setRealm(jpaRealm);
>  }
> }
>
>
> ### JPA REALM CLASS ###
>
> @ApplicationScoped
> public class JpaRealm extends AuthorizingRealm
> {
>  private static Logger LOG = Logger.getLogger(JpaRealm.class);
>
>  @PersistenceContext
>  private EntityManager entityManager;
>
>  public JpaRealm() {
> /* THIS LINE IS NEVER REACHED, SO NO INSTANCE IS CREATED! */
>
>    setName("JpaRealm");
>
>    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
>    matcher.setHashAlgorithmName("SHA-256");
>    setCredentialsMatcher(matcher);
>
>    LOG.debug("JpaRealm instantiated!");
>  }
>
>  @Override
>  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
> authenticationToken) { ... }
>
>  @Override
>  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection
> principals) { ... }
> }
>
>
> atomicknight wrote
> >
> > The Environment is loaded by a servlet context listener, which is managed
> > by the container and consequently supports injection. Then it's just a
> > matter of injecting your JpaRealm, building a WebSecurityManager from it,
> > and associating the SecurityManager with the Environment.
> >
>
> --
> View this message in context:
> http://shiro-user.582556.n2.nabble.com/Shiro-in-CDI-JPA2-JSF2-project-tp7577437p7577442.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>
>

Reply via email to