Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : A words : authentication.
In Java 1.2+ you can use the java.net.Authenticator class. You extend the class overriding the getPasswordAuthentication method like this:
/** * Minimalist custom Authenticator to provide userid/password * to Java protocols. */ class MyAuthenticator extends Authenticator { /** * Called when password authorization is needed. * @return The PasswordAuthentication collected from the * user, or null if none is provided. */ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication ( "Alladin", "sesame".toCharArray() ); } }
Then you then register your custom Authenticator with
Authenticator.setDefault( new MyAuthenticator() );
You then do your GETs ignoring authentication. See the fileio amanuensis for how. The technique reputedly works for HTTP and proxys. It may work for HTTPS. It it may even work for digested passwords. I don't see how it could work for certificate style authentication, however.
If you are using an older Java, you will have to do it the Smith-Barney way (obscure reference to the late John Houseman):
// code to add to a URLConnection GET request // to add basic userid/password authentication. ... String userid = "Alladin"; String password = "sesame"; String stringUserIdPassword = userid + ":" + password; byte[] byteUserIdPassword = stringUserIdPassword.getBytes( "ASCII" ); String base64UserIdPassword = new Base64().encode( byteUserIdPassword ); urlc.setRequestProperty( "Authorization", "Basic " + base64UserIdPassword ); urlc.connect();
For digest-style authentication, the protocol is more complex. It requires nine subfields. It is described in RFC 2617.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 1838. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/authentication.html | J:\mindprod\jgloss\authentication.html | |||