feign 의 url 에 get 인 자 를 추가 하여 검사 기능 을 실현 합 니 다.

2902 단어 SpringCloud
필요:
http 클 라 이언 트 대신 feign 을 사용 하여 http 대 리 를 생 성 할 때 url 마다 검사 서명 의 세 가지 인 자 를 추가 해 야 합 니 다. 예 를 들 어 appkey, sign, 시간 스탬프 등 입 니 다. 작성 방법 은 다음 과 같 습 니 다.
@FeignClient(url = "${url}/xxx",name = "xxxFeign")

이루어지다 
1 클래스 계승 feign. client. Default 를 쓰 고 execute 방법 을 다시 씁 니 다.
import com.js.apiserver.util.endecryption.MD5Util;
import feign.Client;
import feign.Request;
import feign.Response;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

public class MyDefaultClient extends Client.Default {
    @Value("${app_key}")
    private String app_key;
    @Value("${app_secret}")
    private String app_secret;

    public MyDefaultClient() {
        super(null, null);
    }

    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        String timestamp = String.valueOf(System.currentTimeMillis());
        //     SIGN
        String sign = MD5Util.encrypByMd5Jar(timestamp + "#" + app_secret);
        String url = request.url()+"?timestamp="+timestamp+"&app_key="+app_key+"&sign="+sign;
        return super.execute(Request.create(request.httpMethod(),url,request.headers(),request.requestBody()), options);
    }
}

2. 설정 클래스 를 작성 합 니 다.
import feign.Client;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class JsFeignClientAutoConfiguration {

    @Autowired
    private ObjectFactory messageConverters;

    @ConditionalOnMissingBean(name = "myHttpClient")
    @Bean("myHttpClient")
    @Primary
    @ConditionalOnProperty("app_key")
    public Client setClient(){
        return new MyDefaultClient();
    }
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    @Bean
    @Primary
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
}

좋은 웹페이지 즐겨찾기