从 Restlet 1.x 迁移到 2.x

Migrating from Restlet 1.x to 2.x

我正在将 Restlet 从 1.2 迁移到 2.2.3,发现 Guard 现已弃用。我开始使用 ChallengeAuthenticator(HTTP_BASIC 方案)。问题就在这里,我曾经扩展 Guard class 并覆盖方法 checkSecret 来授权应用程序特定的凭据。

public class AgentAuthenticationGuard extends Guard {

public AgentAuthenticationGuard(Context context, ChallengeScheme scheme,
        String realm) throws IllegalArgumentException {
    super(context, scheme, realm);
}

 public boolean checkSecret(Request request, String identifier, char[] secret) {
     return SecurityHelper.authorizeAgent( identifier, new String(secret) );
 }

}

如果我想用 ChallengeRequester 替换 Guard,我应该覆盖哪个方法?认证?我如何获得标识符和秘密?

抱歉跳得太快了。我想我解决了。我如下创建挑战验证器

 private ChallengeAuthenticator createAuthenticator() {
    Context context = getContext();
    ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC;
    String realm = "my-web";

    MapVerifier verifier = new MapVerifier();
    verifier.getLocalSecrets().put("user", "password".toCharArray());

    ChallengeAuthenticator authWithChallenge= new ChallengeAuthenticator(context, challengeScheme, realm) {
        @Override
        protected int beforeHandle(Request request, Response response) {
            ChallengeResponse challengeResponse =  request.getChallengeResponse();
            boolean authorized = SecurityHelper.authorizeAgent( challengeResponse.getIdentifier(), new String(challengeResponse.getSecret()) );
            if(authorized)
            {
                response.setStatus(Status.SUCCESS_OK);
                return CONTINUE;
            }

            logger.info("Invalid Credentials!!!");
            return STOP;

        }
    };

    return authWithChallenge;
}

感谢 https://www.programcreek.com/java-api-examples/index.php?api=org.restlet.security.ChallengeAuthenticator