01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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: * Utility methods to assist with concurrent session management.
26: *
27: * @author Ben Alex
28: * @version $Id: SessionRegistryUtils.java 1784 2007-02-24 21:00:24Z luke_t $
29: */
30: public final class SessionRegistryUtils {
31: //~ Constructors ===================================================================================================
32:
33: private SessionRegistryUtils() {
34: }
35:
36: //~ Methods ========================================================================================================
37:
38: public static Object obtainPrincipalFromAuthentication(
39: Authentication auth) {
40: Assert.notNull(auth, "Authentication required");
41: Assert.notNull(auth.getPrincipal(),
42: "Authentication.getPrincipal() required");
43:
44: if (auth.getPrincipal() instanceof UserDetails) {
45: return ((UserDetails) auth.getPrincipal()).getUsername();
46: } else {
47: return auth.getPrincipal();
48: }
49: }
50:
51: public static String obtainSessionIdFromAuthentication(
52: Authentication auth) {
53: Assert.notNull(auth, "Authentication required");
54: Assert.notNull(auth.getDetails(),
55: "Authentication.getDetails() required");
56: Assert.isInstanceOf(SessionIdentifierAware.class, auth
57: .getDetails());
58:
59: String sessionId = ((SessionIdentifierAware) auth.getDetails())
60: .getSessionId();
61: Assert.hasText(sessionId,
62: "SessionIdentifierAware did not return a Session ID ("
63: + auth.getDetails() + ")");
64:
65: return sessionId;
66: }
67: }
|