shiro& guice - 如何注入AuthenticationStrategy?

时间:2012-07-26 07:26:42

标签: guice shiro

我正在使用Shiro和guice。 Shiro有两个活跃的领域。

默认的shiro AuthenticationStrategy是“AtLeastOneSuccessfulStrategy”。这个策略的基本思路很好,但问题是它忽略了reams异常。这意味着如果Realm1抛出IncorrectCredentialsException,则无法知道它,因为它被AuthenticationException包装,并且消息表明这些域不支持令牌。

如何使用FirstSuccessfulStrategy替换策略?

目前这就是我在ShiroWebModule中所拥有的:

   @Override
    protected void configureShiroWeb() {
       Multibinder<Realm> multibinder = Multibinder.newSetBinder(binder(), Realm.class);
        multibinder.addBinding().to(RealmA.class);
        multibinder.addBinding().to(RealmB.class);
        bind( HashedCredentialsMatcher.class );
        bind( CredentialsMatcher.class ).to( HashedCredentialsMatcher.class );
        bindConstant().annotatedWith( Names.named( "shiro.hashAlgorithmName" ) ).to( Md5Hash.ALGORITHM_NAME );
        addFilterChain( "/login.jsp", AUTHC_REST );
    }

    @Override
    protected void bindSessionManager( AnnotatedBindingBuilder<SessionManager> bind ) {
        bind.to( ServletContainerSessionManager.class );
    }

1 个答案:

答案 0 :(得分:2)

通过添加

解决
   bind(Authenticator.class).toInstance(new ModularRealmAuthenticator());
   bind(AuthenticationStrategy.class).to(FirstSuccessfulStrategy.class);

到configureShiroWeb()方法。