Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration for the authorization http client #4392

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.provectus.kafka.ui.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("httpclient")
@Data
public class HttpClientProperties {

Integer connectTimeoutMillis;
Boolean socketKeepAlive;
Integer tcpKeepIdle;
Integer tcpKeepInterval;
Integer tcpKeepCount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.provectus.kafka.ui.config;

import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioChannelOption;
import jdk.net.ExtendedSocketOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
import org.springframework.security.oauth2.client.endpoint.WebClientReactiveAuthorizationCodeTokenResponseClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

@Configuration
public class SecurityAuthConfig {

public static class ConfigurableReactiveAuthClient extends WebClientReactiveAuthorizationCodeTokenResponseClient {

public ConfigurableReactiveAuthClient(HttpClient httpClient) {
var connector = new ReactorClientHttpConnector(httpClient);
setWebClient(WebClient.builder().clientConnector(connector).build());
}
}

@Bean
public ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authClient(
HttpClientProperties properties) {
HttpClient httpClient = httpClientWithProperties(properties);
return new ConfigurableReactiveAuthClient(httpClient);
}

private HttpClient httpClientWithProperties(HttpClientProperties properties) {
HttpClient httpClient = HttpClient.create();
Integer connectTimeoutMillis = properties.getConnectTimeoutMillis();
Boolean socketKeepAlive = properties.getSocketKeepAlive();
if (connectTimeoutMillis != null) {
httpClient = httpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis);
}
Integer tcpKeepIdle = properties.getTcpKeepIdle();
Integer tcpKeepInterval = properties.getTcpKeepInterval();

if (socketKeepAlive != null) {
httpClient = httpClient.option(ChannelOption.SO_KEEPALIVE, socketKeepAlive);
}
if (tcpKeepIdle != null) {
httpClient = httpClient.option(NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPIDLE), tcpKeepIdle);
}
if (tcpKeepInterval != null) {
httpClient = httpClient.option(NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPINTERVAL), tcpKeepInterval);
}
Integer tcpKeepCount = properties.getTcpKeepCount();
if (tcpKeepCount != null) {
httpClient = httpClient.option(NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPCOUNT), tcpKeepCount);
}
return httpClient;
}

}
Loading