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