|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.providers.anonymous; |
|
17 |
| |
|
18 |
| import org.acegisecurity.AcegiMessageSource; |
|
19 |
| import org.acegisecurity.Authentication; |
|
20 |
| import org.acegisecurity.AuthenticationException; |
|
21 |
| import org.acegisecurity.BadCredentialsException; |
|
22 |
| import org.acegisecurity.providers.AuthenticationProvider; |
|
23 |
| import org.apache.commons.logging.Log; |
|
24 |
| import org.apache.commons.logging.LogFactory; |
|
25 |
| import org.springframework.beans.factory.InitializingBean; |
|
26 |
| import org.springframework.context.MessageSource; |
|
27 |
| import org.springframework.context.MessageSourceAware; |
|
28 |
| import org.springframework.context.support.MessageSourceAccessor; |
|
29 |
| import org.springframework.util.Assert; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| public class AnonymousAuthenticationProvider implements AuthenticationProvider, |
|
43 |
| InitializingBean, MessageSourceAware { |
|
44 |
| |
|
45 |
| |
|
46 |
| private static final Log logger = LogFactory.getLog(AnonymousAuthenticationProvider.class); |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
|
51 |
| private String key; |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
2
| public void afterPropertiesSet() throws Exception {
|
|
56 |
2
| Assert.hasLength(key, "A Key is required");
|
|
57 |
1
| Assert.notNull(this.messages, "A message source must be set");
|
|
58 |
| } |
|
59 |
| |
|
60 |
3
| public Authentication authenticate(Authentication authentication)
|
|
61 |
| throws AuthenticationException { |
|
62 |
3
| if (!supports(authentication.getClass())) {
|
|
63 |
1
| return null;
|
|
64 |
| } |
|
65 |
| |
|
66 |
2
| if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication)
|
|
67 |
| .getKeyHash()) { |
|
68 |
1
| throw new BadCredentialsException(messages.getMessage(
|
|
69 |
| "AnonymousAuthenticationProvider.incorrectKey", |
|
70 |
| "The presented AnonymousAuthenticationToken does not contain the expected key")); |
|
71 |
| } |
|
72 |
| |
|
73 |
1
| return authentication;
|
|
74 |
| } |
|
75 |
| |
|
76 |
1
| public String getKey() {
|
|
77 |
1
| return key;
|
|
78 |
| } |
|
79 |
| |
|
80 |
4
| public void setKey(String key) {
|
|
81 |
4
| this.key = key;
|
|
82 |
| } |
|
83 |
| |
|
84 |
0
| public void setMessageSource(MessageSource messageSource) {
|
|
85 |
0
| this.messages = new MessageSourceAccessor(messageSource);
|
|
86 |
| } |
|
87 |
| |
|
88 |
6
| public boolean supports(Class authentication) {
|
|
89 |
6
| return (AnonymousAuthenticationToken.class.isAssignableFrom(authentication));
|
|
90 |
| } |
|
91 |
| } |