• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLConnection and Authentication in JDK 1.3

 
Chris McGuirk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know if there is a way to have the Java plugin "piggyback" on the browser's current authentication credentials. IE, I have a site that uses Basic Authentication, but when I go to the applet it prompts me for it's own authentication credentials. The JVM for 1.1.8 in IE does this transparently, but apparently the plugin does not. Thanks.
 
daniel distelrath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a similar problem:
when i try to load my basic-user-authentication-protected html page with an applet embedded netscape6.1 (or rather the JVM) prompts for a user/password again and fails to load the applet or just fails w/o showing any further dialog box.
when i remove the protection or when using any other browser everything works fine.
did you find any solution for your problem that might help me?
--dan--
 
Christopher McGuirk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code snippet and declaration for the Base64Encoder class are below. The only way I can think to simulate Windows integrated security (which worked in JDK 1.1.8 using the Microsoft JVM, but not in the Java 1.3 plugin) is to pull the username and clear text password from the server variables (since we are using Basic authentication) and pass them down to the applet in parameters, then use those credentials to Authorize the URLConnection.
<snip>

// create a URL connection to the servlet
URL action = new URL(servletLoc );
URLConnection url = action.openConnection();
String encoding = "username:thepassword";
encoding = new Base64Encoder().encode(encoding.getBytes());

url.setRequestProperty("Authorization", "BASIC " + encoding);
url.setAllowUserInteraction( false );
// get the servlet parameters as an XMl String
String postData = command.getParameters();
// set the URLs attributes
url.setDoInput(true);
url.setDoOutput(true);
url.setUseCaches(false);
url.setRequestProperty("Content-type", "text/xml");
url.setRequestProperty("Content-length", "" + postData.length());
// Write out post data
DataOutputStream out = new DataOutputStream(url.getOutputStream());
out.writeBytes(postData);
out.flush();
out.close();
// get the results into an XML DOMDocument
employeeDoc.loadFromInputStream( url.getInputStream());
</snip>
Here is the Base64Encoder class I am using:
public class Base64Encoder
{
public static final char [ ] alphabet = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
public static String encode ( String s )
{
return encode ( s.getBytes ( ) );
}

public static String encode ( byte [ ] octetString )
{
int bits24;
int bits6;
char [ ] out
= new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ];
int outIndex = 0;
int i = 0;
while ( ( i + 3 ) <= octetString.length )
{
// store the octets
bits24 = ( octetString [ i++ ] & 0xFF ) << 16;
bits24 |= ( octetString [ i++ ] & 0xFF ) << 8;
bits24 |= ( octetString [ i++ ] & 0xFF ) << 0;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0000003F );
out [ outIndex++ ] = alphabet [ bits6 ];
}

if ( octetString.length - i == 2 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits24 |= ( octetString [ i + 1 ] & 0xFF ) << 8;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
}
else if ( octetString.length - i == 1 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
out [ outIndex++ ] = '=';
}
return new String ( out );
}
}

[This message has been edited by Christopher McGuirk (edited September 20, 2001).]
 
daniel distelrath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many thanx for your answer 8-)
i got the java plug-in 1.3.1 and now it works - not automatically like with other browsers than netscape6.1 though: have to enter user+pass.
--dan--
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic