如何在客户端上配置FEIGN,使用SEEDSTACK框架对REST服务进行身份验证?

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

How to configure FEIGN on the client to authenticate against a REST service using SEEDSTACK framework?

问题

我想从我的客户端应用程序中使用 FEIGN 和 SEEDSTACK 调用一个 REST web 服务。这个 web 服务也是使用 SEEDSTACK 开发的,配置了以下身份验证方法:"filters: [ authcBasic ]"

**如何配置或编写客户端以正确进行身份验证?如何传递用户和密码信息?**

客户端的 FEIGNAPI 类:

```java
@FeignApi
public interface neosdServer {

    @RequestLine("GET /file/getfilesprop")
    List<NeosdFile> getfilesprop();

    @RequestLine("GET /file/getfiles")
    List<String> getfiles();
}

客户端的 APPLICATION.YAML 配置:

feign:
  endpoints:
    neosdClient.interfaces.rest.neosdServer: 
      baseUrl: http://localhost:8080
      encoder: feign.jackson.JacksonEncoder
      decoder: feign.jackson.JacksonDecoder

服务端的 APPLICATION.YAML 配置:

web:
  urls:
    -
      pattern: /file/getfiles
      filters: [ authcBasic, 'roles[read]' ]
英文:

I would like to call a REST web service from my client application using FEIGN and SEEDSTACK. The web service, which is developed with SEEDSATCK too, is configured with the following authentication method: "filters: [ authcBasic ]"

How to configure or program the client to get the authentication right? How to pass the USER and PASSWORD information?

client FEIGNAPI class:

@FeignApi
public interface neosdServer {

    @RequestLine(&quot;GET /file/getfilesprop&quot;)
    List&lt;NeosdFile&gt; getfilesprop();

    @RequestLine(&quot;GET /file/getfiles&quot;)
    List&lt;String&gt; getfiles();
}

client APPLICATION.YAML

feign:
  endpoints:
    neosdClient.interfaces.rest.neosdServer: 
      baseUrl: http://localhost:8080
      encoder: feign.jackson.JacksonEncoder
      decoder: feign.jackson.JacksonDecoder

server APPLICATION.YAML

  web:
    urls:
      -
        pattern: /file/getfiles
        filters: [ authcBasic, &#39;roles[read]&#39; ]   

答案1

得分: 2

目前的SeedStack集成尚不支持在Feign构建器上配置拦截器。相反,要进行身份验证,您可以在Feign接口中使用@Headers注解指定标头(以下是基本身份验证的示例):

@FeignApi
@Headers({"Authorization: Basic {credentials}"})
public interface neosdServer {

    @RequestLine("GET /file/getfilesprop")
    List<NeosdFile> getfilesprop(@Param("credentials") String credentials);

    @RequestLine("GET /file/getfiles")
    List<String> getfiles(@Param("credentials") String credentials);
}

请注意,@Headers也可以用于单个方法。

然后,您将不得不将凭据作为方法参数传递。一个示例实现,其中凭据来自您的应用配置,可能如下:

public class MyClass {
    @Configuration("myApp.credentials.user")
    private String username;
    @Configuration("myApp.credentials.password")
    private String password;
    @Inject
    private NeoSdClient client;

    public void myMethod() {
        List<String> files = client.getFiles(encodeCredentials());
    }

    private String encodeCredentials() {
        return BaseEncoding
                .base64()
                .encode((username + ":" + password)
                        .getBytes(Charsets.UTF_8));
    }
}

我在Feign附加库的存储库上创建了一个问题,以跟踪拦截器支持的实现:https://github.com/seedstack/feign-addon/issues/4。

英文:

The current SeedStack integration doesn't support configuring interceptors on feign builders for now. Instead, to do authentication you can specify a header in your Feign interface with the @Headers annotation (example for basic authentication):

@FeignApi
@Headers({&quot;Authorization: Basic {credentials}&quot;})
public interface neosdServer {

    @RequestLine(&quot;GET /file/getfilesprop&quot;)
    List&lt;NeosdFile&gt; getfilesprop(@Param(&quot;credentials&quot;) String credentials);

    @RequestLine(&quot;GET /file/getfiles&quot;)
    List&lt;String&gt; getfiles(@Param(&quot;credentials&quot;) String credentials);
}

Note that @Headers can also be used on individual methods.

You will then have to pass the credentials as method parameter. An example implementation, with credentials coming from your application configuration, coudl be:

public class MyClass {
    @Configuration(&quot;myApp.credentials.user&quot;)
    private String username;
    @Configuration(&quot;myApp.credentials.password&quot;)
    private String password;
    @Inject
    private NeoSdClient client;

    public void myMethod() {
        List&lt;String&gt; files = client.getFiles(encodeCredentials());
    }

    private String encodeCredentials() {
        return BaseEncoding
                .base64()
                .encode((username + &quot;:&quot; + password)
                        .getBytes(Charsets.UTF_8));
    }
}

I created an issue on the Feign add-on repository to track the implementation of interceptor support: https://github.com/seedstack/feign-addon/issues/4.

huangapple
  • 本文由 发表于 2020年1月30日 18:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984083.html
匿名

发表评论

匿名网友

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

确定