Removed unnecessary casts, corrected incomplete comment and reformatted code.

This commit is contained in:
Luke Taylor 2007-10-19 11:53:26 +00:00
parent 380b22f50d
commit 28d04c1759

View File

@ -64,13 +64,11 @@ import javax.servlet.http.HttpServletResponse;
* 03:21:17Z benalex $
*/
public class AuthenticationProcessingFilterEntryPoint implements AuthenticationEntryPoint, InitializingBean {
// ~ Static fields/initializers
// =====================================================================================
//~ Static fields/initializers =====================================================================================
private static final Log logger = LogFactory.getLog(AuthenticationProcessingFilterEntryPoint.class);
// ~ Instance fields
// ================================================================================================
//~ Instance fields ================================================================================================
private PortMapper portMapper = new PortMapperImpl();
@ -82,8 +80,7 @@ public class AuthenticationProcessingFilterEntryPoint implements AuthenticationE
private boolean serverSideRedirect = false;
// ~ Methods
// ========================================================================================================
//~ Methods ========================================================================================================
public void afterPropertiesSet() throws Exception {
Assert.hasLength(loginFormUrl, "loginFormUrl must be specified");
@ -92,105 +89,85 @@ public class AuthenticationProcessingFilterEntryPoint implements AuthenticationE
}
/**
* Allows subclasses to modify the login form URL that should be applicable
* for a given request.
* Allows subclasses to modify the login form URL that should be applicable for a given request.
*
* @param request the request
* @param response the response
* @param exception the exception
* @return the URL (cannot be null or empty; defaults to
* {@link #getLoginFormUrl()})
* @return the URL (cannot be null or empty; defaults to {@link #getLoginFormUrl()})
*/
protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) {
return getLoginFormUrl();
}
public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String scheme = request.getScheme();
String serverName = request.getServerName();
int serverPort = portResolver.getServerPort(request);
String contextPath = req.getContextPath();
String contextPath = httpRequest.getContextPath();
boolean inHttp = "http".equals(scheme.toLowerCase());
boolean inHttps = "https".equals(scheme.toLowerCase());
boolean includePort = true;
String redirectUrl = null;
boolean doForceHttps = false;
Integer httpsPort = null;
if (inHttp && (serverPort == 80)) {
includePort = false;
}
else if (inHttps && (serverPort == 443)) {
} else if (inHttps && (serverPort == 443)) {
includePort = false;
}
if (forceHttps && inHttp) {
httpsPort = (Integer) portMapper.lookupHttpsPort(new Integer(serverPort));
httpsPort = portMapper.lookupHttpsPort(new Integer(serverPort));
if (httpsPort != null) {
doForceHttps = true;
if (httpsPort.intValue() == 443) {
includePort = false;
}
else {
} else {
includePort = true;
}
}
}
String loginForm = determineUrlToUseForThisRequest(req, resp, authException);
String loginForm = determineUrlToUseForThisRequest(httpRequest, httpResponse, authException);
String redirectUrl = null;
if (serverSideRedirect) {
if (doForceHttps) {
// before doing server side redirect, we need to do client redirect to https.
// before doing server side redirect, we need to do client
// redirect to https.
String servletPath = req.getServletPath();
String pathInfo = req.getPathInfo();
String query = req.getQueryString();
String servletPath = httpRequest.getServletPath();
String pathInfo = httpRequest.getPathInfo();
String query = httpRequest.getQueryString();
redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath
+ servletPath + (pathInfo == null ? "" : pathInfo) + (query == null ? "" : "?" + query);
}
else {
} else {
if (logger.isDebugEnabled()) {
logger.debug("Server side forward to: " + loginForm);
}
RequestDispatcher dispatcher = req.getRequestDispatcher(loginForm);
RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(loginForm);
dispatcher.forward(request, response);
return;
}
}
else {
} else {
if (doForceHttps) {
redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath
+ loginForm;
}
else {
} else {
redirectUrl = scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath
+ loginForm;
}
}
@ -198,7 +175,7 @@ public class AuthenticationProcessingFilterEntryPoint implements AuthenticationE
logger.debug("Redirecting to: " + redirectUrl);
}
((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl));
httpResponse.sendRedirect(httpResponse.encodeRedirectURL(redirectUrl));
}
public boolean getForceHttps() {
@ -222,10 +199,10 @@ public class AuthenticationProcessingFilterEntryPoint implements AuthenticationE
}
/**
* Set to true to force login form access to be via https. If this value is
* ture (the default is false), and the incoming request for the protected
* resource which triggered the interceptor was not already
* <code>https</code>, then
* Set to true to force login form access to be via https. If this value is true (the default is false),
* and the incoming request for the protected resource which triggered the interceptor was not already
* <code>https</code>, then the client will first be redirected to an https URL, even if <tt>serverSideRedirect</tt>
* is set to <tt>true</tt>.
*
* @param forceHttps
*/
@ -253,8 +230,7 @@ public class AuthenticationProcessingFilterEntryPoint implements AuthenticationE
}
/**
* Tells if we are to do a server side include of the
* <code>loginFormUrl</code> instead of a 302 redirect.
* Tells if we are to do a server side include of the <code>loginFormUrl</code> instead of a 302 redirect.
*
* @param serverSideRedirect
*/