3
我试图通过HTTP基本身份验证来验证对我服务器上所有资源的访问权限。目前,我的设置是这样的启动服务器时:Restlet中使用JAXRS应用程序进行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();
}
}
但是,我在尝试访问资源时没有看到任何提示或身份验证要求。我究竟做错了什么?我在编组和解组项目时没有任何问题,我确信我的资源正受到请求的冲击。我不正确的是什么?
这是否飞意味着你调用'attachDefault(authenticator)'调用'((JaxRsApplication)应用程序).setGuard((Authenticator)authenticator);'? – curioustechizen 2013-07-15 06:10:33
没关系 - 我在[这里]找到了我的问题的答案(http://restlet.org/learn/guide/2.0/core/security/authentication/) – curioustechizen 2013-07-15 06:20:14