Browse Source

RedirectAuthenticationEntryPoint uses RedirectStrategy

Issue gh-4529
pull/4515/head
Rob Winch 9 years ago
parent
commit
4392d47908
  1. 21
      webflux/src/main/java/org/springframework/security/web/server/authentication/RedirectAuthenticationEntryPoint.java
  2. 19
      webflux/src/test/java/org/springframework/security/web/server/authentication/RedirectAuthenticationEntryPointTests.java

21
webflux/src/main/java/org/springframework/security/web/server/authentication/RedirectAuthenticationEntryPoint.java

@ -20,6 +20,8 @@ package org.springframework.security.web.server.authentication;
import java.net.URI; import java.net.URI;
import org.springframework.security.web.server.DefaultRedirectStrategy;
import org.springframework.security.web.server.RedirectStrategy;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -38,7 +40,7 @@ import org.springframework.web.server.ServerWebExchange;
public class RedirectAuthenticationEntryPoint implements AuthenticationEntryPoint { public class RedirectAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final URI location; private final URI location;
private HttpStatus httpStatus = HttpStatus.FOUND; private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
public RedirectAuthenticationEntryPoint(String location) { public RedirectAuthenticationEntryPoint(String location) {
Assert.notNull(location, "location cannot be null"); Assert.notNull(location, "location cannot be null");
@ -47,20 +49,15 @@ public class RedirectAuthenticationEntryPoint implements AuthenticationEntryPoin
@Override @Override
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) { public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
return Mono.fromRunnable(() -> { return this.redirectStrategy.sendRedirect(exchange, this.location);
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(this.httpStatus);
response.getHeaders().setLocation(this.location);
});
} }
/** /**
* Sets the {@link HttpStatus}. * Sets the RedirectStrategy to use.
* * @param redirectStrategy the strategy to use. Default is DefaultRedirectStrategy.
* @param httpStatus the status to use. The default is {@code HttpStatus.FOUND}
*/ */
public void setHttpStatus(HttpStatus httpStatus) { public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
Assert.notNull(httpStatus, "httpStatus cannot be null"); Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
this.httpStatus = httpStatus; this.redirectStrategy = redirectStrategy;
} }
} }

19
webflux/src/test/java/org/springframework/security/web/server/authentication/RedirectAuthenticationEntryPointTests.java

@ -27,10 +27,14 @@ import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.RedirectStrategy;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
/** /**
* @author Rob Winch * @author Rob Winch
@ -41,6 +45,8 @@ public class RedirectAuthenticationEntryPointTests {
@Mock @Mock
private ServerWebExchange exchange; private ServerWebExchange exchange;
@Mock
private RedirectStrategy redirectStrategy;
private String location = "/login"; private String location = "/login";
@ -76,18 +82,17 @@ public class RedirectAuthenticationEntryPointTests {
@Test @Test
public void commenceWhenCustomStatusThenStatusSet() { public void commenceWhenCustomStatusThenStatusSet() {
Mono<Void> result = Mono.empty();
when(this.redirectStrategy.sendRedirect(any(), any())).thenReturn(result);
HttpStatus status = HttpStatus.MOVED_PERMANENTLY; HttpStatus status = HttpStatus.MOVED_PERMANENTLY;
this.entryPoint.setHttpStatus(status); this.entryPoint.setRedirectStrategy(this.redirectStrategy);
this.exchange = MockServerHttpRequest.get("/").toExchange(); this.exchange = MockServerHttpRequest.get("/").toExchange();
this.entryPoint.commence(this.exchange, this.exception).block(); assertThat(this.entryPoint.commence(this.exchange, this.exception)).isEqualTo(result);
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(status);
assertThat(this.exchange.getResponse().getHeaders().getLocation()).hasPath(this.location);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void setHttpStatusWhenNullLocationThenException() { public void setRedirectStrategyWhenNullThenException() {
this.entryPoint.setHttpStatus(null); this.entryPoint.setRedirectStrategy(null);
} }
} }

Loading…
Cancel
Save