1 package org
.argeo
.security
.equinox
;
5 import javax
.security
.auth
.Subject
;
6 import javax
.security
.auth
.callback
.Callback
;
7 import javax
.security
.auth
.callback
.CallbackHandler
;
8 import javax
.security
.auth
.callback
.NameCallback
;
9 import javax
.security
.auth
.callback
.PasswordCallback
;
10 import javax
.security
.auth
.callback
.TextOutputCallback
;
11 import javax
.security
.auth
.login
.LoginException
;
13 import org
.springframework
.security
.Authentication
;
14 import org
.springframework
.security
.AuthenticationManager
;
15 import org
.springframework
.security
.BadCredentialsException
;
16 import org
.springframework
.security
.context
.SecurityContextHolder
;
17 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
18 import org
.springframework
.security
.providers
.jaas
.SecurityContextLoginModule
;
20 /** Login module which caches one subject per thread. */
21 public class SpringLoginModule
extends SecurityContextLoginModule
{
22 private AuthenticationManager authenticationManager
;
24 private CallbackHandler callbackHandler
;
26 public SpringLoginModule() {
30 @SuppressWarnings("rawtypes")
31 public void initialize(Subject subject
, CallbackHandler callbackHandler
,
32 Map sharedState
, Map options
) {
33 super.initialize(subject
, callbackHandler
, sharedState
, options
);
34 // this.subject.set(subject);
35 this.callbackHandler
= callbackHandler
;
38 public boolean login() throws LoginException
{
39 // thread already logged in
40 if (SecurityContextHolder
.getContext().getAuthentication() != null)
43 // if (getSubject().getPrincipals(Authentication.class).size() == 1) {
44 // registerAuthentication(getSubject()
45 // .getPrincipals(Authentication.class).iterator().next());
46 // return super.login();
47 // } else if (getSubject().getPrincipals(Authentication.class).size() >
49 // throw new LoginException(
50 // "Multiple Authentication principals not supported: "
51 // + getSubject().getPrincipals(Authentication.class));
53 // ask for username and password
54 Callback label
= new TextOutputCallback(TextOutputCallback
.INFORMATION
,
56 NameCallback nameCallback
= new NameCallback("User");
57 PasswordCallback passwordCallback
= new PasswordCallback("Password",
60 if (callbackHandler
== null) {
61 throw new LoginException("No call back handler available");
65 callbackHandler
.handle(new Callback
[] { label
, nameCallback
,
67 } catch (Exception e
) {
68 LoginException le
= new LoginException("Callback handling failed");
73 // Set user name and password
74 String username
= nameCallback
.getName();
76 if (passwordCallback
.getPassword() != null) {
77 password
= String
.valueOf(passwordCallback
.getPassword());
79 UsernamePasswordAuthenticationToken credentials
= new UsernamePasswordAuthenticationToken(
83 Authentication authentication
= authenticationManager
84 .authenticate(credentials
);
85 registerAuthentication(authentication
);
86 boolean res
= super.login();
87 // if (log.isDebugEnabled())
88 // log.debug("User " + username + " logged in");
90 } catch (BadCredentialsException bce
) {
92 } catch (Exception e
) {
93 LoginException loginException
= new LoginException(
95 loginException
.initCause(e
);
102 public boolean logout() throws LoginException
{
103 return super.logout();
107 * Register an {@link Authentication} in the security context.
109 * @param authentication
110 * has to implement {@link Authentication}.
112 protected void registerAuthentication(Object authentication
) {
113 SecurityContextHolder
.getContext().setAuthentication(
114 (Authentication
) authentication
);
117 public void setAuthenticationManager(
118 AuthenticationManager authenticationManager
) {
119 this.authenticationManager
= authenticationManager
;
122 // protected Subject getSubject() {
123 // return subject.get();