View Javadoc

    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.concurrent;
    17
    18  import org.acegisecurity.Authentication;
    19  import org.acegisecurity.ui.WebAuthenticationDetails;
    20  import org.acegisecurity.userdetails.UserDetails;
    21
    22  import org.springframework.util.Assert;
    23
    24
    25  /***
    26   * Utility methods to assist with concurrent session management.
    27   *
    28   * @author Ben Alex
    29   * @version $Id: SessionRegistryUtils.java,v 1.3 2005/11/29 13:10:09 benalex Exp $
    30   */
    31  public class SessionRegistryUtils {
    32      //~ Methods ================================================================
    33
    34      public static Object obtainPrincipalFromAuthentication(Authentication auth) {
    35          Assert.notNull(auth, "Authentication required");
    36          Assert.notNull(auth.getPrincipal(),
    37              "Authentication.getPrincipal() required");
    38
    39          if (auth.getPrincipal() instanceof UserDetails) {
    40              return ((UserDetails) auth.getPrincipal()).getUsername();
    41          } else {
    42              return auth.getPrincipal();
    43          }
    44      }
    45
    46      public static String obtainSessionIdFromAuthentication(Authentication auth) {
    47          Assert.notNull(auth, "Authentication required");
    48          Assert.notNull(auth.getDetails(), "Authentication.getDetails() required");
    49          Assert.isInstanceOf(WebAuthenticationDetails.class, auth.getDetails());
    50
    51          String sessionId = ((WebAuthenticationDetails) auth.getDetails())
    52              .getSessionId();
    53          Assert.hasText(sessionId, "WebAuthenticationDetails missing SessionId");
    54
    55          return sessionId;
    56      }
    57  }