Basic HTTP authentication in Restlet using JAXRS app?

I am trying to authenticate access to all resources on my server using HTTP authentication. Currently, my installation looks like this when you start the server:

this.component = new Component();
this.server = this.component.getServers().add(Protocol.HTTP, 8118);

JaxRsApplication application = new JaxRsApplication(component.getContext()
        .createChildContext());
application.add(new RestletApplication());

ChallengeAuthenticator authenticator = new ChallengeAuthenticator(
        this.component.getContext(), 
        ChallengeScheme.HTTP_BASIC, "test");        
authenticator.setVerifier(new ApplicationVerifier());

this.component.getDefaultHost().attachDefault(authenticator);
this.component.getDefaultHost().attach(application);
this.component.start();

Here's mine RestletApplication:

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class RestletApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(TestResource.class);
        return classes;
    }
}

Here's mine ApplicationVerifier:

import java.util.HashMap;
import java.util.Map;

import org.restlet.security.LocalVerifier;

public class ApplicationVerifier extends LocalVerifier {

    private Map<String, char[]> localSecrets = new HashMap<String, char[]>();

    public ApplicationVerifier() {
        this.localSecrets.put("username", "password".toCharArray());
    }

    @Override
    public char[] getLocalSecret(String key) {
        if (this.localSecrets.containsKey(key))
            return this.localSecrets.get(key);

        return null;
    }
}

Finally here is my TestResource:

import java.util.*;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("test")
public class TestResource {

    @GET
    @Path("list")
    public TestList getTestList() {
        return makeTestList();
    }
}

However, when I try to access the resource, I don’t see the prompts or requirements for authentication. What am I doing wrong? I have no problem sorting and unpacking items, and I’m sure that my resource falls into requests. What am I not doing right?

+3
source share
1 answer

JavaDocs JaxRsApplication :

((JaxRsApplication)application).setGuard((Authenticator)authenticator);

, :)

+3

All Articles