【问题标题】:HTTP Basic Authentication in Restlet with a JAXRS Application?带有 JAXRS 应用程序的 Restlet 中的 HTTP 基本身份验证?
【发布时间】:2011-05-03 22:00:53
【问题描述】:

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

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

【问题讨论】:

    标签: rest jaxb jax-rs restlet basic-authentication


    【解决方案1】:

    根据JaxRsApplication 的JavaDocs,可以在启动之前使用以下代码设置身份验证器:

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

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

    【讨论】:

    • 这是否意味着您替换attachDefault(authenticator)的调用与对((JaxRsApplication)application).setGuard((Authenticator)authenticator);的调用?
    • 没关系 - 我找到了我的问题的答案here
    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多