1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.runas;
17
18 import org.acegisecurity.AcegiMessageSource;
19 import org.acegisecurity.Authentication;
20 import org.acegisecurity.AuthenticationException;
21 import org.acegisecurity.BadCredentialsException;
22
23 import org.acegisecurity.providers.AuthenticationProvider;
24
25 import org.springframework.beans.factory.InitializingBean;
26
27 import org.springframework.context.MessageSource;
28 import org.springframework.context.MessageSourceAware;
29 import org.springframework.context.support.MessageSourceAccessor;
30
31 import org.springframework.util.Assert;
32
33
34
35
36
37
38
39
40
41 public class RunAsImplAuthenticationProvider implements InitializingBean, AuthenticationProvider, MessageSourceAware {
42
43
44 protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
45 private String key;
46
47
48
49 public void afterPropertiesSet() throws Exception {
50 Assert.notNull(key, "A Key is required and should match that configured for the RunAsManagerImpl");
51 }
52
53 public Authentication authenticate(Authentication authentication)
54 throws AuthenticationException {
55 RunAsUserToken token = (RunAsUserToken) authentication;
56
57 if (token.getKeyHash() == key.hashCode()) {
58 return authentication;
59 } else {
60 throw new BadCredentialsException(messages.getMessage("RunAsImplAuthenticationProvider.incorrectKey",
61 "The presented RunAsUserToken does not contain the expected key"));
62 }
63 }
64
65 public String getKey() {
66 return key;
67 }
68
69 public void setKey(String key) {
70 this.key = key;
71 }
72
73 public void setMessageSource(MessageSource messageSource) {
74 this.messages = new MessageSourceAccessor(messageSource);
75 }
76
77 public boolean supports(Class authentication) {
78 if (RunAsUserToken.class.isAssignableFrom(authentication)) {
79 return true;
80 } else {
81 return false;
82 }
83 }
84 }