1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.shiro.authc.pam; 20 21 import org.apache.shiro.authc.AuthenticationException; 22 import org.apache.shiro.authc.AuthenticationInfo; 23 import org.apache.shiro.authc.AuthenticationToken; 24 import org.apache.shiro.realm.Realm; 25 import org.apache.shiro.subject.PrincipalCollection; 26 27 import java.util.Collection; 28 29 /** 30 * {@link AuthenticationStrategy} implementation that only accepts the account data from 31 * the first successfully consulted Realm and ignores all subsequent realms. This is slightly 32 * different behavior than {@link AtLeastOneSuccessfulStrategy}, so please review both to see 33 * which one meets your needs better. 34 * 35 * @see AtLeastOneSuccessfulStrategy AtLeastOneSuccessfulAuthenticationStrategy 36 * @since 0.9 37 */ 38 public class FirstSuccessfulStrategy extends AbstractAuthenticationStrategy { 39 40 private boolean stopAfterFirstSuccess; 41 42 public void setStopAfterFirstSuccess (boolean stopAfterFirstSuccess ) { 43 44 this.stopAfterFirstSuccess = stopAfterFirstSuccess ; 45 } 46 47 public boolean getStopAfterFirstSuccess() { 48 return stopAfterFirstSuccess ; 49 } 50 51 /** 52 * Returns {@code null} immediately, relying on this class's {@link #merge merge} implementation to return 53 * only the first {@code info} object it encounters, ignoring all subsequent ones. 54 */ 55 public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException { 56 return null; 57 } 58 59 60 /** 61 * Throws ShortCircuitIterationException if stopAfterFirstSuccess is set and authentication is 62 * successful with a previously consulted realm. 63 * Returns the <code>aggregate</code> method argument, without modification 64 * otherwise. 65 */ 66 public AuthenticationInfo.html#AuthenticationInfo">AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException { 67 if (getStopAfterFirstSuccess() && aggregate != null && !isEmpty(aggregate.getPrincipals())) { 68 throw new ShortCircuitIterationException(); 69 } 70 return aggregate; 71 } 72 73 74 75 private static boolean isEmpty(PrincipalCollection pc) { 76 return pc == null || pc.isEmpty(); 77 } 78 79 /** 80 * Returns the specified {@code aggregate} instance if is non null and valid (that is, has principals and they are 81 * not empty) immediately, or, if it is null or not valid, the {@code info} argument is returned instead. 82 * <p/> 83 * This logic ensures that the first valid info encountered is the one retained and all subsequent ones are ignored, 84 * since this strategy mandates that only the info from the first successfully authenticated realm be used. 85 */ 86 protected AuthenticationInfo/../../org/apache/shiro/authc/AuthenticationInfo.html#AuthenticationInfo">AuthenticationInfo/../../org/apache/shiro/authc/AuthenticationInfo.html#AuthenticationInfo">AuthenticationInfo merge(AuthenticationInfo/../../org/apache/shiro/authc/AuthenticationInfo.html#AuthenticationInfo">AuthenticationInfo info, AuthenticationInfo aggregate) { 87 if (aggregate != null && !isEmpty(aggregate.getPrincipals())) { 88 return aggregate; 89 } 90 return info != null ? info : aggregate; 91 } 92 }