我正在使用Restlet身份验证和授权。但我无法在我的自定义注册器中获取秘密,它是空的。这是代码:
TestApplication.java
@Override
public Restlet createInboundRoot()
{
Router rootRouter = new Router(getContext());
Router pubRouter = new Router(getContext());
pubRouter.attach("/test", TestResource.class);
RoleAuthorizer ra = new RoleAuthorizer();
ra.getAuthorizedRoles().add(MyEnroler.PUBLISHER);
ra.setNext(pubRouter);
rootRouter.attach("/publish", ra);
ChallengeAuthenticator guard =
new ChallengeAuthenticator(getContext(),ChallengeScheme.HTTP_BASIC, "my realm");
guard.setVerifier(new MyVerifier());
guard.setEnroler(new MyEnroler());
guard.setNext(rootRouter);
return guard;
}
Enroller.java
public class MyEnroler implements Enroler
{
public final static Role PUBLISHER = new Role("publisher", "publisher");
public void enrole(ClientInfo clientInfo)
{
System.out.println(clientInfo.getUser().getIdentifier());
System.out.println(new String(theClientInfo.getUser().getSecret()));
}
}
theClientInfo.getUser()。getSecret()始终返回null
ClientResource.java
public static void main(String[] args) throws Exception
{
// Create a HTTP Server listening to the port 8182
Component c = new Component();
c.getServers().add(Protocol.HTTP, 8182);
c.getDefaultHost().attach("/myapp", new TestApplication());
c.start();
ClientResource pubResource =
new ClientResource("http://localhost:8182/myapp/publish/test");
ChallengeResponse cr =
new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "scott", "tiger");
pubResource.setChallengeResponse(cr);
pubResource.get()
}
有人知道为什么吗?
谢谢!
答案 0 :(得分:0)
查看最新代码,似乎没有在User对象上设置秘密。但是,您可以覆盖MyVerifier类中的verify(请求,响应)方法来设置它,如下所示:
@Override
public int verify(Request request, Response response) {
int result = super.verify(request, response);
if (result == RESULT_VALID) {
request.getClientInfo().getUser().setSecret(getSecret(request, response));
}
return result;
}