View Javadoc
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      /**
41       * Returns {@code null} immediately, relying on this class's {@link #merge merge} implementation to return
42       * only the first {@code info} object it encounters, ignoring all subsequent ones.
43       */
44      public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException {
45          return null;
46      }
47  
48      private static boolean isEmpty(PrincipalCollection pc) {
49          return pc == null || pc.isEmpty();
50      }
51  
52      /**
53       * Returns the specified {@code aggregate} instance if is non null and valid (that is, has principals and they are
54       * not empty) immediately, or, if it is null or not valid, the {@code info} argument is returned instead.
55       * <p/>
56       * This logic ensures that the first valid info encountered is the one retained and all subsequent ones are ignored,
57       * since this strategy mandates that only the info from the first successfully authenticated realm be used.
58       */
59      protected AuthenticationInfo merge(AuthenticationInfo info, AuthenticationInfo aggregate) {
60          if (aggregate != null && !isEmpty(aggregate.getPrincipals())) {
61              return aggregate;
62          }
63          return info != null ? info : aggregate;
64      }
65  }