Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added support for per-request authentication to Jsoup.connect #2046

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

jhy
Copy link
Owner

@jhy jhy commented Nov 9, 2023

Uses the multi-version support so that in Java versions that support it (9+), an authenticator is set via java.net.HttpURLConnection.setAuthenticator().

On Java 8, we set the system-wide default authenticator, and use ThreadLocals to enable per-request authenticators.

Also adds tests for HTTP and HTTPS server and proxy basic authentication.

Example use:

 Connection session = Jsoup.newSession()
     .proxy("proxy.example.com", 8080)
     .auth(auth -> {
         if (auth.isServer()) { // provide credentials for the request url
             Validate.isTrue(auth.url().getHost().equals("example.com"));
             // check that we're sending credentials were we expect, and not redirected out
             return auth.credentials("username", "password");
         } else { // auth.isProxy()
             return auth.credentials("proxy-user", "proxy-password");
         }
     });

Uses the multi-version support so that in Java versions that support it (9+), an authenticator is set via `java.net.HttpURLConnection.setAuthenticator()`.

On Java 8, we set the system-wide default authenticator, and use ThreadLocals to enable per-request authenticators.

Also adds tests for HTTP and HTTPS server and proxy basic authentication.
src/test/java/org/jsoup/integration/ProxyTest.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/integration/ConnectTest.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/helper/HttpConnectionTest.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/helper/HttpConnectionTest.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/helper/HttpConnectionTest.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/helper/HttpConnectionTest.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/integration/servlets/AuthFilter.java Dismissed Show resolved Hide resolved
src/test/java/org/jsoup/integration/servlets/AuthFilter.java Dismissed Show resolved Hide resolved
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant