简体   繁体   English

Restlet客户端使用身份验证密钥

[英]Restlet client use authentication key

Using restlet JEE 2.3.2. 使用restlet JEE 2.3.2。

I have a client id and secret to interact with the server restful API. 我有一个客户端ID和与服务器Restful API交互的秘密。 Submitting that info gets me back an authorization key that must be used for subsequent request. 提交该信息后,我将获得必须用于后续请求的授权密钥。 In curl, I can make queries using that key and can get data back: 在curl中,我可以使用该键进行查询并获取数据:

curl -XGET "Authorization c79cec57-a52f-4e04-f3ca-55ea2a202114" "https://some/restful/endpoint"

How do I set my client resource to submit that authorization key? 如何设置我的客户资源以提交该授权密钥? The online docs doesn't seem to cover this scenario. 在线文档似乎没有涵盖这种情况。

if the scheme is not important, you can use a "Custom" scheme, (as it is mandatory in HTTP specification"). In order to avoid the warning "scheme is not supported by restlet engine", just register one, as follow: 如果该方案不重要,则可以使用“自定义”方案(因为它在HTTP规范中是必需的)。为避免警告“ restlet引擎不支持该方案”,只需注册一个,如下所示:

You can achieve what you need using a "custom" scheme, as follow. 您可以使用“自定义”方案来实现所需的目标,如下所示。

    // Declare a custom Authenticator helper, if it is not standard
    Engine.getInstance().getRegisteredAuthenticators().add(new AuthenticatorHelper(ChallengeScheme.CUSTOM, true, false) {});

    // set up the reusable challenge response
    ChallengeResponse cred = new ChallengeResponse(ChallengeScheme.CUSTOM);
    cred.setRawValue("12344");

    ClientResource cr = new ClientResource("http://localhost:8183/");
    cr.setChallengeResponse(cred);
    cr.get();

If you want an empty scheme, you can do as follow: 如果您想要一个空方案,可以执行以下操作:

    ChallengeResponse cred = new ChallengeResponse(new ChallengeScheme("",""));
    cred.setRawValue("12345");

In this case, I think that you can use challenge response as described since such feature builds the Authorization header using format Authorization: Scheme ChallengeResponseContent : 在这种情况下,我认为您可以使用描述的质询响应,因为该功能使用格式Authorization: Scheme ChallengeResponseContent来构建Authorization标头:

ClientResource resource = new ClientResource(resouceURL);
String token = "myToken";
ChallengeResponse cr = new ChallengeResponse(
             ChallengeScheme.HTTP_OAUTH_BEARER);
cr.setRawValue(token);
resource.setChallengeResponse(cr);
(...)

As a matter of fact, Restlet requires a challenge scheme that will be added before the token (or something else) within the value of the header Authorization . 实际上,Restlet需要一个挑战方案,该挑战方案将在标头Authorization值内的令牌(或其他事物)之前添加。 See extract from class AuthenticatorUtils#formatRequest : 请参见类AuthenticatorUtils#formatRequest摘录:

public static String formatRequest(ChallengeRequest challenge,
        Response response, Series<Header> httpHeaders) {
    String result = null;

    if (challenge == null) {
        Context.getCurrentLogger().warning(
                "No challenge response to format.");
    } else if (challenge.getScheme() == null) {
        Context.getCurrentLogger().warning(
                "A challenge response must have a scheme defined.");
    } else if (challenge.getScheme().getTechnicalName() == null) {
        Context.getCurrentLogger().warning(
                "A challenge scheme must have a technical name defined.");
    } else {
        ChallengeWriter cw = new ChallengeWriter();
        cw.append(challenge.getScheme().getTechnicalName()).appendSpace();
        int cwInitialLength = cw.getBuffer().length();

        if (challenge.getRawValue() != null) {
            cw.append(challenge.getRawValue());
        } else {
    (...)

In your case, I think that you need to build the header Authorization by yourself as described below: 在您的情况下,我认为您需要按如下所述Authorization构建标题Authorization

ClientResource resource = new ClientResource(resouceURL);
String token = "myToken";
resource.getRequest().getHeaders().add("Authorization", token);
resource.get();

You can also implement a custom client resource for your needs in order to automatically apply the token: 您还可以根据需要实现自定义客户端资源,以便自动应用令牌:

public class ProtectedClientResource extends ClientResource {
    private String token;

    public ProtectedClientResource(String uri) {
        super(uri);
    }

    @Override
    public Response handleOutbound(Request request) {
        if (token!=null) {
            request.getHeaders().add("Authorization", token);
        }
        return super.handleOutbound(request);
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

Hope it helps you, Thierry 希望对您有帮助,蒂埃里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM