2011-05-10 57 views
0

我正在使用Restlet身份驗證和授權。但是我無法在自定義的註冊器中獲得這些祕密,它是空的。下面是代碼:無法從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

查看最新的代碼,它看起來並沒有在用戶對象上設置密碼。但是,您可以覆蓋MyVerifier類中的驗證(請求,響應)方法來設置它,如下所示:

@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; 
}