Skip to content

Commit

Permalink
Use JdkClientHttpRequestFactory for RestTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Aug 19, 2024
1 parent c60393e commit d2ca155
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -99,7 +100,9 @@ public boolean hasPermission(String... permissionTechnicalName) {
public void authorize(HydraAuthorizedEvent event) {
meResult = null;

restTemplate = restTemplateBuilder.additionalMessageConverters(jsonApiMessageConverter)
restTemplate = restTemplateBuilder
.requestFactory(JdkClientHttpRequestFactory.class)
.additionalMessageConverters(jsonApiMessageConverter)
.setReadTimeout(Duration.ofMinutes(5))
.errorHandler(jsonApiErrorHandler)
.rootUri(environmentProperties.getBaseUrl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -46,6 +47,7 @@ public void initialize(EnvironmentProperties environmentProperties) {
@EventListener
public void authorize(HydraAuthorizedEvent event) {
restTemplate = restTemplateBuilder
.requestFactory(JdkClientHttpRequestFactory.class)
.rootUri(environmentProperties.getUserBaseUrl())
.interceptors(oAuthTokenInterceptor)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
Expand All @@ -32,7 +33,10 @@ public TokenService(ApplicationEventPublisher applicationEventPublisher) {

public void prepare(EnvironmentProperties environmentProperties) {
this.environmentProperties = environmentProperties;
this.restTemplate = new RestTemplateBuilder().rootUri(environmentProperties.getOauthBaseUrl()).build();
this.restTemplate = new RestTemplateBuilder()
.requestFactory(JdkClientHttpRequestFactory.class)
.rootUri(environmentProperties.getOauthBaseUrl())
.build();
}

@SneakyThrows
Expand Down

0 comments on commit d2ca155

Please sign in to comment.