All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.web.remote.WebRemoteRestTemplateAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.web.remote;

import java.io.File;
import java.util.List;

import javax.net.ssl.SSLException;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.Netty4ClientHttpRequestFactory;
import org.zodiac.autoconfigure.web.condition.ConditionalOnWebRemoteEnabled;

import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

@SuppressWarnings("deprecation")
@SpringBootConfiguration
@ConditionalOnWebRemoteEnabled
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(value = {org.springframework.web.client.RestTemplate.class})
public class WebRemoteRestTemplateAutoConfiguration {

    private final WebRemoteProperties webRemoteProperties;

    public WebRemoteRestTemplateAutoConfiguration(WebRemoteProperties webRemoteProperties) {
        this.webRemoteProperties = webRemoteProperties;
    }

    /*
     * Clearly specify OpenSSL, because jdk8 may have performance problems, See:
     * https://www.cnblogs.com/wade-luffy/p/6019743.html#_label1
     * 
     * @see {@link io.netty.handler.ssl.ReferenceCountedOpenSslContext}
     */
    // @Bean
    // @ConditionalOnMissingBean
    protected SslContext sslContext() throws SSLException {
        org.zodiac.core.httpclient.config.HttpClientSslInfo ssl = webRemoteProperties.getSsl();
        List ciphers = ssl.getCiphers() == null ? org.zodiac.core.httpclient.config.HttpClientSslInfo.DEFAULT_CIPHERS : ssl.getCiphers();
        return SslContextBuilder.forServer(new File(ssl.getKeyCertChainFile()), new File(ssl.getKeyFile()))
                .sslProvider(SslProvider.OPENSSL).ciphers(ciphers).clientAuth(ClientAuth.REQUIRE)
                .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    }

    @Bean
    @ConditionalOnMissingBean
    protected ClientHttpRequestFactory netty4ClientHttpRequestFactory(/* , SslContext sslContext */) {
        Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
        factory.setReadTimeout(webRemoteProperties.getReadTimeoutMills());
        factory.setConnectTimeout(webRemoteProperties.getConnectTimeoutMills());
        factory.setMaxResponseSize(webRemoteProperties.getMaxResponseSize());
        // factory.setSslContext(sslContext);
        return factory;
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.springframework.web.client.RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new org.springframework.web.client.RestTemplate(factory);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy