在restlet中获取客户端证书

时间:2014-09-08 12:56:59

标签: ssl restlet

我设法实现了一个带有bot客户端和服务器认证的https restlet。我可以证明它有效,因为如果我用不受信任的认证通信调用服务器失败。不幸的是,我无法在服务器上找到客户端的证书。我正在使用此代码:

List<Certificate> certs = request.getClientInfo().getCertificates();

但列表为空。我做错了什么?

修改

版本 Restlet-Framework / 2.3m2

2 个答案:

答案 0 :(得分:1)

问题与通过com.sun.httpserver使用默认服务器实现有关。 类org.restlet.engine.connector.HttpExchangeCall应该使用getCertificates()方法返回证书, 但它总是返回null。此类用于org.restlet.engine.connector.HttpsServerHelper 在使用服务器实现com.sun.httpserver时,它又是Restlet框架的帮助器。

要解决这个问题,需要做一些事情。
首先,新课程HttpsExchangeCall

package org.restlet.engine.connector;

import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.List;

import org.restlet.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpsExchange;

/**
 * The default {@link HttpExchangeCall} fails to extract certificates from the SSL connection.
 * This class implements {@link #getCertificates()} to extract certificates.
 */
@SuppressWarnings("restriction")
public class HttpsExchangeCall extends HttpExchangeCall {

    private static final Logger log = LoggerFactory.getLogger(HttpsExchangeCall.class);

    private final HttpsExchange sexchange;

    public HttpsExchangeCall(Server server, HttpExchange exchange) {
        this(server, exchange, true);
    }

    public HttpsExchangeCall(Server server, HttpExchange exchange, boolean confidential) {
        super(server, exchange, confidential);
        if (exchange instanceof HttpsExchange) {
            sexchange = (HttpsExchange) exchange;
        } else {
            sexchange = null;
        }
    }

    @Override
    public List<Certificate> getCertificates() {

        if (sexchange == null) {
            log.debug("Cannot extract peer certificates from unsecure connection.");
            return null;
        }
        Certificate[] certs = null;
        try {
            certs = sexchange.getSSLSession().getPeerCertificates();
            if (log.isDebugEnabled()) {
                log.debug("Found " + (certs == null ? "no" : Integer.toString(certs.length)) + " peer certificate(s).");
            }
        } catch (Exception e) {
            log.debug("Unable to find peer certificates - " + e);
        }
        List<Certificate> lcerts = null;
        if (certs != null) {
            lcerts = new ArrayList<Certificate>();
            for (int i = 0; i < certs.length; i++) {
                lcerts.add(certs[i]);
            }
        }
        return lcerts;
    }

}

然后是HttpsServerHelper的副本 重命名为HttpsServerHelper2,修改了一行。更换线
HttpsServerHelper.this.handle(new HttpExchangeCall(getHelped(),
与线:
HttpsServerHelper2.this.handle(new HttpsExchangeCall(getHelped(),

此助手需要注册:
Engine.getInstance().getRegisteredServers().add(new HttpsServerHelper2(null));
创建Server现在变得非常明确:

Component component = new Component();
Server server = new Server(
        (Context) null, Arrays.asList(Protocol.HTTPS),
        (String) null, Constants.PORT_TEST, component.getServers().getNext(), 
        HttpsServerHelper2.class.getName()
    );
component.getServers().add(server);

我希望Restlet自己的HttpExchangeCall将被更新以提取证书: 这是一个小修复,可以节省解决问题所需的大量不必要的代码。
同时,您可以找到所有源代码(使用Restlet 2.3.4)和一个工作示例 restlet-clientcert Github项目。

答案 1 :(得分:-1)

此方法应该提供您要查找的内容:request.getClientInfo()。getCertificates() http://restlet.com/learn/javadocs/2.3/jse/api/org/restlet/data/ClientInfo.html

您还可以检索密码套件