英文:
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("GET /file/getfilesprop")
List<NeosdFile> getfilesprop();
@RequestLine("GET /file/getfiles")
List<String> 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, 'roles[read]' ]
答案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({"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);
}
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("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));
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论