簡體   English   中英

如何從Spring Boot Feign客戶端登錄到遠程Web服務

[英]How can I login to remote webservice from spring boot feign client

嗨,我是新來的假裝客戶和outh2。 我正在使用Spring Boot 1.5.6。 我想登錄我沒有寫的遠程網絡服務。 該Web服務使用outh2。 我想將訪問令牌從該Web服務獲取到我的偽裝客戶端。

我的假客戶喜歡:

@FeignClient(name = "feignclient", url ="BASE_URL" )

  public interface FeignGateAway{
        @RequestMapping(method = RequestMethod.POST, value = "BASE_URL/oauth/token?"
                + "client_id=client_id"
                + "&client_secret=client_sercret"
                + "&username=email"
                + "&password=password"
                + "&grant_type=password"
                + "&redirect_uri=urn:ietf:wg:oauth:2.0:oob")
        Object login();
    }

當我發送請求時,出現此錯誤:

com.netflix.client.ClientException:負載平衡器沒有適用於客戶端的服務器:feignGateAway

您將oauth配置添加到偽裝中

package org.roshan.ihs.client;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.core.annotation.AliasFor;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@FeignClient
public @interface AuthorizedFeignClient {

    @AliasFor(annotation = FeignClient.class, attribute = "name")
    String name() default "";

    /**
     * A custom <code>@Configuration</code> for the feign client.
     *
     * Can contain override <code>@Bean</code> definition for the pieces that
     * make up the client, for instance {@link feign.codec.Decoder},
     * {@link feign.codec.Encoder}, {@link feign.Contract}.
     *
     * @see FeignClientsConfiguration for the defaults
     */
    @AliasFor(annotation = FeignClient.class, attribute = "configuration")
    Class<?>[] configuration() default OAuth2InterceptedFeignConfiguration.class;

    /**
     * An absolute URL or resolvable hostname (the protocol is optional).
     */
    String url() default "";

    /**
     * Whether 404s should be decoded instead of throwing FeignExceptions.
     */
    boolean decode404() default false;

    /**
     * Fallback class for the specified Feign client interface. The fallback class must
     * implement the interface annotated by this annotation and be a valid spring bean.
     */
    Class<?> fallback() default void.class;

    /**
     * Path prefix to be used by all method-level mappings. Can be used with or without
     * <code>@RibbonClient</code>.
     */
    String path() default "";
}





@Configuration
public class OAuth2InterceptedFeignConfiguration {

    private JHipsterProperties jHipsterProperties;

    private LoadBalancerClient loadBalancerClient;

    @Bean(name = "oauth2RequestInterceptor")
    public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
        if (loadBalancerClient != null) {
            jHipsterProperties.getSecurity().getClientAuthorization().setLoadBalancerClient(loadBalancerClient);
        }
        return new OAuth2FeignRequestInterceptor(
            new DefaultOAuth2ClientContext(), jHipsterProperties.getSecurity().getClientAuthorization()
        );
    }

    @Inject
    public void setjHipsterProperties(JHipsterProperties jHipsterProperties) {
        this.jHipsterProperties = jHipsterProperties;
    }

    @Autowired(required = false)
    public void setLoadBalancerClient(LoadBalancerClient loadBalancerClient) {
        this.loadBalancerClient = loadBalancerClient;
    }
}

並把假裝客戶變成這樣

@AuthorizedFeignClient(name = "notification-service")
public interface NotificationServiceClient {

    @RequestMapping(path = "notifications/email/send", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void sendMail(@RequestParam("to") String to, @RequestParam("subject") String subject, @RequestParam("body") String body);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM