4

2 つのレルムを使用しています (1 つはハッシュ化されたパスワード用、もう 1 つは生成されたプレーンテキスト キー用) - これは期待どおりに機能しています。

単一のレルムを使用すると、DisabledAccountExceptionレルムで例外をスローprotected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken authToken)し、アプリケーションでそのような例外を明示的にキャッチできます。

2 つのレルムができたので、すべての例外は Shiro 内部でキャッチされます。そのため、1 つのレルムが失敗した場合、2 番目のレルムも試すことができます。ただし、この種のリダイレクトはAuthenticationExceptions、アプリケーションにジェネリックをスローするだけです。

より具体的な例外を設定できるように、複数のレルムを使用する回避策はありますか (アカウントがロックされているかどうか、資格情報が単に間違っているかどうかを知るには...)?

4

1 に答える 1

7

ModularRealmAuthenticatorで独自のAuthenticationStrategyを指定する必要があります。ModularRealmAuthenticatorはデフォルトでAtLeastOneSuccessfulStrategyを使用し、AtLeastOneSuccessfulStrategyは例外を無視し、使用可能なすべてのレルムを使用してユーザーにログインしようとし続けます。

tynamoプロジェクトでも同様のシナリオがあり、この問題を回避するために、 FirstExceptionStrategyと呼ばれる独自のAuthenticationStrategyを実装しました。これは、複数のレルムで機能し、最初の例外をスローします。このアプローチは、トークンタイプごとにレルムが1つしかない限り、正常に機能します。

実装はかなり単純です:

/**
 * {@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);
    }

}

繰り返しますが、これはトークンタイプごとにレルム1つしかない場合にのみ機能します。

私の特定のシナリオの詳細については、ここを参照してください:http: //jira.codehaus.org/browse/TYNAMO-154

于 2012-11-19T12:15:03.743 に答える