|
28 | 28 | import java.util.List;
|
29 | 29 | import java.util.Map;
|
30 | 30 |
|
| 31 | +import org.springframework.security.core.context.ReactiveSecurityContextHolder; |
31 | 32 | import reactor.core.publisher.Mono;
|
32 | 33 | import reactor.util.context.Context;
|
33 | 34 |
|
@@ -552,7 +553,7 @@ protected void configure(ServerHttpSecurity http) {
|
552 | 553 | }
|
553 | 554 |
|
554 | 555 | AuthenticationWebFilter authenticationFilter = new OAuth2LoginAuthenticationWebFilter(manager, authorizedClientRepository);
|
555 |
| - authenticationFilter.setRequiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/login/oauth2/code/{registrationId}")); |
| 556 | + authenticationFilter.setRequiresAuthenticationMatcher(createAttemptAuthenticationRequestMatcher()); |
556 | 557 | authenticationFilter.setServerAuthenticationConverter(new ServerOAuth2AuthorizationCodeAuthenticationTokenConverter(clientRegistrationRepository));
|
557 | 558 |
|
558 | 559 | RedirectServerAuthenticationSuccessHandler redirectHandler = new RedirectServerAuthenticationSuccessHandler();
|
@@ -581,6 +582,13 @@ public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange,
|
581 | 582 | http.addFilterAt(authenticationFilter, SecurityWebFiltersOrder.AUTHENTICATION);
|
582 | 583 | }
|
583 | 584 |
|
| 585 | + private ServerWebExchangeMatcher createAttemptAuthenticationRequestMatcher() { |
| 586 | + PathPatternParserServerWebExchangeMatcher loginPathMatcher = new PathPatternParserServerWebExchangeMatcher("/login/oauth2/code/{registrationId}"); |
| 587 | + ServerWebExchangeMatcher notAuthenticatedMatcher = e -> ReactiveSecurityContextHolder.getContext() |
| 588 | + .flatMap(p -> ServerWebExchangeMatcher.MatchResult.notMatch()) |
| 589 | + .switchIfEmpty(ServerWebExchangeMatcher.MatchResult.match()); |
| 590 | + return new AndServerWebExchangeMatcher(loginPathMatcher, notAuthenticatedMatcher); |
| 591 | + } |
584 | 592 | private Map<String, String> getLinks() {
|
585 | 593 | Iterable<ClientRegistration> registrations = getBeanOrNull(ResolvableType.forClassWithGenerics(Iterable.class, ClientRegistration.class));
|
586 | 594 | if (registrations == null) {
|
@@ -686,7 +694,7 @@ protected void configure(ServerHttpSecurity http) {
|
686 | 694 |
|
687 | 695 | OAuth2AuthorizationRequestRedirectWebFilter oauthRedirectFilter = new OAuth2AuthorizationRequestRedirectWebFilter(
|
688 | 696 | clientRegistrationRepository);
|
689 |
| - http.addFilterAt(codeGrantWebFilter, SecurityWebFiltersOrder.AUTHENTICATION); |
| 697 | + http.addFilterAt(codeGrantWebFilter, SecurityWebFiltersOrder.OAUTH2_AUTHORIZATION_CODE); |
690 | 698 | http.addFilterAt(oauthRedirectFilter, SecurityWebFiltersOrder.HTTP_BASIC);
|
691 | 699 | }
|
692 | 700 |
|
|
0 commit comments