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