Exit using java.net.Authenticator

I was wondering if any of you know how to "log out" with Basic Authentication (BA) using the java.net.Authenticator class. I know that BA does not have a logout method, and you need to close and reopen the browser to end the session. The question is, how do you close and open the browser in Java code? That is, I connect via java, and not to borwser, so how do I get the JVM to “de-authenticate” itself?

Context: I am writing a tweet posting application for multiple Twitter accounts using BA. I can use java.net. * To publish a tweet for one account (and see that it calls my authentication class), but when I try to publish a tweet for the second, I do not see the second call of the authenticator, and the tweet will be launched on the first account.

Is it possible to return the authenticator for re-authentication or is it a dead end? If so, I can just use OAuth instead.

Thanks so much for any information you can offer!

Shane

static class MyAuthenticator extends Authenticator { private String username, password; public MyAuthenticator(String user, String pass) { username = user; password = pass; } protected PasswordAuthentication getPasswordAuthentication() { System.out.println("Requesting Host : " + getRequestingHost()); System.out.println("Requesting Scheme : " + getRequestingScheme()); System.out.println("Requesting Site : " + getRequestingSite()); return new PasswordAuthentication(username, password.toCharArray()); } } 

public void tweet (AutoTwitterAccount acc, String tweet) {Authenticator.setDefault (zero);

  Authenticator.setDefault(new MyAuthenticator(acc.getUserName(), acc.getPassword())); try { /* First login the session and fire off tweet*/ URL url = new URL(AutoTweeter.TWEET_URL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "Mozilla/4.0"); conn.setRequestProperty("User-Agent", AGENT); conn.setRequestProperty("Content-Type", TYPE); conn.setRequestProperty("Content-Length", "" + tweet.length()); ...fire off tweet.... conn.disconnect(); 

Thanks again!

+4
source share
3 answers

If the authenticator is not really called again (and even reloading it to another does not work, which, apparently, is due to an error ), then you can refuse Authenticator and manually send the HTTP Basic Auth header :

 URL url = new URL("http://www.example.com/comment"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Authorization:", "Basic "+codec.encodeBase64String(("username:password").getBytes()); 
+4
source

I tried this, but it still did not authenticate, but I circumvented this using my colleagues' utility class to build the HTTP request myself and write it directly to the socket, bypassing the Sun Http classes. But thanks for your reply anyway!

0
source

If you are using Authenticator for a proxy server, try the following:

 import org.apache.commons.codec.binary.Base64; ... StringBuilder auth = new StringBuilder("Basic "); auth.append(Base64.encodeBase64String((username + ':' + password).getBytes())); connection.setRequestProperty("Proxy-Authorization", auth.toString()); 
0
source

Source: https://habr.com/ru/post/1299418/


All Articles