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

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

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服务:

  1. @FeignClient(value = "service-auth",
  2. configuration = ClientConfig.class,
  3. fallbackFactory = ClientFallbackFactory.class,
  4. url = "http://localhost:10000/oauth/")
  5. public interface GenericAbstractClient {
  6. @RequestMapping(value="/token",
  7. method = RequestMethod.POST,
  8. consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
  9. produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  10. ResponseVo auth(@RequestBody RequestVo body);
  11. }

以及ClientFallbackFactory类:

  1. @Component
  2. class ClientFallbackFactory implements FallbackFactory<GenericAbstractClient> {
  3. private static final Logger LOGGER = LoggerFactory.getLogger(ClientFallbackFactory.class);
  4. @Override
  5. public GenericAbstractClient create(Throwable cause) {
  6. return new GenericAbstractClient() {
  7. @Override
  8. public ResponseVo auth(RequestVo body) {
  9. LOGGER.warn("Hystrix exception", cause.getMessage());
  10. return null;
  11. }
  12. };
  13. }
  14. }

你的RequestBody可能是一个Java类:

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @Builder
  5. public class RequestVo {
  6. private String grant_type;
  7. private String username;
  8. private String password;
  9. }

以及ResponseVo类:

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @Builder
  5. public class ResponseVo {
  6. private String access_token;
  7. private String token_type;
  8. private String expires_in;
  9. private String scope;
  10. }

你还可以添加以下配置:

  1. @Configuration
  2. public class ClientConfig {
  3. private int connectTimeOutMillis = 120000;
  4. private int readTimeOutMillis = 120000;
  5. @Bean
  6. public Request.Options options() {
  7. return new Request.Options(connectTimeOutMillis, readTimeOutMillis);
  8. }
  9. }

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

英文:

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

  1. @FeignClient(value = &quot;service-auth&quot;,
  2. configuration = ClientConfig.class,
  3. fallbackFactory = ClientFallbackFactory.class,
  4. url = &quot;http://localhost:10000/oauth/&quot;)
  5. public interface GenericAbstractClient {
  6. @RequestMapping(value=&quot;/token&quot;,
  7. method = RequestMethod.POST,
  8. consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
  9. produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  10. ResponseVo auth(@RequestBody RequestVo body);
  11. }

> and ClientFallbackFactory as :

  1. @Component
  2. class ClientFallbackFactory implements FallbackFactory&lt;GenericAbstractClient&gt; {
  3. private static final Logger LOGGER = LoggerFactory.getLogger(ClientFallbackFactory.class);
  4. @Override
  5. public GenericAbstractClient create(Throwable cause) {
  6. return new GenericAbstractClient () {
  7. @Override
  8. public ResponseVo oauth(RequestVo body) {
  9. LOGGER.warn(&quot;Hystrix exception&quot;, cause.getMessage());
  10. return null;
  11. }
  12. };
  13. }
  14. }

Your RequestBody might be a java class :

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @Builder
  5. public class RequestVo {
  6. private String grant_type;
  7. private String username;
  8. private String password;

}

and The ResponseVo class :

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @Builder
  5. public class ResponseVo {
  6. private String access_token;
  7. private String token_type;
  8. private String exprires_in;
  9. private String scope;

}

and you can add thise config :

  1. @Configuration
  2. public class ClientConfig {
  3. private int connectTimeOutMillis = 120000;
  4. private int readTimeOutMillis = 120000;
  5. @Bean
  6. public Request.Options options() {
  7. return new Request.Options(connectTimeOutMillis, readTimeOutMillis);
  8. }
  9. }

答案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:

确定