View Javadoc

1   /* Copyright 2004, 2005, 2006 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  
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 1784 2007-02-24 21:00:24Z luke_t $
30   */
31  public final class SessionRegistryUtils {
32      //~ Constructors ===================================================================================================
33  
34      private SessionRegistryUtils() {
35      }
36  
37      //~ Methods ========================================================================================================
38  
39      public static Object obtainPrincipalFromAuthentication(Authentication auth) {
40          Assert.notNull(auth, "Authentication required");
41          Assert.notNull(auth.getPrincipal(), "Authentication.getPrincipal() required");
42  
43          if (auth.getPrincipal() instanceof UserDetails) {
44              return ((UserDetails) auth.getPrincipal()).getUsername();
45          } else {
46              return auth.getPrincipal();
47          }
48      }
49  
50      public static String obtainSessionIdFromAuthentication(Authentication auth) {
51          Assert.notNull(auth, "Authentication required");
52          Assert.notNull(auth.getDetails(), "Authentication.getDetails() required");
53          Assert.isInstanceOf(SessionIdentifierAware.class, auth.getDetails());
54  
55          String sessionId = ((SessionIdentifierAware) auth.getDetails()).getSessionId();
56          Assert.hasText(sessionId, "SessionIdentifierAware did not return a Session ID (" + auth.getDetails() + ")");
57  
58          return sessionId;
59      }
60  }