【发布时间】:2011-05-03 22:00:53
【问题描述】:
我正在尝试通过 HTTP 基本身份验证来验证对我服务器上所有资源的访问。目前,我的设置在启动服务器时如下所示:
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();
这是我的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;
}
}
这是我的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;
}
}
最后,这是我的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();
}
}
但是,当我尝试访问资源时,我没有看到任何身份验证提示或要求。我究竟做错了什么?我在编组和解组项目方面没有任何问题,我确信我的资源受到请求的影响。我做错了什么?
【问题讨论】:
标签: rest jaxb jax-rs restlet basic-authentication