Spring-Boot客户端身份验证配置。

时间:2015-04-14 18:29:44

标签: ssl spring-boot keystore restful-authentication

首先,我一般都是Spring-Boot和SSL的新手,但我花了几天时间研究,基本上是在尝试使用客户端身份验证配置一个简单的Spring-Boot应用程序。

我已经设置了这样的连接器:

private Connector createSslConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
    try {
        File keystore = getKeyStoreFile();
        File truststore = keystore;
        connector.setScheme("https");
        connector.setSecure(true);
        connector.setPort(sslPort);
        protocol.setSSLEnabled(true);
        protocol.setKeystoreFile(keystore.getAbsolutePath());
        protocol.setKeystorePass("changeit");
        protocol.setTruststoreFile(truststore.getAbsolutePath());
        protocol.setTruststorePass("changeit");
        protocol.setKeyAlias("apitester");
        protocol.setClientAuth("need");
        return connector;
    }
    catch (IOException ex) {
        throw new IllegalStateException("cant access keystore: [" + "keystore"
                + "] or truststore: [" + "keystore" + "]", ex);
    }
}

一个看起来像这样的控制器:

@RequestMapping("/test/{identifier}")
@ResponseBody
ResponseEntity<String> test(HttpServletRequest request, @PathVariable String identifier) {
    return new ResponseEntity<String>("hello: " + identifier, HttpStatus.OK)
}

但是,一旦我启动我的应用程序,我就可以使用浏览器导航到localhost:sslport / hello / test / xxxx并获得没有加载任何类型的客户端证书的响应。我原本希望获得客户证书。

1 个答案:

答案 0 :(得分:0)

Spring启动时默认使用tomcat(嵌入式)Web容器。

在调用tomcat doc时,我们必须将其设置为 true ,以便在接受连接之前强制从客户端传播有效证书链 。设置想要将允许客户提供证书,但不是绝对必需的。

我怀疑“需要”是否对容器有任何意义。

 protocol.setClientAuth("need");