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