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
.AuthenticationException
;
15 import org
.springframework
.security
.AuthenticationManager
;
16 import org
.springframework
.security
.BadCredentialsException
;
17 import org
.springframework
.security
.context
.SecurityContextHolder
;
18 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
19 import org
.springframework
.security
.providers
.jaas
.SecurityContextLoginModule
;
21 public class SpringLoginModule
extends SecurityContextLoginModule
{
22 private AuthenticationManager authenticationManager
;
23 private Subject subject
;
25 private CallbackHandler callbackHandler
;
27 public SpringLoginModule() {
31 @SuppressWarnings("rawtypes")
32 public void initialize(Subject subject
, CallbackHandler callbackHandler
,
33 Map sharedState
, Map options
) {
34 super.initialize(subject
, callbackHandler
, sharedState
, options
);
35 this.subject
= subject
;
36 this.callbackHandler
= callbackHandler
;
39 public boolean login() throws LoginException
{
40 // thread already logged in
41 if (SecurityContextHolder
.getContext().getAuthentication() != null)
44 if (subject
.getPrincipals(Authentication
.class).size() == 1) {
45 registerAuthentication(subject
.getPrincipals(Authentication
.class)
48 } else if (subject
.getPrincipals(Authentication
.class).size() > 1) {
49 throw new LoginException(
50 "Multiple Authentication principals not supported: "
51 + subject
.getPrincipals(Authentication
.class));
53 // ask for username and password
54 Callback label
= new TextOutputCallback(
55 TextOutputCallback
.INFORMATION
, "Required login");
56 NameCallback nameCallback
= new NameCallback("User");
57 PasswordCallback passwordCallback
= new PasswordCallback(
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(
69 "Callback handling failed");
74 // Set user name and password
75 String username
= nameCallback
.getName();
77 if (passwordCallback
.getPassword() != null) {
78 password
= String
.valueOf(passwordCallback
.getPassword());
80 UsernamePasswordAuthenticationToken credentials
= new UsernamePasswordAuthenticationToken(
84 Authentication authentication
= authenticationManager
85 .authenticate(credentials
);
86 registerAuthentication(authentication
);
88 } catch (Exception e
) {
89 LoginException loginException
= new LoginException(
91 loginException
.initCause(e
);
98 * Register an {@link Authentication} in the security context.
100 * @param authentication
101 * has to implement {@link Authentication}.
103 protected void registerAuthentication(Object authentication
) {
104 SecurityContextHolder
.getContext().setAuthentication(
105 (Authentication
) authentication
);
108 public void setAuthenticationManager(
109 AuthenticationManager authenticationManager
) {
110 this.authenticationManager
= authenticationManager
;