package cn.ey.biz.module.external.api.hr.feign.config;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.springframework.context.annotation.Bean;

import feign.Client;
import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
/**
 * Feign 自定义配置
 * @author 86182
 *
 */
public class HrFeignConfiguration {

    @Bean
    public HrFeignClientInterceptor feignFdInterceptor() {
        return new HrFeignClientInterceptor();
    }

    @Bean
    public Logger.Level level() {
        return Logger.Level.FULL;
    }

    @Bean
    public Decoder feignDecoder() {
        return new HrResponseDecoder();
    }
    
    @Bean
    public Feign.Builder feignBuilder(Client feignClient) {
        return Feign.builder().client(feignClient);
    }

//    @Bean
//    public Client client(){
//        return new Client.Default(TrustingSSLSocketFactory.get(), new NoopHostnameVerifier());
//    }
    
    @Bean
    public Client feignClient() throws NoSuchAlgorithmException, KeyManagementException {
		SSLContext ctx = SSLContext.getInstance("SSL");// "SSL"TLSv1.2
		X509TrustManager tm = new X509TrustManager() {

			@Override
			public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public X509Certificate[] getAcceptedIssuers() {
				return new X509Certificate[0];
			}
			
		};
		ctx.init(null, new TrustManager[]{tm}, null);
		return new Client.Default(ctx.getSocketFactory(),new NoopHostnameVerifier());
    }
       
}
