1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.security.authentication;
18
19
20 import org.springframework.beans.factory.InitializingBean;
21 import org.springframework.security.authentication.AuthenticationManager;
22 import org.springframework.security.core.Authentication;
23 import org.springframework.security.core.context.SecurityContextHolder;
24
25 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
26
27 /**
28 * Default implementation of <code>AuthenticationService</code> used for
29 * logging in and out. <br>
30 * <br>
31 * Simple usage example with a given BeanFactory: <br>
32 * <br>
33 * AuthenticationService as = (AuthenticationService)
34 * beanFactory.getBean("authenticationService"); <br>
35 * as.login(loginContext, callbackHandler); <br>
36 * <br>
37 * Both parameters are optional, if they are included in the applicationContext
38 * configuration. The bean may contain two properties :
39 * <ul>
40 * <li>defaultCallbackHandler, which is the default callback handler to be used
41 * for the callbacks of the PAM.
42 * <li>defaultLoginContext, which defines the default login context to be used
43 * for the login.
44 * </ul>
45 * <br>
46 *
47 * @svnLink $Revision: 4091 $;$Date: 2010-01-15 12:21:07 +0100 (Fr, 15. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/security/src/main/java/ch/elca/el4j/services/security/authentication/DefaultAuthenticationService.java $
48 *
49 * @author Raphael Boog (RBO)
50 * @author Andreas Pfenninger (APR)
51 * @author Christoph Schwitter (CSC)
52 */
53 public class DefaultAuthenticationService implements AuthenticationService,
54 InitializingBean {
55
56
57 /** The LoginService to be used for the login. */
58 private AuthenticationManager m_authenticationManager;
59
60 /**
61 * Return the authentication data that is stored for this thread. Used by
62 * the AuthenticationServiceContextPasser.
63 *
64 * @return The authentication data, may be null.
65 */
66 public Authentication getAuthenticationData() {
67
68 return SecurityContextHolder.getContext().getAuthentication();
69 }
70
71 /**
72 * Convenience method to set the authentication data.
73 *
74 * @param authenticationData
75 * The authentication data to be stored in the ThreadLocal.
76 */
77 private void setAuthenticationData(Authentication authenticationData) {
78 SecurityContextHolder.getContext().setAuthentication(authenticationData);
79 }
80
81 /**
82 * Sets the authenticationManager to be used for the authentication.
83 *
84 * @param am
85 * The AuthenticationManager to be used for the authentication.
86 */
87 public void setAuthenticationManager(AuthenticationManager am) {
88 m_authenticationManager = am;
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 public void afterPropertiesSet() throws Exception {
95 CoreNotificationHelper.notifyIfEssentialPropertyIsEmpty(
96 m_authenticationManager, "authenticationManager", this);
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 public void authenticate(Authentication auth) {
103 Authentication authResult = m_authenticationManager.authenticate(auth);
104 setAuthenticationData(authResult);
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 public String getUserName() {
111 if (getAuthenticationData() != null) {
112 Object obj = getAuthenticationData().getPrincipal();
113 if (obj instanceof String) {
114 return (String) obj;
115 }
116 }
117 return null;
118 }
119 }