使用 JAXRS 应用程序在 Restlet 中进行 HTTP 基本身份验证?

发布于 2024-11-04 21:17:19 字数 2116 浏览 0 评论 0原文

我正在尝试通过 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();
    }
}

但是,我当我尝试访问资源时,没有看到任何身份验证提示或要求。我做错了什么?我在编组和解组项目方面没有任何问题,并且我确信我的资源正在受到请求的影响。我什么地方做得不对?

I'm trying to authenticate access to all resources on my server via HTTP Basic authentication. Currently, my setup looks like this when starting the server:

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();

Here's my 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;
    }
}

Here's my 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;
    }
}

Finally, here's my 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();
    }
}

However, I'm not seeing any prompt or requirement for authentication when I try to access the resource. What am I doing wrong? I'm not having any issues with marshalling and unmarshalling items and I'm sure that my resource is getting hit with requests. What am I not doing right?

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

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

发布评论

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

评论(1

残龙傲雪 2024-11-11 21:17:19

根据 JaxRsApplication 的 JavaDocs,可以在开始之前使用以下代码设置身份验证器事情做好了:

((JaxRsApplication)application).setGuard((Authenticator)authenticator);

这样做,所有请求都将通过您的身份验证器:)

According to the JavaDocs for JaxRsApplication, an authenticator can be set using the following code before starting things up:

((JaxRsApplication)application).setGuard((Authenticator)authenticator);

Do that, and all requests will fly through your authenticator :)

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