parent
5502856095
commit
98412d530f
|
@ -28,7 +28,7 @@ import java.net.URI;
|
|||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public class DefaultRedirectStrategy implements RedirectStrategy {
|
||||
public class DefaultServerRedirectStrategy implements ServerRedirectStrategy {
|
||||
private HttpStatus httpStatus = HttpStatus.FOUND;
|
||||
|
||||
private boolean contextRelative = true;
|
|
@ -26,7 +26,7 @@ import org.springframework.web.server.ServerWebExchange;
|
|||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public interface RedirectStrategy {
|
||||
public interface ServerRedirectStrategy {
|
||||
|
||||
Mono<Void> sendRedirect(ServerWebExchange exchange, URI location);
|
||||
}
|
|
@ -18,12 +18,10 @@ package org.springframework.security.web.server.authentication;
|
|||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.security.web.server.DefaultRedirectStrategy;
|
||||
import org.springframework.security.web.server.RedirectStrategy;
|
||||
import org.springframework.security.web.server.DefaultServerRedirectStrategy;
|
||||
import org.springframework.security.web.server.ServerRedirectStrategy;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.server.AuthenticationEntryPoint;
|
||||
import org.springframework.util.Assert;
|
||||
|
@ -38,7 +36,7 @@ import org.springframework.web.server.ServerWebExchange;
|
|||
public class RedirectAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
private final URI location;
|
||||
|
||||
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||
private ServerRedirectStrategy serverRedirectStrategy = new DefaultServerRedirectStrategy();
|
||||
|
||||
public RedirectAuthenticationEntryPoint(String location) {
|
||||
Assert.notNull(location, "location cannot be null");
|
||||
|
@ -47,15 +45,15 @@ public class RedirectAuthenticationEntryPoint implements AuthenticationEntryPoin
|
|||
|
||||
@Override
|
||||
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
||||
return this.redirectStrategy.sendRedirect(exchange, this.location);
|
||||
return this.serverRedirectStrategy.sendRedirect(exchange, this.location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the RedirectStrategy to use.
|
||||
* @param redirectStrategy the strategy to use. Default is DefaultRedirectStrategy.
|
||||
* @param serverRedirectStrategy the strategy to use. Default is DefaultRedirectStrategy.
|
||||
*/
|
||||
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
|
||||
Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
|
||||
this.redirectStrategy = redirectStrategy;
|
||||
public void setServerRedirectStrategy(ServerRedirectStrategy serverRedirectStrategy) {
|
||||
Assert.notNull(serverRedirectStrategy, "redirectStrategy cannot be null");
|
||||
this.serverRedirectStrategy = serverRedirectStrategy;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,12 +17,11 @@
|
|||
package org.springframework.security.web.server.authentication;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.server.DefaultRedirectStrategy;
|
||||
import org.springframework.security.web.server.RedirectStrategy;
|
||||
import org.springframework.security.web.server.DefaultServerRedirectStrategy;
|
||||
import org.springframework.security.web.server.ServerRedirectStrategy;
|
||||
import org.springframework.security.web.server.WebFilterExchange;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
|
@ -34,7 +33,7 @@ import java.net.URI;
|
|||
public class RedirectAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
||||
private URI location = URI.create("/");
|
||||
|
||||
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||
private ServerRedirectStrategy serverRedirectStrategy = new DefaultServerRedirectStrategy();
|
||||
|
||||
public RedirectAuthenticationSuccessHandler() {}
|
||||
|
||||
|
@ -45,7 +44,7 @@ public class RedirectAuthenticationSuccessHandler implements AuthenticationSucce
|
|||
@Override
|
||||
public Mono<Void> success(Authentication authentication, WebFilterExchange webFilterExchange) {
|
||||
ServerWebExchange exchange = webFilterExchange.getExchange();
|
||||
return this.redirectStrategy.sendRedirect(exchange, this.location);
|
||||
return this.serverRedirectStrategy.sendRedirect(exchange, this.location);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,10 +58,10 @@ public class RedirectAuthenticationSuccessHandler implements AuthenticationSucce
|
|||
|
||||
/**
|
||||
* The RedirectStrategy to use.
|
||||
* @param redirectStrategy the strategy to use. Default is DefaultRedirectStrategy.
|
||||
* @param serverRedirectStrategy the strategy to use. Default is DefaultRedirectStrategy.
|
||||
*/
|
||||
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
|
||||
Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
|
||||
this.redirectStrategy = redirectStrategy;
|
||||
public void setServerRedirectStrategy(ServerRedirectStrategy serverRedirectStrategy) {
|
||||
Assert.notNull(serverRedirectStrategy, "redirectStrategy cannot be null");
|
||||
this.serverRedirectStrategy = serverRedirectStrategy;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
package org.springframework.security.web.server.authentication.logout;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.server.DefaultRedirectStrategy;
|
||||
import org.springframework.security.web.server.RedirectStrategy;
|
||||
import org.springframework.security.web.server.DefaultServerRedirectStrategy;
|
||||
import org.springframework.security.web.server.ServerRedirectStrategy;
|
||||
import org.springframework.security.web.server.context.SecurityContextRepository;
|
||||
import org.springframework.security.web.server.WebFilterExchange;
|
||||
import org.springframework.security.web.server.context.WebSessionSecurityContextRepository;
|
||||
|
@ -35,12 +35,12 @@ public class SecurityContextRepositoryLogoutHandler implements LogoutHandler {
|
|||
|
||||
private URI logoutSuccessUrl = URI.create("/login?logout");
|
||||
|
||||
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||
private ServerRedirectStrategy serverRedirectStrategy = new DefaultServerRedirectStrategy();
|
||||
|
||||
@Override
|
||||
public Mono<Void> logout(WebFilterExchange exchange,
|
||||
Authentication authentication) {
|
||||
return this.repository.save(exchange.getExchange(), null)
|
||||
.then(this.redirectStrategy.sendRedirect(exchange.getExchange(), this.logoutSuccessUrl));
|
||||
.then(this.serverRedirectStrategy.sendRedirect(exchange.getExchange(), this.logoutSuccessUrl));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.server.authentication.RedirectAuthenticationEntryPoint;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import java.net.URI;
|
||||
|
@ -37,15 +36,15 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
|||
* @since 5.0
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultRedirectStrategyTests {
|
||||
public class DefaultServerRedirectStrategyTests {
|
||||
|
||||
@Mock
|
||||
private ServerWebExchange exchange;
|
||||
|
||||
private URI location = URI.create("/login");
|
||||
|
||||
private DefaultRedirectStrategy strategy =
|
||||
new DefaultRedirectStrategy();
|
||||
private DefaultServerRedirectStrategy strategy =
|
||||
new DefaultServerRedirectStrategy();
|
||||
|
||||
private AuthenticationException exception = new AuthenticationCredentialsNotFoundException("Authentication Required");
|
||||
|
|
@ -25,7 +25,7 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.server.RedirectStrategy;
|
||||
import org.springframework.security.web.server.ServerRedirectStrategy;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
|
@ -44,7 +44,7 @@ public class RedirectAuthenticationEntryPointTests {
|
|||
@Mock
|
||||
private ServerWebExchange exchange;
|
||||
@Mock
|
||||
private RedirectStrategy redirectStrategy;
|
||||
private ServerRedirectStrategy serverRedirectStrategy;
|
||||
|
||||
private String location = "/login";
|
||||
|
||||
|
@ -81,9 +81,9 @@ public class RedirectAuthenticationEntryPointTests {
|
|||
@Test
|
||||
public void commenceWhenCustomStatusThenStatusSet() {
|
||||
Mono<Void> result = Mono.empty();
|
||||
when(this.redirectStrategy.sendRedirect(any(), any())).thenReturn(result);
|
||||
when(this.serverRedirectStrategy.sendRedirect(any(), any())).thenReturn(result);
|
||||
HttpStatus status = HttpStatus.MOVED_PERMANENTLY;
|
||||
this.entryPoint.setRedirectStrategy(this.redirectStrategy);
|
||||
this.entryPoint.setServerRedirectStrategy(this.serverRedirectStrategy);
|
||||
this.exchange = MockServerHttpRequest.get("/").toExchange();
|
||||
|
||||
assertThat(this.entryPoint.commence(this.exchange, this.exception)).isEqualTo(result);
|
||||
|
@ -91,6 +91,6 @@ public class RedirectAuthenticationEntryPointTests {
|
|||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setRedirectStrategyWhenNullThenException() {
|
||||
this.entryPoint.setRedirectStrategy(null);
|
||||
this.entryPoint.setServerRedirectStrategy(null);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,10 +22,8 @@ import org.mockito.Mock;
|
|||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.server.RedirectStrategy;
|
||||
import org.springframework.security.web.server.ServerRedirectStrategy;
|
||||
import org.springframework.security.web.server.WebFilterExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
|
@ -34,7 +32,6 @@ import reactor.core.publisher.Mono;
|
|||
import java.net.URI;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
@ -53,7 +50,7 @@ public class RedirectAuthenticationSuccessHandlerTests {
|
|||
@Mock
|
||||
private WebFilterChain chain;
|
||||
@Mock
|
||||
private RedirectStrategy redirectStrategy;
|
||||
private ServerRedirectStrategy serverRedirectStrategy;
|
||||
@Mock
|
||||
private Authentication authentication;
|
||||
|
||||
|
@ -90,18 +87,18 @@ public class RedirectAuthenticationSuccessHandlerTests {
|
|||
@Test
|
||||
public void successWhenCustomLocationThenCustomLocationUsed() {
|
||||
Mono<Void> result = Mono.empty();
|
||||
when(this.redirectStrategy.sendRedirect(any(), any())).thenReturn(result);
|
||||
this.handler.setRedirectStrategy(this.redirectStrategy);
|
||||
when(this.serverRedirectStrategy.sendRedirect(any(), any())).thenReturn(result);
|
||||
this.handler.setServerRedirectStrategy(this.serverRedirectStrategy);
|
||||
this.exchange = MockServerHttpRequest.get("/").toExchange();
|
||||
|
||||
assertThat(this.handler.success(this.authentication, new WebFilterExchange(this.exchange,
|
||||
this.chain))).isEqualTo(result);
|
||||
verify(this.redirectStrategy).sendRedirect(any(), eq(this.location));
|
||||
verify(this.serverRedirectStrategy).sendRedirect(any(), eq(this.location));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setRedirectStrategyWhenNullThenException() {
|
||||
this.handler.setRedirectStrategy(null);
|
||||
this.handler.setServerRedirectStrategy(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
|
Loading…
Reference in New Issue