|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.adapters; |
|
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 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public class AuthByAdapterProvider implements InitializingBean, |
|
50 |
| AuthenticationProvider, MessageSourceAware { |
|
51 |
| |
|
52 |
| |
|
53 |
| protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
|
54 |
| private String key; |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
2
| public void afterPropertiesSet() throws Exception {
|
|
59 |
2
| Assert.notNull(key,
|
|
60 |
| "A Key is required and should match that configured for the adapters"); |
|
61 |
1
| Assert.notNull(messages, "A message source must be set");
|
|
62 |
| } |
|
63 |
| |
|
64 |
3
| public Authentication authenticate(Authentication authentication)
|
|
65 |
| throws AuthenticationException { |
|
66 |
3
| AuthByAdapter token = (AuthByAdapter) authentication;
|
|
67 |
| |
|
68 |
2
| if (token.getKeyHash() == key.hashCode()) {
|
|
69 |
1
| return authentication;
|
|
70 |
| } else { |
|
71 |
1
| throw new BadCredentialsException(messages.getMessage(
|
|
72 |
| "AuthByAdapterProvider.incorrectKey", |
|
73 |
| "The presented AuthByAdapter implementation does not contain the expected key")); |
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
1
| public String getKey() {
|
|
78 |
1
| return key;
|
|
79 |
| } |
|
80 |
| |
|
81 |
4
| public void setKey(String key) {
|
|
82 |
4
| this.key = key;
|
|
83 |
| } |
|
84 |
| |
|
85 |
0
| public void setMessageSource(MessageSource messageSource) {
|
|
86 |
0
| this.messages = new MessageSourceAccessor(messageSource);
|
|
87 |
| } |
|
88 |
| |
|
89 |
2
| public boolean supports(Class authentication) {
|
|
90 |
2
| if (AuthByAdapter.class.isAssignableFrom(authentication)) {
|
|
91 |
1
| return true;
|
|
92 |
| } else { |
|
93 |
1
| return false;
|
|
94 |
| } |
|
95 |
| } |
|
96 |
| } |