简体   繁体   English

如何从Spring Boot Feign客户端登录到远程Web服务

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

Hi I am new at feign client and outh2. 嗨,我是新来的假装客户和outh2。 I am using spring boot 1.5.6 . 我正在使用Spring Boot 1.5.6。 I want to login remote webservice which is I didn't write. 我想登录我没有写的远程网络服务。 This webservice uses outh2. 该Web服务使用outh2。 I want to get access token from that web service to my feign client. 我想将访问令牌从该Web服务获取到我的伪装客户端。

My feign client like: 我的假客户喜欢:

@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();
    }

When I send a request I get this error: 当我发送请求时,出现此错误:

com.netflix.client.ClientException: Load balancer does not have available server for client: feignGateAway com.netflix.client.ClientException:负载平衡器没有适用于客户端的服务器:feignGateAway

You will add oauth configuration to feign 您将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;
    }
}

and change feign client to like this 并把假装客户变成这样

@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.

相关问题 如何在 java spring 中配置 Feign 客户端 - How to i configure Feign Client in java spring 我如何从假冒的客户请求中获得经过的时间? - How can I get the elapsed time from a feign client request? 如何使用 Spring Boot feign 客户端进行 Oauth2 身份验证? - How to use Spring Boot feign client for Oauth2 authentication? 如何使用 Feign Client Spring Boot 从另一个服务获取数据(错误:406) - How to get data from another service using Feign Client Spring Boot (ERROR: 406) 如何使用 postman 或 feign 客户端在 Spring 引导中将值设置为 @RequestAttribute - How to set value to @RequestAttribute in Spring boot using postman or feign client 如何在Spring Boot应用程序中的Feign客户端上使用WireMock? - How to use WireMock on a Feign client in a Spring Boot application? 我如何获得Feign客户的名字? - How can I get name of the Feign client? 如何从spring-cloud-netflix-feign在Feign-Client中设置HostnameVerifier - How to set HostnameVerifier in Feign-Client from spring-cloud-netflix-feign 如何在 spring 引导 Java 中动态获取假客户端名称和 url - how to get feign client name and url both dynamically in spring boot Java 如何根据属性文件更改feign客户端url? - How can I change feign client url according to property file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM