|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.configurers.oauth2.client;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Field; |
19 | 20 | import java.util.ArrayList;
|
20 | 21 | import java.util.Collections;
|
21 | 22 | import java.util.HashMap;
|
|
73 | 74 | import org.springframework.security.web.savedrequest.RequestCache;
|
74 | 75 | import org.springframework.security.web.util.matcher.AndRequestMatcher;
|
75 | 76 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
| 77 | +import org.springframework.security.web.util.matcher.AnyRequestMatcher; |
76 | 78 | import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
|
77 | 79 | import org.springframework.security.web.util.matcher.OrRequestMatcher;
|
78 | 80 | import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
|
79 | 81 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
80 | 82 | import org.springframework.util.Assert;
|
81 | 83 | import org.springframework.util.ClassUtils;
|
| 84 | +import org.springframework.util.ReflectionUtils; |
82 | 85 |
|
83 | 86 | /**
|
84 | 87 | * An {@link AbstractHttpConfigurer} for OAuth 2.0 Login, which leverages the OAuth 2.0
|
@@ -503,14 +506,28 @@ private AuthenticationEntryPoint getLoginEntryPoint(B http, String providerLogin
|
503 | 506 | new OrRequestMatcher(loginPageMatcher, faviconMatcher), defaultEntryPointMatcher);
|
504 | 507 | RequestMatcher notXRequestedWith = new NegatedRequestMatcher(
|
505 | 508 | new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
|
| 509 | + RequestMatcher formLoginNotEnabled = getFormLoginNotEnabledRequestMatcher(http); |
506 | 510 | LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>();
|
507 |
| - entryPoints.put(new AndRequestMatcher(notXRequestedWith, new NegatedRequestMatcher(defaultLoginPageMatcher)), |
508 |
| - new LoginUrlAuthenticationEntryPoint(providerLoginPage)); |
| 511 | + entryPoints.put(new AndRequestMatcher(notXRequestedWith, new NegatedRequestMatcher(defaultLoginPageMatcher), |
| 512 | + formLoginNotEnabled), new LoginUrlAuthenticationEntryPoint(providerLoginPage)); |
509 | 513 | DelegatingAuthenticationEntryPoint loginEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);
|
510 | 514 | loginEntryPoint.setDefaultEntryPoint(this.getAuthenticationEntryPoint());
|
511 | 515 | return loginEntryPoint;
|
512 | 516 | }
|
513 | 517 |
|
| 518 | + private RequestMatcher getFormLoginNotEnabledRequestMatcher(B http) { |
| 519 | + DefaultLoginPageGeneratingFilter defaultLoginPageGeneratingFilter = http |
| 520 | + .getSharedObject(DefaultLoginPageGeneratingFilter.class); |
| 521 | + Field formLoginEnabledField = (defaultLoginPageGeneratingFilter != null) |
| 522 | + ? ReflectionUtils.findField(DefaultLoginPageGeneratingFilter.class, "formLoginEnabled") : null; |
| 523 | + if (formLoginEnabledField != null) { |
| 524 | + ReflectionUtils.makeAccessible(formLoginEnabledField); |
| 525 | + return (request) -> Boolean.FALSE |
| 526 | + .equals(ReflectionUtils.getField(formLoginEnabledField, defaultLoginPageGeneratingFilter)); |
| 527 | + } |
| 528 | + return AnyRequestMatcher.INSTANCE; |
| 529 | + } |
| 530 | + |
514 | 531 | /**
|
515 | 532 | * Configuration options for the Authorization Server's Authorization Endpoint.
|
516 | 533 | */
|
|
0 commit comments