Fix bugs about connect to Azure AD by proxy - #31213
Conversation
|
API change check API changes are not detected in this pull request. |
1. For RestTemplate used to get access token, only keep 2 converters: FormHttpMessageConverter and OAuth2AccessTokenResponseHttpMessageConverter. Because keep more converters may cause the 2 converters not been used. 2. Return new HashMap() instead of Collections.emptyMap(). Because the returned value needs to add new values, but Collections.emptyMap() does not support add new value.
abfa967 to
5470386
Compare
| List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters(); | ||
| if (notContainsElementOfType(converters, FormHttpMessageConverter.class)) { | ||
| converters.add(new FormHttpMessageConverter()); | ||
| } | ||
| if (notContainsElementOfType(converters, OAuth2AccessTokenResponseHttpMessageConverter.class)) { | ||
| converters.add(new OAuth2AccessTokenResponseHttpMessageConverter()); | ||
| } | ||
| converters.clear(); | ||
| converters.add(new FormHttpMessageConverter()); | ||
| converters.add(new OAuth2AccessTokenResponseHttpMessageConverter()); |
There was a problem hiding this comment.
Why not calling this method public RestTemplateBuilder messageConverters(HttpMessageConverter<?>... messageConverters)?
|
This is not working for me, and indeed does not appear to address the root cause. The problem as far as I can see is that Looking at The initial call this works correctly ( However, on any subsequent call, we call Hence, on the second call, we do Indeed, on each additional request, we call n.b., this is in part due to the fact that It seems a correct solution would be instead to call The same goes for |
|
PR to fix above #31290 |
|
This pull request is protected by Check Enforcer. What is Check Enforcer?Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass. Why am I getting this message?You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged. What should I do now?If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows: What if I am onboarding a new service?Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment: |
1fda10b to
a647731
Compare
|
Hi, @saragluna , @0x006EA1E5 please help to review this PR when you have time. |
…n if jwkResolver is null.
| .map(s -> s.getAttribute(AUTHORIZED_CLIENTS_ATTR_NAME)) | ||
| .map(Object::toString) | ||
| .map(SerializerUtils::deserializeOAuth2AuthorizedClientMap) | ||
| .orElse(Collections.emptyMap()); |
There was a problem hiding this comment.
I wouldn't do this, as it means the execution of an additional lambda, whereas the Collections.emptyMap() method just returns the static java.util.Collections#EMPTY_MAP field (so there is no allocation cost saving which would otherwise justify the orElse).
There was a problem hiding this comment.
@0x006EA1E5
Thank you for your suggestion.
But I have different understanding:
lambdais just a pointer to a method, it will not need extra cost.- In
orElse(doSomething()),doSomething()will always be executed, it's a kind of waste (a method call will costStack Frame). Here is code to verify this:
public class Test {
public static void main(String[] args) {
System.out.println("Start orElse");
String orElseResult = Optional.of("string")
.orElse(doSomething());
System.out.println("orElseResult = " + orElseResult);
System.out.println();
System.out.println("Start orElseGet");
String orElseGetResult = Optional.of("string")
.orElseGet(Test::doSomething);
System.out.println("orElseGetResult = " + orElseGetResult);
}
public static String doSomething() {
System.out.println("doSomething");
return "doSomething";
}
}
There was a problem hiding this comment.
Ah, yes, you are right, it is a method reference so the lambda doesn't need to be created.
When I mentioned cost saving, I meant different scenario which doesn't apply here:
Optional
...
.orElse(new ExpensiveObject(param));
vs
Optional
...
.orElseGet(() -> new ExpensiveObject(param));
Where the second is obviously preferable, even with the extra lambda.
In any case, I suspect that the performance of .orElse(Collections.emptyMap()) will be indistinguishable from that of .orElseGet(Collections::emptyMap) in the real world, and they would both get optimised and inlined to the same thing if executed enough to make any difference.
Thanks for the response, please ignore my change suggestion :)
There was a problem hiding this comment.
Got it. Thank you for your detailed explanation.
| */ | ||
| public MultiValueMap<String, String> getHttpBody(OAuth2AuthorizationCodeGrantRequest request) { | ||
| return null; | ||
| return new LinkedMultiValueMap<>(); |
There was a problem hiding this comment.
My personal preference would be to return an immutable collection here, if possible. Hence my suggestion of new MultiValueMapAdapter<>(Collections.emptyMap());
Maybe even have a static field to return, so there is no repeated allocation.
There was a problem hiding this comment.
Good point. Will accept this suggestion.
|
|
||
| /** | ||
| * Additional default headers information. | ||
| * @return HttpHeaders |
There was a problem hiding this comment.
What do you think about making this public HttpHeaders getHttpHeaders(OAuth2AuthorizationCodeGrantRequest request) {?
This is an abstract class, it seems like it seems like it might be nice to give implementors access to the request parameter
|
Closing this PR, fix one bug by one PR: |

Fix #31191, #31190, #31218
OAuth2AccessTokenResponseClientRestTemplatecontains too many converters. It should only contain 2 converters:FormHttpMessageConverterandOAuth2AccessTokenResponseHttpMessageConverter.Collections.emptyMap().put(..)is not supported.addParametersConverterandaddHeadersConverterexecuted multiple times.RestOperationsis not well-configured when jwkResolver is null.