【问题标题】:shiro & guice - How to inject AuthenticationStrategy?shiro & guice - 如何注入 AuthenticationStrategy?
【发布时间】:2012-07-24 18:00:27
【问题描述】:

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

默认的 shiro AuthenticationStrategy 是“AtLeastOneSuccessfulStrategy”。这种策略的基本思想很好,但问题是它忽略了大量异常。这意味着如果 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 );
    }

【问题讨论】:

    标签: guice shiro


    【解决方案1】:

    添加解决

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

    到 configureShiroWeb() 方法。

    【讨论】:

    • 这是如何工作的?在bind(Authenticator.class).toInstance(new ModularRealmAuthenticator()); Shiro 最终调用ModularRealmAuthenticator 的默认构造函数,将策略设置为AtLeastOneSuccessfulStrategy
    猜你喜欢
    • 2013-03-20
    • 2011-06-18
    • 2014-10-29
    • 2013-12-11
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多