在单元测试中,我通过REST客户端(通过@RegisterRestClient注释接口(调用@Authenticated端点。如何在REST调用中提供令牌?
接口:
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
@RegisterRestClient
public interface MyAuthenticatedService {
@GET
public void myAuthenticatedCall(@HeaderParam("Authorization") String token);
}
以及呼叫本身:
import javax.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;
public class FoobarService {
@Inject
@RestClient
MyAuthenticatedService myService;
public void foobar(String token) {
myService.myAuthenticatedCall("Bearer " + token); // beware of the space
}
}
为了自动将令牌传递给您的rest客户端,请执行以下操作:
- 将
@RegisterClientHeaders
注释添加到您的界面 - 将
org.eclipse.microprofile.rest.client.propagateHeaders=Authorization
添加到application.properties中(其中Authorization
是请求头密钥名称(