首页 > 解决方案 > quarkus:如何通过休息客户端提供令牌

问题描述

在单元测试中,我通过 REST 客户端(通过 @RegisterRestClient 注释接口)调用 @Authenticated 端点。如何在 REST 调用中提供令牌?

标签: restsecuritytestingtokenquarkus

解决方案


界面:

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
    }
}

推荐阅读