| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.ui.webapp; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.AuthenticationException; |
| 19 |
|
|
| 20 |
|
import org.acegisecurity.ui.AuthenticationEntryPoint; |
| 21 |
|
|
| 22 |
|
import org.acegisecurity.util.PortMapper; |
| 23 |
|
import org.acegisecurity.util.PortMapperImpl; |
| 24 |
|
import org.acegisecurity.util.PortResolver; |
| 25 |
|
import org.acegisecurity.util.PortResolverImpl; |
| 26 |
|
|
| 27 |
|
import org.apache.commons.logging.Log; |
| 28 |
|
import org.apache.commons.logging.LogFactory; |
| 29 |
|
|
| 30 |
|
import org.springframework.beans.factory.InitializingBean; |
| 31 |
|
|
| 32 |
|
import org.springframework.util.Assert; |
| 33 |
|
|
| 34 |
|
import java.io.IOException; |
| 35 |
|
|
| 36 |
|
import javax.servlet.RequestDispatcher; |
| 37 |
|
import javax.servlet.ServletException; |
| 38 |
|
import javax.servlet.ServletRequest; |
| 39 |
|
import javax.servlet.ServletResponse; |
| 40 |
|
import javax.servlet.http.HttpServletRequest; |
| 41 |
|
import javax.servlet.http.HttpServletResponse; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
9 |
public class AuthenticationProcessingFilterEntryPoint implements AuthenticationEntryPoint, InitializingBean { |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
2 |
private static final Log logger = LogFactory.getLog(AuthenticationProcessingFilterEntryPoint.class); |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
9 |
private PortMapper portMapper = new PortMapperImpl(); |
| 76 |
|
|
| 77 |
9 |
private PortResolver portResolver = new PortResolverImpl(); |
| 78 |
|
|
| 79 |
|
private String loginFormUrl; |
| 80 |
|
|
| 81 |
9 |
private boolean forceHttps = false; |
| 82 |
|
|
| 83 |
9 |
private boolean serverSideRedirect = false; |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
public void afterPropertiesSet() throws Exception { |
| 89 |
10 |
Assert.hasLength(loginFormUrl, "loginFormUrl must be specified"); |
| 90 |
9 |
Assert.notNull(portMapper, "portMapper must be specified"); |
| 91 |
8 |
Assert.notNull(portResolver, "portResolver must be specified"); |
| 92 |
7 |
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, |
| 105 |
|
AuthenticationException exception) { |
| 106 |
8 |
return getLoginFormUrl(); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException) |
| 110 |
|
throws IOException, ServletException { |
| 111 |
8 |
HttpServletRequest req = (HttpServletRequest) request; |
| 112 |
8 |
HttpServletResponse resp = (HttpServletResponse) response; |
| 113 |
8 |
String scheme = request.getScheme(); |
| 114 |
8 |
String serverName = request.getServerName(); |
| 115 |
8 |
int serverPort = portResolver.getServerPort(request); |
| 116 |
8 |
String contextPath = req.getContextPath(); |
| 117 |
|
|
| 118 |
8 |
boolean inHttp = "http".equals(scheme.toLowerCase()); |
| 119 |
8 |
boolean inHttps = "https".equals(scheme.toLowerCase()); |
| 120 |
|
|
| 121 |
8 |
boolean includePort = true; |
| 122 |
|
|
| 123 |
8 |
String redirectUrl = null; |
| 124 |
8 |
boolean doForceHttps = false; |
| 125 |
8 |
Integer httpsPort = null; |
| 126 |
|
|
| 127 |
8 |
if (inHttp && (serverPort == 80)) { |
| 128 |
2 |
includePort = false; |
| 129 |
|
} |
| 130 |
6 |
else if (inHttps && (serverPort == 443)) { |
| 131 |
1 |
includePort = false; |
| 132 |
|
} |
| 133 |
|
|
| 134 |
8 |
if (forceHttps && inHttp) { |
| 135 |
5 |
httpsPort = (Integer) portMapper.lookupHttpsPort(new Integer(serverPort)); |
| 136 |
|
|
| 137 |
5 |
if (httpsPort != null) { |
| 138 |
4 |
doForceHttps = true; |
| 139 |
4 |
if (httpsPort.intValue() == 443) { |
| 140 |
1 |
includePort = false; |
| 141 |
|
} |
| 142 |
|
else { |
| 143 |
3 |
includePort = true; |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
} |
| 148 |
|
|
| 149 |
8 |
String loginForm = determineUrlToUseForThisRequest(req, resp, authException); |
| 150 |
|
|
| 151 |
8 |
if (serverSideRedirect) { |
| 152 |
|
|
| 153 |
0 |
if (doForceHttps) { |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
0 |
String servletPath = req.getServletPath(); |
| 159 |
0 |
String pathInfo = req.getPathInfo(); |
| 160 |
0 |
String query = req.getQueryString(); |
| 161 |
|
|
| 162 |
0 |
redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath |
| 163 |
|
+ servletPath + (pathInfo == null ? "" : pathInfo) + (query == null ? "" : "?" + query); |
| 164 |
|
|
| 165 |
0 |
} |
| 166 |
|
else { |
| 167 |
|
|
| 168 |
0 |
if (logger.isDebugEnabled()) { |
| 169 |
0 |
logger.debug("Server side forward to: " + loginForm); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
0 |
RequestDispatcher dispatcher = req.getRequestDispatcher(loginForm); |
| 173 |
|
|
| 174 |
0 |
dispatcher.forward(request, response); |
| 175 |
|
|
| 176 |
0 |
return; |
| 177 |
|
|
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
} |
| 181 |
|
else { |
| 182 |
|
|
| 183 |
8 |
if (doForceHttps) { |
| 184 |
|
|
| 185 |
4 |
redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath |
| 186 |
|
+ loginForm; |
| 187 |
|
|
| 188 |
|
} |
| 189 |
|
else { |
| 190 |
|
|
| 191 |
4 |
redirectUrl = scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath |
| 192 |
|
+ loginForm; |
| 193 |
|
|
| 194 |
|
} |
| 195 |
|
} |
| 196 |
|
|
| 197 |
8 |
if (logger.isDebugEnabled()) { |
| 198 |
0 |
logger.debug("Redirecting to: " + redirectUrl); |
| 199 |
|
} |
| 200 |
|
|
| 201 |
8 |
((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl)); |
| 202 |
8 |
} |
| 203 |
|
|
| 204 |
|
public boolean getForceHttps() { |
| 205 |
2 |
return forceHttps; |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
public String getLoginFormUrl() { |
| 209 |
9 |
return loginFormUrl; |
| 210 |
|
} |
| 211 |
|
|
| 212 |
|
public PortMapper getPortMapper() { |
| 213 |
1 |
return portMapper; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
public PortResolver getPortResolver() { |
| 217 |
1 |
return portResolver; |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
public boolean isServerSideRedirect() { |
| 221 |
0 |
return serverSideRedirect; |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
public void setForceHttps(boolean forceHttps) { |
| 233 |
6 |
this.forceHttps = forceHttps; |
| 234 |
6 |
} |
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
public void setLoginFormUrl(String loginFormUrl) { |
| 244 |
8 |
this.loginFormUrl = loginFormUrl; |
| 245 |
8 |
} |
| 246 |
|
|
| 247 |
|
public void setPortMapper(PortMapper portMapper) { |
| 248 |
11 |
this.portMapper = portMapper; |
| 249 |
11 |
} |
| 250 |
|
|
| 251 |
|
public void setPortResolver(PortResolver portResolver) { |
| 252 |
10 |
this.portResolver = portResolver; |
| 253 |
10 |
} |
| 254 |
|
|
| 255 |
|
|
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
public void setServerSideRedirect(boolean serverSideRedirect) { |
| 262 |
0 |
this.serverSideRedirect = serverSideRedirect; |
| 263 |
0 |
} |
| 264 |
|
|
| 265 |
|
} |