Refactored autoLogin method to reduce nesting of conditionals and loops.
This commit is contained in:
parent
38210d2039
commit
8b199d38ed
|
@ -16,7 +16,6 @@
|
||||||
package org.springframework.security.ui.rememberme;
|
package org.springframework.security.ui.rememberme;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
@ -24,7 +23,6 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.security.Authentication;
|
import org.springframework.security.Authentication;
|
||||||
import org.springframework.security.providers.rememberme.RememberMeAuthenticationToken;
|
import org.springframework.security.providers.rememberme.RememberMeAuthenticationToken;
|
||||||
import org.springframework.security.ui.AccessDeniedHandler;
|
|
||||||
import org.springframework.security.ui.AuthenticationDetailsSource;
|
import org.springframework.security.ui.AuthenticationDetailsSource;
|
||||||
import org.springframework.security.ui.AuthenticationDetailsSourceImpl;
|
import org.springframework.security.ui.AuthenticationDetailsSourceImpl;
|
||||||
import org.springframework.security.ui.logout.LogoutHandler;
|
import org.springframework.security.ui.logout.LogoutHandler;
|
||||||
|
@ -36,7 +34,6 @@ import org.apache.commons.codec.digest.DigestUtils;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.RequestUtils;
|
import org.springframework.web.bind.RequestUtils;
|
||||||
|
@ -143,27 +140,39 @@ public class TokenBasedRememberMeServices implements RememberMeServices, Initial
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < cookies.length; i++) {
|
for (int i = 0; i < cookies.length; i++) {
|
||||||
if (cookieName.equals(cookies[i].getName())) {
|
if (!cookieName.equals(cookies[i].getName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have the spring security cookie
|
||||||
String cookieValue = cookies[i].getValue();
|
String cookieValue = cookies[i].getValue();
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Remember-me cookie detected");
|
||||||
|
}
|
||||||
|
|
||||||
for (int j = 0; j < cookieValue.length() % 4; j++) {
|
for (int j = 0; j < cookieValue.length() % 4; j++) {
|
||||||
cookieValue = cookieValue + "=";
|
cookieValue = cookieValue + "=";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Base64.isArrayByteBase64(cookieValue.getBytes())) {
|
if (!Base64.isArrayByteBase64(cookieValue.getBytes())) {
|
||||||
if (logger.isDebugEnabled()) {
|
cancelCookie(request, response, "Cookie token was not Base64 encoded; value was '" + cookieValue + "'");
|
||||||
logger.debug("Remember-me cookie detected");
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode token from Base64
|
// Decode token from Base64
|
||||||
// format of token is:
|
// format of token is:
|
||||||
// username + ":" + expiryTime + ":" +
|
// username + ":" + expiryTime + ":" + Md5Hex(username + ":" + expiryTime + ":" + password + ":" + key)
|
||||||
// Md5Hex(username + ":" + expiryTime + ":" + password + ":"
|
|
||||||
// + key)
|
|
||||||
String cookieAsPlainText = new String(Base64.decodeBase64(cookieValue.getBytes()));
|
String cookieAsPlainText = new String(Base64.decodeBase64(cookieValue.getBytes()));
|
||||||
String[] cookieTokens = StringUtils.delimitedListToStringArray(cookieAsPlainText, ":");
|
String[] cookieTokens = StringUtils.delimitedListToStringArray(cookieAsPlainText, ":");
|
||||||
|
|
||||||
if (cookieTokens.length == 3) {
|
if (cookieTokens.length != 3) {
|
||||||
|
cancelCookie(request, response, "Cookie token did not contain 3 tokens; decoded value was '"
|
||||||
|
+ cookieAsPlainText + "'");
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
long tokenExpiryTime;
|
long tokenExpiryTime;
|
||||||
|
|
||||||
|
@ -172,8 +181,7 @@ public class TokenBasedRememberMeServices implements RememberMeServices, Initial
|
||||||
}
|
}
|
||||||
catch (NumberFormatException nfe) {
|
catch (NumberFormatException nfe) {
|
||||||
cancelCookie(request, response,
|
cancelCookie(request, response,
|
||||||
"Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1]
|
"Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1] + "')");
|
||||||
+ "')");
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -230,21 +238,7 @@ public class TokenBasedRememberMeServices implements RememberMeServices, Initial
|
||||||
|
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
cancelCookie(request, response, "Cookie token did not contain 3 tokens; decoded value was '"
|
|
||||||
+ cookieAsPlainText + "'");
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cancelCookie(request, response, "Cookie token was not Base64 encoded; value was '" + cookieValue
|
|
||||||
+ "'");
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue