|
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.Authentication; |
|
19 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
20 |
| import org.acegisecurity.userdetails.memory.UserAttribute; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| |
|
25 |
| import org.springframework.beans.factory.InitializingBean; |
|
26 |
| |
|
27 |
| import org.springframework.util.Assert; |
|
28 |
| |
|
29 |
| import java.io.IOException; |
|
30 |
| |
|
31 |
| import javax.servlet.Filter; |
|
32 |
| import javax.servlet.FilterChain; |
|
33 |
| import javax.servlet.FilterConfig; |
|
34 |
| import javax.servlet.ServletException; |
|
35 |
| import javax.servlet.ServletRequest; |
|
36 |
| import javax.servlet.ServletResponse; |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class AnonymousProcessingFilter implements Filter, InitializingBean { |
|
53 |
| |
|
54 |
| |
|
55 |
| private static final Log logger = LogFactory.getLog(AnonymousProcessingFilter.class); |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| private String key; |
|
60 |
| private UserAttribute userAttribute; |
|
61 |
| private boolean removeAfterRequest = true; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
4
| public void setKey(String key) {
|
|
66 |
4
| this.key = key;
|
|
67 |
| } |
|
68 |
| |
|
69 |
1
| public String getKey() {
|
|
70 |
1
| return key;
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
3
| public void setRemoveAfterRequest(boolean removeAfterRequest) {
|
|
91 |
3
| this.removeAfterRequest = removeAfterRequest;
|
|
92 |
| } |
|
93 |
| |
|
94 |
2
| public boolean isRemoveAfterRequest() {
|
|
95 |
2
| return removeAfterRequest;
|
|
96 |
| } |
|
97 |
| |
|
98 |
4
| public void setUserAttribute(UserAttribute userAttributeDefinition) {
|
|
99 |
4
| this.userAttribute = userAttributeDefinition;
|
|
100 |
| } |
|
101 |
| |
|
102 |
1
| public UserAttribute getUserAttribute() {
|
|
103 |
1
| return userAttribute;
|
|
104 |
| } |
|
105 |
| |
|
106 |
5
| public void afterPropertiesSet() throws Exception {
|
|
107 |
5
| Assert.notNull(userAttribute);
|
|
108 |
4
| Assert.hasLength(key);
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
3
| public void destroy() {}
|
|
115 |
| |
|
116 |
3
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
117 |
| FilterChain chain) throws IOException, ServletException { |
|
118 |
3
| boolean addedToken = false;
|
|
119 |
| |
|
120 |
3
| if (applyAnonymousForThisRequest(request)) {
|
|
121 |
3
| if (SecurityContextHolder.getContext().getAuthentication() == null) {
|
|
122 |
2
| SecurityContextHolder.getContext().setAuthentication(createAuthentication(
|
|
123 |
| request)); |
|
124 |
2
| addedToken = true;
|
|
125 |
| |
|
126 |
2
| if (logger.isDebugEnabled()) {
|
|
127 |
0
| logger.debug(
|
|
128 |
| "Populated SecurityContextHolder with anonymous token: '" |
|
129 |
| + SecurityContextHolder.getContext().getAuthentication() |
|
130 |
| + "'"); |
|
131 |
| } |
|
132 |
| } else { |
|
133 |
1
| if (logger.isDebugEnabled()) {
|
|
134 |
0
| logger.debug(
|
|
135 |
| "SecurityContextHolder not populated with anonymous token, as it already contained: '" |
|
136 |
| + SecurityContextHolder.getContext().getAuthentication() |
|
137 |
| + "'"); |
|
138 |
| } |
|
139 |
| } |
|
140 |
| } |
|
141 |
| |
|
142 |
3
| try {
|
|
143 |
3
| chain.doFilter(request, response);
|
|
144 |
| } finally { |
|
145 |
3
| if (addedToken && removeAfterRequest
|
|
146 |
| && createAuthentication(request).equals(SecurityContextHolder.getContext() |
|
147 |
| .getAuthentication())) { |
|
148 |
1
| SecurityContextHolder.getContext().setAuthentication(null);
|
|
149 |
| } |
|
150 |
| } |
|
151 |
| } |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
3
| public void init(FilterConfig ignored) throws ServletException {}
|
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
3
| protected boolean applyAnonymousForThisRequest(ServletRequest request) {
|
|
177 |
3
| return true;
|
|
178 |
| } |
|
179 |
| |
|
180 |
3
| protected Authentication createAuthentication(ServletRequest request) {
|
|
181 |
3
| return new AnonymousAuthenticationToken(key,
|
|
182 |
| userAttribute.getPassword(), userAttribute.getAuthorities()); |
|
183 |
| } |
|
184 |
| } |