您需要指定您自己的AuthenticationStrategy您的ModularRealmAuthenticator。 的ModularRealmAuthenticator使用AtLeastOneSuccessfulStrategy默认和AtLeastOneSuccessfulStrategy忽略异常并不断尝试登录使用所有可用的领域的用户。
我们不得不对tynamo项目类似的场景和要解决这个问题,我已经实现了我自己AuthenticationStrategy,称为FirstExceptionStrategy,与多个领域的工作,并抛出它获得的第一个例外。这种方法工作正常,只要只有一个领域每令牌类型。
的实现是相当简单:
/**
* {@link org.apache.shiro.authc.pam.AuthenticationStrategy} implementation that throws the first exception it gets
* and ignores all subsequent realms. If there is no exceptions it works as the {@link FirstSuccessfulStrategy}
*
* WARN: This approach works fine as long as there is ONLY ONE Realm per Token type.
*
*/
public class FirstExceptionStrategy extends FirstSuccessfulStrategy {
@Override
public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
if ((t != null) && (t instanceof AuthenticationException)) throw (AuthenticationException) t;
return super.afterAttempt(realm, token, singleRealmInfo, aggregateInfo, t);
}
}
我再说一遍,这只有作品,如果有每个令牌类型ONLY ONE境界。
我的特定方案欲了解更多信息请看这里:http://jira.codehaus.org/browse/TYNAMO-154
不幸的是,我们正在使用''UsernamePasswordToken''的两个领域,因此这不会解决我们的问题......但感谢输入! – xeraa
没有不同的_Tokens_,这个策略真的很难知道它是否应该抛出异常,或者应该继续查看领域列表。看看_ModularRealmAuthenticator.doMultiRealmAuthentication_。也许你可以在_beforeAllAttempts_中创建自己的聚合,将聚合中的异常保存在_afterAttempt_中,然后将异常保存在_afterAllAttempts_中。 – ascandroli
感谢您的指点,但我认为这不值得。我们目前对通用例外情况良好,并会在我们需要额外信息的情况下尝试您的建议。 – xeraa