7

I'm developing a JavaFX application and I'm trying to use WebView to access a web application. This web application has Basic Authentication and I want to submit the credentials programatically (don't want to prompt the user for his credentials, they're stored in the JavaFX application, I know the security implications about this approach).

The only link I found in google is this:

https://community.oracle.com/message/12518017

With no answers yet.

2 Answers2

5

We were actually trying the approach with using the Authenticator and it worked.

URI baseUri = URI.create("https://my.company.com/");
String username = "";
String password = "";

Authenticator.setDefault(new Authenticator() {

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        // only return our credentials for our URI
        if (baseUri.getHost().equals(getRequestingHost())) {
            return new PasswordAuthentication(username, password.toCharArray());
        }
        return null;
    }
});
Dirk Fauth
  • 4,128
  • 2
  • 13
  • 23
3

If you simply need to add an Authorization header, you can use the webEngine.setUserAgent() method to add any header you need.

For example, for basic authorization you would add the header like so:

webEngine.setUserAgent("foo\nAuthorization: Basic YourBase64EncodedCredentials");

A hacky solution I know, but I couldn't find another way to set a header.

  • 1
    Hacky, but you're not alone in using it: https://twitter.com/CodingFabian/status/524942996748652544 – David Sep 24 '19 at 16:48