无法从 Restlet Authorizer 获取机密

发布于 2024-11-05 19:08:43 字数 1787 浏览 1 评论 0原文

我正在使用 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()

}

有人知道为什么吗?

谢谢!

I am using Restlet Authentication and Authorization. But I can't get the secrets in my customized enroller, it is null. Here is the code:

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() always return 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()

}

Does anybody know why?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-11-12 19:08:43

查看最新的代码,似乎没有在 User 对象上设置秘密。但是,您可以重写 MyVerifier 类中的 verify(request, response) 方法来设置它,如下所示:

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

Looking through the latest code, it does not appear that the secret is ever set on the User object. However, you can override the verify(request, response) method in your MyVerifier class to set it, like so:

@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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文