使用.p12证书验证Selenium WebDriver(Java)

问题描述 投票:0回答:1

我花了几天时间寻找解决方案。

我有一个项目,我试图创建一个Selenium WebDriver测试套件。本网站旨在通过双因素身份验证进行保护。这两个因素将用于提供可以与服务器进行身份验证的证书。

目前,我们已经拥有它,以便我们使用以“https://”开头的URL访问服务器。当我们进入网址(在Firefox中)时,我们会看到一个标有“用户识别请求”的弹出窗口,其下拉标签为“选择要作为标识显示的证书:”。

之前,我通过转到选项 - >隐私和安全 - >证书 - >查看证书,选择“您的证书”选项卡,单击“导入”,浏览到“client1”,添加了证书(标记为“client1.p12”)。 p12“文件并输入密码。通过这样做,我现在可以在“选择要呈现的证书...”下拉列表中看到相应的证书。

我的问题是如何设置Selenium WebDriver来选择证书。上面描述的弹出窗口是Windows组件(不是html),因此我不能简单地单击并选择证书。我也试图使用geckodriver的无头选项。

我能找到的最佳解决方案包括以下内容:

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

capabilities.setCapability("ssl-client-certificate-file", "<path-to-client1.p12>");
capabilities.setCapability("ssl-client-key-passphrase", "<password>");
WebDriver driver = new FirefoxDriver(capabilities);
driver.get(<url>);

不幸的是,我在最后一行得到了“org.openqa.selenium.WebDriverException”,“driver.get();”

authentication firefox selenium-webdriver ssl-certificate
1个回答
0
投票

我找到了部分解决方案。在其他情况下,它可能是我所需要的全部,但是,由于我将在最后描述的原因,这里是不够的。

这个问题有两个方面。首先,我需要设置Selenium来接受服务器的证书。其次,我需要让Selenium将.p12证书提供给服务器。

要接受服务器的证书,我做了类似以下的事情:

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
. . . 
FirefoxOptions firefoxOptions = new FirefoxOptions();
. . . 
firefoxOptions.addCapabilities(capabilities);
. . .
driver = new FirefoxDriver(firefoxOptions);
. . . 
driver.get(nbisURL);

要将.p12证书发送到服务器,我使用了firefox配置文件。部分程序如下所述:https://seleniumbycharan.wordpress.com/2015/07/12/how-to-create-custom-firefox-profile-in-selenium-webdriver/

本质上,我创建了一个配置文件(“eAgency-Client1”),如本文所述,它导致了一个开放的Firefox浏览器。在该浏览器中,我按照我在原始帖子中描述的方式设置证书。然后我设置selenium来使用该配置文件:

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("eAgency-Client1");
ffProfile.setPreference("security.default_personal_cert", "Select Automatically");
. . . 
FirefoxOptions firefoxOptions = new FirefoxOptions();
. . . 
firefoxOptions.setProfile(ffProfile);
. . .
driver = new FirefoxDriver(firefoxOptions);

因此,隔离上述两个片段会产生我的解决方案。

我现在遇到的问题是我似乎无法在其他机器上使用该配置文件,因此这个Selenium测试套件不可移植。

我在Jenkins运行测试套件。 Jenkins运行在没有GUI的CentOS服务器上。我将“eAgency-Client1”配置文件从我的本地Windows机器复制到CentOS服务器中的正确位置,并相应地修改了CentOS上的profiles.ini文件。

(我松散地遵循这里给出的建议http://forum.notebookreview.com/threads/migrate-firefox-profile-from-windows-to-linux.444601/。但是,我需要将配置文件添加到Jenkins firefox实例,方法是将其复制到/var/lib/jenkins/.mozilla/firefox。我也没有复制整个Mozilla目录。配置文件,之后我修改了profiles.ini文件。)

我知道配置文件已成功复制,因为。 。 。

FirefoxProfile ffProfile = profile.getProfile("eAgency-Client1");

。 。 。不返回null。当我访问该网站时出现问题。当我访问该站点并获取页面源时,我可以看到标准错误,表明我没有发送证书:

<html><head><title>400 No required SSL certificate was sent</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>No required SSL certificate was sent</center>
<hr><center>nginx/1.10.2</center>


</body></html>

我在本地机器上没有这个来源。我的本地机器到达了它需要去的地方没有问题。在本地,我没有收到“400 Bad Request”。

© www.soinside.com 2019 - 2024. All rights reserved.