| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.ui.basicauth; |
| 17 |
|
|
| 18 |
|
import java.io.IOException; |
| 19 |
|
|
| 20 |
|
import javax.servlet.Filter; |
| 21 |
|
import javax.servlet.FilterChain; |
| 22 |
|
import javax.servlet.FilterConfig; |
| 23 |
|
import javax.servlet.ServletException; |
| 24 |
|
import javax.servlet.ServletRequest; |
| 25 |
|
import javax.servlet.ServletResponse; |
| 26 |
|
import javax.servlet.http.HttpServletRequest; |
| 27 |
|
import javax.servlet.http.HttpServletResponse; |
| 28 |
|
|
| 29 |
|
import org.acegisecurity.Authentication; |
| 30 |
|
import org.acegisecurity.AuthenticationException; |
| 31 |
|
import org.acegisecurity.AuthenticationManager; |
| 32 |
|
import org.acegisecurity.context.SecurityContextHolder; |
| 33 |
|
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
| 34 |
|
import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken; |
| 35 |
|
import org.acegisecurity.ui.AuthenticationDetailsSource; |
| 36 |
|
import org.acegisecurity.ui.AuthenticationDetailsSourceImpl; |
| 37 |
|
import org.acegisecurity.ui.AuthenticationEntryPoint; |
| 38 |
|
import org.acegisecurity.ui.rememberme.RememberMeServices; |
| 39 |
|
import org.apache.commons.codec.binary.Base64; |
| 40 |
|
import org.apache.commons.logging.Log; |
| 41 |
|
import org.apache.commons.logging.LogFactory; |
| 42 |
|
import org.springframework.beans.factory.InitializingBean; |
| 43 |
|
import org.springframework.util.Assert; |
| 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 |
17 |
public class BasicProcessingFilter implements Filter, InitializingBean { |
| 77 |
|
|
| 78 |
|
|
| 79 |
2 |
private static final Log logger = LogFactory.getLog(BasicProcessingFilter.class); |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
17 |
private AuthenticationDetailsSource authenticationDetailsSource = new AuthenticationDetailsSourceImpl(); |
| 84 |
|
private AuthenticationEntryPoint authenticationEntryPoint; |
| 85 |
|
private AuthenticationManager authenticationManager; |
| 86 |
|
private RememberMeServices rememberMeServices; |
| 87 |
17 |
private boolean ignoreFailure = false; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
public void afterPropertiesSet() throws Exception { |
| 92 |
2 |
Assert.notNull(this.authenticationManager, "An AuthenticationManager is required"); |
| 93 |
1 |
Assert.notNull(this.authenticationEntryPoint, "An AuthenticationEntryPoint is required"); |
| 94 |
0 |
} |
| 95 |
|
|
| 96 |
8 |
public void destroy() {} |
| 97 |
|
|
| 98 |
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 99 |
|
throws IOException, ServletException { |
| 100 |
|
|
| 101 |
10 |
if (!(request instanceof HttpServletRequest)) { |
| 102 |
1 |
throw new ServletException("Can only process HttpServletRequest"); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
9 |
if (!(response instanceof HttpServletResponse)) { |
| 106 |
1 |
throw new ServletException("Can only process HttpServletResponse"); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
8 |
HttpServletRequest httpRequest = (HttpServletRequest) request; |
| 110 |
8 |
HttpServletResponse httpResponse = (HttpServletResponse) response; |
| 111 |
|
|
| 112 |
8 |
String header = httpRequest.getHeader("Authorization"); |
| 113 |
|
|
| 114 |
8 |
if (logger.isDebugEnabled()) { |
| 115 |
0 |
logger.debug("Authorization header: " + header); |
| 116 |
|
} |
| 117 |
|
|
| 118 |
8 |
if ((header != null) && header.startsWith("Basic ")) { |
| 119 |
6 |
String base64Token = header.substring(6); |
| 120 |
6 |
String token = new String(Base64.decodeBase64(base64Token.getBytes())); |
| 121 |
|
|
| 122 |
6 |
String username = ""; |
| 123 |
6 |
String password = ""; |
| 124 |
6 |
int delim = token.indexOf(":"); |
| 125 |
|
|
| 126 |
6 |
if (delim != -1) { |
| 127 |
5 |
username = token.substring(0, delim); |
| 128 |
5 |
password = token.substring(delim + 1); |
| 129 |
|
} |
| 130 |
|
|
| 131 |
6 |
if (authenticationIsRequired(username)) { |
| 132 |
6 |
UsernamePasswordAuthenticationToken authRequest = |
| 133 |
|
new UsernamePasswordAuthenticationToken(username, password); |
| 134 |
6 |
authRequest.setDetails(authenticationDetailsSource.buildDetails((HttpServletRequest) request)); |
| 135 |
|
|
| 136 |
|
Authentication authResult; |
| 137 |
|
|
| 138 |
|
try { |
| 139 |
6 |
authResult = authenticationManager.authenticate(authRequest); |
| 140 |
4 |
} catch (AuthenticationException failed) { |
| 141 |
|
|
| 142 |
4 |
if (logger.isDebugEnabled()) { |
| 143 |
0 |
logger.debug("Authentication request for user: " + username + " failed: " + failed.toString()); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
4 |
SecurityContextHolder.getContext().setAuthentication(null); |
| 147 |
|
|
| 148 |
4 |
if (rememberMeServices != null) { |
| 149 |
0 |
rememberMeServices.loginFail(httpRequest, httpResponse); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
4 |
if (ignoreFailure) { |
| 153 |
1 |
chain.doFilter(request, response); |
| 154 |
|
} else { |
| 155 |
3 |
authenticationEntryPoint.commence(request, response, failed); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
4 |
return; |
| 159 |
2 |
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
2 |
if (logger.isDebugEnabled()) { |
| 163 |
0 |
logger.debug("Authentication success: " + authResult.toString()); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
2 |
SecurityContextHolder.getContext().setAuthentication(authResult); |
| 167 |
|
|
| 168 |
2 |
if (rememberMeServices != null) { |
| 169 |
0 |
rememberMeServices.loginSuccess(httpRequest, httpResponse, authResult); |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
|
|
| 174 |
4 |
chain.doFilter(request, response); |
| 175 |
4 |
} |
| 176 |
|
|
| 177 |
|
private boolean authenticationIsRequired(String username) { |
| 178 |
|
|
| 179 |
|
|
| 180 |
6 |
Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); |
| 181 |
|
|
| 182 |
6 |
if(existingAuth == null || !existingAuth.isAuthenticated()) { |
| 183 |
5 |
return true; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
1 |
if (existingAuth instanceof UsernamePasswordAuthenticationToken && !existingAuth.getName().equals(username)) { |
| 190 |
1 |
return true; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
0 |
if (existingAuth instanceof AnonymousAuthenticationToken) { |
| 201 |
0 |
return true; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
0 |
return false; |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
public AuthenticationEntryPoint getAuthenticationEntryPoint() { |
| 208 |
1 |
return authenticationEntryPoint; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
public AuthenticationManager getAuthenticationManager() { |
| 212 |
1 |
return authenticationManager; |
| 213 |
|
} |
| 214 |
|
|
| 215 |
8 |
public void init(FilterConfig arg0) throws ServletException {} |
| 216 |
|
|
| 217 |
|
public boolean isIgnoreFailure() { |
| 218 |
2 |
return ignoreFailure; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
public void setAuthenticationDetailsSource(AuthenticationDetailsSource authenticationDetailsSource) { |
| 222 |
0 |
Assert.notNull(authenticationDetailsSource, "AuthenticationDetailsSource required"); |
| 223 |
0 |
this.authenticationDetailsSource = authenticationDetailsSource; |
| 224 |
0 |
} |
| 225 |
|
|
| 226 |
|
public void setAuthenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) { |
| 227 |
14 |
this.authenticationEntryPoint = authenticationEntryPoint; |
| 228 |
14 |
} |
| 229 |
|
|
| 230 |
|
public void setAuthenticationManager(AuthenticationManager authenticationManager) { |
| 231 |
14 |
this.authenticationManager = authenticationManager; |
| 232 |
14 |
} |
| 233 |
|
|
| 234 |
|
public void setIgnoreFailure(boolean ignoreFailure) { |
| 235 |
1 |
this.ignoreFailure = ignoreFailure; |
| 236 |
1 |
} |
| 237 |
|
|
| 238 |
|
public void setRememberMeServices(RememberMeServices rememberMeServices) { |
| 239 |
0 |
this.rememberMeServices = rememberMeServices; |
| 240 |
0 |
} |
| 241 |
|
|
| 242 |
|
} |