1 package org
.argeo
.security
.equinox
;
4 import java
.util
.concurrent
.Executor
;
6 import javax
.security
.auth
.Subject
;
7 import javax
.security
.auth
.callback
.Callback
;
8 import javax
.security
.auth
.callback
.CallbackHandler
;
9 import javax
.security
.auth
.callback
.NameCallback
;
10 import javax
.security
.auth
.callback
.PasswordCallback
;
11 import javax
.security
.auth
.callback
.TextOutputCallback
;
12 import javax
.security
.auth
.login
.LoginException
;
14 import org
.argeo
.security
.SiteAuthenticationToken
;
15 import org
.springframework
.security
.Authentication
;
16 import org
.springframework
.security
.AuthenticationManager
;
17 import org
.springframework
.security
.BadCredentialsException
;
18 import org
.springframework
.security
.context
.SecurityContextHolder
;
19 import org
.springframework
.security
.providers
.jaas
.SecurityContextLoginModule
;
21 /** Login module which caches one subject per thread. */
22 public class SpringLoginModule
extends SecurityContextLoginModule
{
23 private AuthenticationManager authenticationManager
;
24 private Executor systemExecutor
;
26 private CallbackHandler callbackHandler
;
28 public SpringLoginModule() {
32 @SuppressWarnings("rawtypes")
33 public void initialize(Subject subject
, CallbackHandler callbackHandler
,
34 Map sharedState
, Map options
) {
35 super.initialize(subject
, callbackHandler
, sharedState
, options
);
36 // this.subject.set(subject);
37 this.callbackHandler
= callbackHandler
;
40 public boolean login() throws LoginException
{
41 // thread already logged in
42 if (SecurityContextHolder
.getContext().getAuthentication() != null)
45 // if (getSubject().getPrincipals(Authentication.class).size() == 1) {
46 // registerAuthentication(getSubject()
47 // .getPrincipals(Authentication.class).iterator().next());
48 // return super.login();
49 // } else if (getSubject().getPrincipals(Authentication.class).size() >
51 // throw new LoginException(
52 // "Multiple Authentication principals not supported: "
53 // + getSubject().getPrincipals(Authentication.class));
55 // ask for username and password
56 Callback label
= new TextOutputCallback(TextOutputCallback
.INFORMATION
,
58 NameCallback nameCallback
= new NameCallback("User");
59 PasswordCallback passwordCallback
= new PasswordCallback("Password",
61 NameCallback urlCallback
= new NameCallback("Site URL");
63 if (callbackHandler
== null) {
64 throw new LoginException("No call back handler available");
68 callbackHandler
.handle(new Callback
[] { label
, nameCallback
,
69 passwordCallback
, urlCallback
});
70 } catch (Exception e
) {
71 LoginException le
= new LoginException("Callback handling failed");
76 // Set user name and password
77 String username
= nameCallback
.getName();
79 if (passwordCallback
.getPassword() != null) {
80 password
= String
.valueOf(passwordCallback
.getPassword());
82 String url
= urlCallback
.getName();
83 // TODO: set it via system properties
84 String workspace
= null;
86 // UsernamePasswordAuthenticationToken credentials = new
87 // UsernamePasswordAuthenticationToken(
88 // username, password);
89 SiteAuthenticationToken credentials
= new SiteAuthenticationToken(
90 username
, password
, url
, workspace
);
94 Authentication authentication
= authenticationManager
95 .authenticate(credentials
);
96 registerAuthentication(authentication
);
97 boolean res
= super.login();
98 // if (log.isDebugEnabled())
99 // log.debug("User " + username + " logged in");
101 } catch (BadCredentialsException bce
) {
103 } catch (Exception e
) {
104 LoginException loginException
= new LoginException(
106 loginException
.initCause(e
);
107 throw loginException
;
113 public boolean logout() throws LoginException
{
114 return super.logout();
118 * Register an {@link Authentication} in the security context.
120 * @param authentication
121 * has to implement {@link Authentication}.
123 protected void registerAuthentication(Object authentication
) {
124 SecurityContextHolder
.getContext().setAuthentication(
125 (Authentication
) authentication
);
128 public void setAuthenticationManager(
129 AuthenticationManager authenticationManager
) {
130 this.authenticationManager
= authenticationManager
;
133 public void setSystemExecutor(Executor systemExecutor
) {
134 this.systemExecutor
= systemExecutor
;
137 // protected Subject getSubject() {
138 // return subject.get();