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