|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.ui.rememberme; |
|
17 |
| |
|
18 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
19 |
| import org.acegisecurity.event.authentication.InteractiveAuthenticationSuccessEvent; |
|
20 |
| import org.acegisecurity.Authentication; |
|
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.context.ApplicationEventPublisher; |
|
28 |
| import org.springframework.context.ApplicationEventPublisherAware; |
|
29 |
| |
|
30 |
| import org.springframework.util.Assert; |
|
31 |
| |
|
32 |
| import java.io.IOException; |
|
33 |
| |
|
34 |
| import javax.servlet.Filter; |
|
35 |
| import javax.servlet.FilterChain; |
|
36 |
| import javax.servlet.FilterConfig; |
|
37 |
| import javax.servlet.ServletException; |
|
38 |
| import javax.servlet.ServletRequest; |
|
39 |
| import javax.servlet.ServletResponse; |
|
40 |
| import javax.servlet.http.HttpServletRequest; |
|
41 |
| import javax.servlet.http.HttpServletResponse; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| public class RememberMeProcessingFilter implements Filter, InitializingBean, |
|
78 |
| ApplicationEventPublisherAware { |
|
79 |
| |
|
80 |
| |
|
81 |
| private static final Log logger = LogFactory.getLog(RememberMeProcessingFilter.class); |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| private ApplicationEventPublisher eventPublisher; |
|
86 |
| private RememberMeServices rememberMeServices = new NullRememberMeServices(); |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
0
| public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
|
|
91 |
0
| this.eventPublisher = eventPublisher;
|
|
92 |
| } |
|
93 |
| |
|
94 |
4
| public void setRememberMeServices(RememberMeServices rememberMeServices) {
|
|
95 |
4
| this.rememberMeServices = rememberMeServices;
|
|
96 |
| } |
|
97 |
| |
|
98 |
2
| public RememberMeServices getRememberMeServices() {
|
|
99 |
2
| return rememberMeServices;
|
|
100 |
| } |
|
101 |
| |
|
102 |
3
| public void afterPropertiesSet() throws Exception {
|
|
103 |
3
| Assert.notNull(rememberMeServices);
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
2
| public void destroy() {}
|
|
110 |
| |
|
111 |
4
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
112 |
| FilterChain chain) throws IOException, ServletException { |
|
113 |
4
| if (!(request instanceof HttpServletRequest)) {
|
|
114 |
1
| throw new ServletException("Can only process HttpServletRequest");
|
|
115 |
| } |
|
116 |
| |
|
117 |
3
| if (!(response instanceof HttpServletResponse)) {
|
|
118 |
1
| throw new ServletException("Can only process HttpServletResponse");
|
|
119 |
| } |
|
120 |
| |
|
121 |
2
| HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|
122 |
2
| HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
123 |
| |
|
124 |
2
| if (SecurityContextHolder.getContext().getAuthentication() == null) {
|
|
125 |
1
| Authentication rememberMeAuth =
|
|
126 |
| rememberMeServices.autoLogin(httpRequest, httpResponse); |
|
127 |
| |
|
128 |
1
| if(rememberMeAuth != null) {
|
|
129 |
1
| SecurityContextHolder.getContext().setAuthentication(rememberMeAuth);
|
|
130 |
| |
|
131 |
1
| if (logger.isDebugEnabled()) {
|
|
132 |
0
| logger.debug(
|
|
133 |
| "SecurityContextHolder populated with remember-me token: '" |
|
134 |
| + SecurityContextHolder.getContext().getAuthentication() |
|
135 |
| + "'"); |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
1
| if (this.eventPublisher != null) {
|
|
140 |
0
| eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
|
|
141 |
| SecurityContextHolder.getContext().getAuthentication(), |
|
142 |
| this.getClass())); |
|
143 |
| } |
|
144 |
| } |
|
145 |
| } else { |
|
146 |
1
| if (logger.isDebugEnabled()) {
|
|
147 |
0
| logger.debug(
|
|
148 |
| "SecurityContextHolder not populated with remember-me token, as it already contained: '" |
|
149 |
| + SecurityContextHolder.getContext().getAuthentication() |
|
150 |
| + "'"); |
|
151 |
| } |
|
152 |
| } |
|
153 |
| |
|
154 |
2
| chain.doFilter(request, response);
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
2
| public void init(FilterConfig ignored) throws ServletException {}
|
|
164 |
| } |