如何使用Spring Boot Feign客户端进行Oauth2认证?

huangapple go评论101阅读模式
英文:

How to use Spring Boot feign client for Oauth2 authentication?

问题

我在本地有一个 OAuth 服务器,并希望从另一个服务使用 Feign 客户端调用它,以对我进行身份验证并提供一些令牌。

这是我在 Postman 中如何传递用户名、密码和授权类型的方式

这是我如何传递基本身份验证详细信息的方式

如何在 Spring Boot 中使用 Feign 客户端实现相同的功能?

英文:

I have an oauth server at localhost and want to call it from another service uisng feign-client for authenticating me and giving me some token.

Here is how i pass my username,password and grant type i postman

Here is how my pass the basic auth details

How to do the same functionality using FeignClient in Spring Boot?

答案1

得分: 0

将@EnableFeignClients注解放置在Spring Boot主应用程序的Java类中。
创建一个Feign客户端接口来调用Web服务:

@FeignClient(value = "service-auth",
    configuration = ClientConfig.class,
    fallbackFactory = ClientFallbackFactory.class,
    url = "http://localhost:10000/oauth/")
public interface GenericAbstractClient {

    @RequestMapping(value="/token",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseVo auth(@RequestBody RequestVo body);
}

以及ClientFallbackFactory类:

@Component
class ClientFallbackFactory implements FallbackFactory<GenericAbstractClient> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientFallbackFactory.class);

    @Override
    public GenericAbstractClient create(Throwable cause) {

        return new GenericAbstractClient() {

            @Override
            public ResponseVo auth(RequestVo body) {
                LOGGER.warn("Hystrix exception", cause.getMessage());
                return null;
            }
        };
    }
}

你的RequestBody可能是一个Java类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RequestVo {

    private String grant_type;
    private String username;
    private String password;
}

以及ResponseVo类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseVo {

    private String access_token;
    private String token_type;
    private String expires_in;
    private String scope;
}

你还可以添加以下配置:

@Configuration
public class ClientConfig {

    private int connectTimeOutMillis = 120000;
    private int readTimeOutMillis = 120000;

    @Bean
    public Request.Options options() {
        return new Request.Options(connectTimeOutMillis, readTimeOutMillis);
    }
}

这是你提供的内容的翻译部分。

英文:

put @EnableFeignClients in the spring boot main application java class .
create after an Feign client interface to call the web service :

    @FeignClient(value = &quot;service-auth&quot;,
        configuration = ClientConfig.class,
        fallbackFactory = ClientFallbackFactory.class,
        url = &quot;http://localhost:10000/oauth/&quot;)
    public interface GenericAbstractClient {


    @RequestMapping(value=&quot;/token&quot;,
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseVo auth(@RequestBody RequestVo body);


}

> and ClientFallbackFactory as :

    @Component
    class ClientFallbackFactory implements FallbackFactory&lt;GenericAbstractClient&gt; {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientFallbackFactory.class);


   

     @Override
      public GenericAbstractClient create(Throwable cause) {

    return new GenericAbstractClient () {


      @Override
      public ResponseVo oauth(RequestVo body) {
        LOGGER.warn(&quot;Hystrix exception&quot;, cause.getMessage());
        return null;
      }

    
    };
}
}

Your RequestBody might be a java class :

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RequestVo {


private String grant_type;
private String username;
private String password;

}

and The ResponseVo class :

    @Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseVo {


private String access_token;
private String token_type;
private String exprires_in;
private String scope;

}

and you can add thise config :

@Configuration
public class ClientConfig {


    private int connectTimeOutMillis = 120000;
    private int readTimeOutMillis = 120000;


    @Bean
    public Request.Options options() {
        return new Request.Options(connectTimeOutMillis, readTimeOutMillis);
    }
}

答案2

得分: 0

请查看我在 https://stackoverflow.com/a/65741386/698471 上关于如何使client_credentials在您的情况下工作的回答,它会对您有所帮助,与您的使用情况相同。

英文:

Check my response on how to get client_credentials working on https://stackoverflow.com/a/65741386/698471, it will help you, it's the same use case

huangapple
  • 本文由 发表于 2020年10月6日 19:31:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64224916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定