1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.security.xwss.callback.jaas;
18
19 import java.security.Principal;
20 import java.util.Iterator;
21 import javax.security.auth.Subject;
22 import javax.security.auth.callback.CallbackHandler;
23 import javax.security.auth.login.LoginException;
24 import javax.security.auth.spi.LoginModule;
25 import javax.security.auth.x500.X500Principal;
26
27 public class CertificateLoginModule implements LoginModule {
28
29 private Subject subject;
30
31 private boolean loginSuccessful = false;
32
33 public boolean abort() {
34 return true;
35 }
36
37 public boolean commit() {
38 if (!loginSuccessful) {
39 subject.getPrincipals().clear();
40 subject.getPrivateCredentials().clear();
41 return false;
42 }
43 return true;
44 }
45
46 public void initialize(Subject subject,
47 CallbackHandler callbackHandler,
48 java.util.Map sharedState,
49 java.util.Map options) {
50 this.subject = subject;
51 }
52
53 public boolean login() throws LoginException {
54 if (subject == null) {
55 return false;
56 }
57
58 String name = getName(subject);
59
60 loginSuccessful = "CN=Arjen Poutsma,OU=Spring-WS,O=Interface21,L=Amsterdam,ST=Unknown,C=NL".equals(name);
61 return loginSuccessful;
62 }
63
64 public boolean logout() {
65 subject.getPrincipals().clear();
66 subject.getPrivateCredentials().clear();
67 return true;
68 }
69
70 private String getName(Subject subject) {
71 for (Iterator iterator = subject.getPrincipals().iterator(); iterator.hasNext();) {
72 Principal principal = (Principal) iterator.next();
73 if (principal instanceof X500Principal) {
74 return principal.getName();
75 }
76 }
77 return null;
78 }
79 }