|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 | import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
25 | 25 | import org.springframework.security.web.server.DefaultServerRedirectStrategy;
|
26 | 26 | import org.springframework.security.web.server.ServerRedirectStrategy;
|
| 27 | +import org.springframework.security.web.server.savedrequest.ServerRequestCache; |
| 28 | +import org.springframework.security.web.server.savedrequest.WebSessionServerRequestCache; |
27 | 29 | import org.springframework.util.Assert;
|
28 | 30 | import org.springframework.web.server.ServerWebExchange;
|
29 | 31 | import org.springframework.web.server.WebFilter;
|
@@ -67,6 +69,7 @@ public class OAuth2AuthorizationRequestRedirectWebFilter implements WebFilter {
|
67 | 69 | private final ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver;
|
68 | 70 | private ServerAuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository =
|
69 | 71 | new WebSessionOAuth2ServerAuthorizationRequestRepository();
|
| 72 | + private ServerRequestCache requestCache = new WebSessionServerRequestCache(); |
70 | 73 |
|
71 | 74 | /**
|
72 | 75 | * Constructs an {@code OAuth2AuthorizationRequestRedirectFilter} using the provided parameters.
|
@@ -98,11 +101,23 @@ public final void setAuthorizationRequestRepository(
|
98 | 101 | this.authorizationRequestRepository = authorizationRequestRepository;
|
99 | 102 | }
|
100 | 103 |
|
| 104 | + /** |
| 105 | + * The request cache to use to save the request before sending a redirect. |
| 106 | + * @param requestCache the cache to redirect to. |
| 107 | + */ |
| 108 | + public void setRequestCache(ServerRequestCache requestCache) { |
| 109 | + Assert.notNull(requestCache, "requestCache cannot be null"); |
| 110 | + this.requestCache = requestCache; |
| 111 | + } |
| 112 | + |
101 | 113 | @Override
|
102 | 114 | public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
103 | 115 | return this.authorizationRequestResolver.resolve(exchange)
|
104 | 116 | .switchIfEmpty(chain.filter(exchange).then(Mono.empty()))
|
105 |
| - .onErrorResume(ClientAuthorizationRequiredException.class, e -> this.authorizationRequestResolver.resolve(exchange, e.getClientRegistrationId())) |
| 117 | + .onErrorResume(ClientAuthorizationRequiredException.class, e -> { |
| 118 | + return this.requestCache.saveRequest(exchange) |
| 119 | + .then(this.authorizationRequestResolver.resolve(exchange, e.getClientRegistrationId())); |
| 120 | + }) |
106 | 121 | .flatMap(clientRegistration -> sendRedirectForAuthorization(exchange, clientRegistration));
|
107 | 122 | }
|
108 | 123 |
|
|
0 commit comments