1 package org
.argeo
.security
.equinox
;
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
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.security
.SiteAuthenticationToken
;
17 import org
.springframework
.security
.Authentication
;
18 import org
.springframework
.security
.AuthenticationManager
;
19 import org
.springframework
.security
.BadCredentialsException
;
20 import org
.springframework
.security
.context
.SecurityContextHolder
;
21 import org
.springframework
.security
.providers
.jaas
.SecurityContextLoginModule
;
23 /** Login module which caches one subject per thread. */
24 public class SpringLoginModule
extends SecurityContextLoginModule
{
25 private final static Log log
= LogFactory
.getLog(SpringLoginModule
.class);
27 private AuthenticationManager authenticationManager
;
29 private CallbackHandler callbackHandler
;
31 private Subject subject
;
33 public SpringLoginModule() {
37 @SuppressWarnings("rawtypes")
38 public void initialize(Subject subject
, CallbackHandler callbackHandler
,
39 Map sharedState
, Map options
) {
40 super.initialize(subject
, callbackHandler
, sharedState
, options
);
41 this.callbackHandler
= callbackHandler
;
42 this.subject
= subject
;
45 public boolean login() throws LoginException
{
46 // try to retrieve Authentication from Subject
47 Set
<Authentication
> auths
= subject
.getPrincipals(Authentication
.class);
49 SecurityContextHolder
.getContext().setAuthentication(
50 auths
.iterator().next());
52 // thread already logged in
53 if (SecurityContextHolder
.getContext().getAuthentication() != null)
56 // ask for username and password
57 Callback label
= new TextOutputCallback(TextOutputCallback
.INFORMATION
,
59 NameCallback nameCallback
= new NameCallback("User");
60 PasswordCallback passwordCallback
= new PasswordCallback("Password",
62 NameCallback urlCallback
= new NameCallback("Site URL");
64 if (callbackHandler
== null) {
65 throw new LoginException("No call back handler available");
69 callbackHandler
.handle(new Callback
[] { label
, nameCallback
,
70 passwordCallback
, urlCallback
});
71 } catch (Exception e
) {
72 LoginException le
= new LoginException("Callback handling failed");
77 // Set user name and password
78 String username
= nameCallback
.getName();
80 if (passwordCallback
.getPassword() != null) {
81 password
= String
.valueOf(passwordCallback
.getPassword());
83 String url
= urlCallback
.getName();
84 // TODO: set it via system properties
85 String workspace
= null;
87 // UsernamePasswordAuthenticationToken credentials = new
88 // UsernamePasswordAuthenticationToken(
89 // username, password);
90 SiteAuthenticationToken credentials
= new SiteAuthenticationToken(
91 username
, password
, url
, workspace
);
94 Authentication authentication
= authenticationManager
95 .authenticate(credentials
);
96 registerAuthentication(authentication
);
97 boolean res
= super.login();
99 } catch (BadCredentialsException bce
) {
101 } catch (Exception e
) {
102 LoginException loginException
= new LoginException(
104 loginException
.initCause(e
);
105 throw loginException
;
111 public boolean logout() throws LoginException
{
112 // if (log.isDebugEnabled())
113 // log.debug("logout subject=" + subject);
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
;