mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 09:12:14 +00:00
SEC-1413: Add RedirectStrategy to AbstractRetryEntryPoint.
This commit is contained in:
parent
dca0fd871c
commit
db6da77a5f
@ -1,9 +1,6 @@
|
|||||||
package org.springframework.security.web.access.channel;
|
package org.springframework.security.web.access.channel;
|
||||||
|
|
||||||
import org.springframework.security.web.PortMapper;
|
import org.springframework.security.web.*;
|
||||||
import org.springframework.security.web.PortMapperImpl;
|
|
||||||
import org.springframework.security.web.PortResolver;
|
|
||||||
import org.springframework.security.web.PortResolverImpl;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
@ -30,6 +27,8 @@ public abstract class AbstractRetryEntryPoint implements ChannelEntryPoint {
|
|||||||
/** The standard port for the scheme (80 for http, 443 for https) */
|
/** The standard port for the scheme (80 for http, 443 for https) */
|
||||||
private final int standardPort;
|
private final int standardPort;
|
||||||
|
|
||||||
|
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
public AbstractRetryEntryPoint(String scheme, int standardPort) {
|
public AbstractRetryEntryPoint(String scheme, int standardPort) {
|
||||||
@ -39,11 +38,11 @@ public abstract class AbstractRetryEntryPoint implements ChannelEntryPoint {
|
|||||||
|
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
public void commence(HttpServletRequest request, HttpServletResponse res) throws IOException, ServletException {
|
public void commence(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||||
String queryString = request.getQueryString();
|
String queryString = request.getQueryString();
|
||||||
String redirectUrl = request.getRequestURI() + ((queryString == null) ? "" : ("?" + queryString));
|
String redirectUrl = request.getRequestURI() + ((queryString == null) ? "" : ("?" + queryString));
|
||||||
|
|
||||||
Integer currentPort = new Integer(portResolver.getServerPort(request));
|
Integer currentPort = Integer.valueOf(portResolver.getServerPort(request));
|
||||||
Integer redirectPort = getMappedPort(currentPort);
|
Integer redirectPort = getMappedPort(currentPort);
|
||||||
|
|
||||||
if (redirectPort != null) {
|
if (redirectPort != null) {
|
||||||
@ -56,7 +55,7 @@ public abstract class AbstractRetryEntryPoint implements ChannelEntryPoint {
|
|||||||
logger.debug("Redirecting to: " + redirectUrl);
|
logger.debug("Redirecting to: " + redirectUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.sendRedirect(res.encodeRedirectURL(redirectUrl));
|
redirectStrategy.sendRedirect(request, response, redirectUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Integer getMappedPort(Integer mapFromPort);
|
protected abstract Integer getMappedPort(Integer mapFromPort);
|
||||||
@ -65,10 +64,6 @@ public abstract class AbstractRetryEntryPoint implements ChannelEntryPoint {
|
|||||||
return portMapper;
|
return portMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final PortResolver getPortResolver() {
|
|
||||||
return portResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPortMapper(PortMapper portMapper) {
|
public void setPortMapper(PortMapper portMapper) {
|
||||||
Assert.notNull(portMapper, "portMapper cannot be null");
|
Assert.notNull(portMapper, "portMapper cannot be null");
|
||||||
this.portMapper = portMapper;
|
this.portMapper = portMapper;
|
||||||
@ -78,4 +73,23 @@ public abstract class AbstractRetryEntryPoint implements ChannelEntryPoint {
|
|||||||
Assert.notNull(portResolver, "portResolver cannot be null");
|
Assert.notNull(portResolver, "portResolver cannot be null");
|
||||||
this.portResolver = portResolver;
|
this.portResolver = portResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final PortResolver getPortResolver() {
|
||||||
|
return portResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the strategy to be used for redirecting to the required channel URL. A {@code DefaultRedirectStrategy}
|
||||||
|
* instance will be used if not set.
|
||||||
|
*
|
||||||
|
* @param redirectStrategy the strategy instance to which the URL will be passed.
|
||||||
|
*/
|
||||||
|
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
|
||||||
|
Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
|
||||||
|
this.redirectStrategy = redirectStrategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final RedirectStrategy getRedirectStrategy() {
|
||||||
|
return redirectStrategy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,16 @@
|
|||||||
|
|
||||||
package org.springframework.security.web.access.channel;
|
package org.springframework.security.web.access.channel;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.springframework.security.MockPortResolver;
|
import org.springframework.security.MockPortResolver;
|
||||||
|
|
||||||
|
import org.springframework.security.web.PortMapper;
|
||||||
import org.springframework.security.web.PortMapperImpl;
|
import org.springframework.security.web.PortMapperImpl;
|
||||||
|
import org.springframework.security.web.PortResolver;
|
||||||
|
import org.springframework.security.web.RedirectStrategy;
|
||||||
import org.springframework.security.web.access.channel.RetryWithHttpEntryPoint;
|
import org.springframework.security.web.access.channel.RetryWithHttpEntryPoint;
|
||||||
|
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
@ -59,10 +64,15 @@ public class RetryWithHttpEntryPointTests extends TestCase {
|
|||||||
|
|
||||||
public void testGettersSetters() {
|
public void testGettersSetters() {
|
||||||
RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
|
RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
|
||||||
ep.setPortMapper(new PortMapperImpl());
|
PortMapper portMapper = mock(PortMapper.class);
|
||||||
ep.setPortResolver(new MockPortResolver(8080, 8443));
|
PortResolver portResolver = mock(PortResolver.class);
|
||||||
assertTrue(ep.getPortMapper() != null);
|
RedirectStrategy redirector = mock(RedirectStrategy.class);
|
||||||
assertTrue(ep.getPortResolver() != null);
|
ep.setPortMapper(portMapper);
|
||||||
|
ep.setPortResolver(portResolver);
|
||||||
|
ep.setRedirectStrategy(redirector);
|
||||||
|
assertSame(portMapper, ep.getPortMapper());
|
||||||
|
assertSame(portResolver, ep.getPortResolver());
|
||||||
|
assertSame(redirector, ep.getRedirectStrategy());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNormalOperation() throws Exception {
|
public void testNormalOperation() throws Exception {
|
||||||
|
@ -37,10 +37,6 @@ import java.util.Map;
|
|||||||
public class RetryWithHttpsEntryPointTests extends TestCase {
|
public class RetryWithHttpsEntryPointTests extends TestCase {
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
public final void setUp() throws Exception {
|
|
||||||
super.setUp();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testDetectsMissingPortMapper() throws Exception {
|
public void testDetectsMissingPortMapper() throws Exception {
|
||||||
RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
|
RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user