无法使用Browsermob代理执行http基本身份验证

时间:2018-06-09 14:27:10

标签: selenium selenium-webdriver http-authentication browsermob-proxy

我正在尝试使用Browsermob代理来实现跨不同浏览器的基本http身份验证。但面对问题,尝试在Browsermob的GITHUB页面上搜索解决方案,但在那里找不到多少帮助。

这是我实现身份验证的代码:

    // start the proxy
    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.start();

    proxy.autoAuthorization("", "username", "password", AuthType.BASIC);

    // get the Selenium proxy object
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

如果我在chrome上运行上面的代码,那么它仍然要求auth。并且错误控制台中没有任何内容。

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + config.getChromeDriverPath());
WebDriver driver = new ChromeDriver(capabilities);

Chrome Results

如果我在firefox上运行上面的代码,则会出错

  

您的连接不安全。

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + config.getChromeDriverPath());
WebDriver driver = new ChromeDriver(capabilities);

Firefox results

在控制台中出现以下错误:

FAILED CONFIGURATION: @BeforeClass launchBrowser
org.openqa.selenium.WebDriverException: 
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'USER', ip: '192.168.43.57', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: false, browserName: firefox, browserVersion: 60.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 8180, moz:profile: C:\Users\USER\AppDat..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.3, proxy: Proxy(manual, http=tarun:49..., rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 6b53fdb7-78fe-44cb-a3b9-2592494ae7a5

什么是不正确的?

1 个答案:

答案 0 :(得分:0)

proxy.autoAuthorization("", "guest", "guest", AuthType.BASIC);

此功能不起作用。

改为使用此:

String encodedCreadentials = "Basic " + (Base64.getEncoder().encodeToString("login:password".getBytes()));
proxy.addHeader("Authorization", encodedCreadentials);