Heloo每个人,我都试图通过一个假冒客户端连接到api,我在一个jhipster网关中使用该假冒客户端..我已经在微服务中使用了相同的代码,并且工作正常,这是我编写的代码:



@FeignClient( name = "berrycord" ,url = "https://dev1.digitalberry.fr/bcs-berrycord-direct/")

/**
 * This interface is used to call berryscheduler APIs ,
 * using netflix feign client
 * @param body  host to manage
 */

public interface TraceClientInterface {

    @PostMapping("api/v1/records/")
    public JSONObject sendReport(@RequestBody JSONObject report);

    // @GetMapping(value="/jokes/count")
    // public JSONObject sendReport();

}


@Component
public class UserFeignClientInterceptor implements RequestInterceptor {
    private static final String AUTHORIZATION_HEADER = "Authorization";
    private static final String BEARER = "Bearer";

    @Override
    public void apply(RequestTemplate template) {

        System.out.println("test ========================" +template.request());
        System.out.println("test ========================2" +template.toString());
        System.out.println("test ========================3" +new String(template.body()));

        SecurityUtils.getCurrentUserJWT()
            .ifPresent(s -> template.header(AUTHORIZATION_HEADER,String.format("%s %s", BEARER, s)));


        SecurityUtils.getCurrentUserLogin()
            .ifPresent(user -> template.header("X-Forwarded-User", user));

        SecurityUtils.getCurrentUserAuthorities()
            .ifPresent(authorities -> template.header("X-Forwarded-Role", authorities));

    }
}


/**
     * This service communicates with berryCord to create a send report POST
     * /api/v1/report/ endpoint, is called when creating or updating the host
     * resource
     *
     * @param task
     */
    public JSONObject sendReport(JSONObject report) {

        log.debug("Request to create log report in berrycord ");
        JSONObject rep = new JSONObject() ;
        try {
            log.info("=========== Request to create log report in berrycord 2 " , report);
            rep =  traceClientInterface.sendReport(report);
            log.info("=========== Request to create log report in berrycord 3 " , report);

        } catch (FeignException e) {
            e.getStackTrace();
        }
        return rep;

    }



feign:
    hystrix:
        enabled: false
    client:
        url:
            berryCordUrl: https://dev1.digitalberry.fr/bcs-berrycord-direct/


但是两者之间的联系从未完成,我看不到被调用API的结果。
谁能告诉我我做错了什么..谢谢:) :)

最佳答案

您应该具有以下@Configuration并将@EnableFeignClients添加到Application.java

@Configuration
public class FooConfiguration {

    @Bean
    public UserFeignClientInterceptor userFeignClientInterceptor() {
        return new UserFeignClientInterceptor();
    }
}

关于java - 假冒客户连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62436293/

10-12 01:24