Java Tip 42: Write
Java apps that work with
proxy-based firewalls - JavaWorld - December 1997
Tutorial Details:
Java Tip 42: Write Java apps that work with proxy-based firewalls
Java Tip 42: Write Java apps that work with proxy-based firewalls
By: By Ron Kurr
How to use Java to connect with HTTP servers outside your corporate firewall
lmost every company is concerned with protecting its internal network from hackers and thieves. One common security measure is to completely disconnect the corporate network from the Internet. If the bad guys can't connect to any of your machines, they can't hack into them. The unfortunate side effect of this tactic is that internal users can't access external Internet servers, like Yahoo or JavaWorld . To address this problem, network administrators often install something called a "proxy server." Essentially, a proxy is a service that sits between the Internet and the internal network and manages connections between the two worlds. Proxies help reduce outside security threats while still allowing internal users to access Internet services. While Java makes it easy to write Internet clients, these clients are useless unless they can get past your proxy. Fortunately, Java makes it easy to work with proxies -- if you know the magic words, that is.
The secret to combining Java and proxies lies in activating certain system properties in the Java runtime. These properties appear to be undocumented, and are whispered between programmers as part of the Java folklore. In order to work with a proxy, your Java application needs to specify information about the proxy itself as well as specify user information for authentication purposes. In your program, before you begin to work with any Internet protocols, you'll need to add the following lines:
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "myProxyMachineName" );
System.getProperties().put( "proxyPort", "85" );
The first line above tells Java that you'll be using a proxy for your connections, the second line specifies the machine that the proxy lives on, and the third line indicates what port the proxy is listening on. Some proxies require a user to type in a username and password before Internet access is granted. You've probably encountered this behavior if you use a Web browser behind a firewall. Here's how to perform the authentication:
URLConnection connection = url.openConnection();
String password = "username:password";
String encodedPassword = base64Encode( password );
connection.setRequestProperty( "Proxy-Authorization", encodedPassword );
The idea behind the above code fragment is that you must adjust your HTTP header to send out your user information. This is achieved with the setRequestProperty() call. This method allows you to manipulate the HTTP headers before the request is sent out. HTTP requires the user name and password to be base64 encoded. Luckily, there are a couple of public domain APIs that will perform the encoding for you (see the Resources section).
As you can see, there's not a whole lot to adding proxy support to your Java application. Given what you now know, and a little research (you'll have to find out how your proxy handles the protocol you're interested in and how to deal with user authentication), you can implement your proxy with other protocols.
Proxying FTP
Scott D. Taylor sent in the magic incantation to deal with proxying the FTP protocol:
defaultProperties.put( "ftpProxySet", "true" );
defaultProperties.put( "ftpProxyHost", "proxy-host-name" );
defaultProperties.put( "ftpProxyPort", "85" );
You can then access the files URLs using the "ftp" protocol via something like:
URL url = new URL("ftp://ftp.netscape.com/pub/navigator/3.04/windows/readme.txt" );
If anybody has examples of using a proxy with other Internet protocols, I'd love to see them.
Note: The example code (Example.java) has only been tested with JDK 1.1.4.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Java Tip 42: Write
Java apps that work with
proxy-based firewalls - JavaWorld - December 1997
View Tutorial: Java Tip 42: Write
Java apps that work with
proxy-based firewalls - JavaWorld - December 1997
Related
Tutorials:
Java in a Nutshell Code Example
The Java programming examples shown here are from the book Java in a Nutshell , by David Flanagan, published by O\'Reilly & Associates. |
Reloading Applets
Reloading Applets |
Applet to Applet
Communication
Applet to Applet
Communication |
Java Tip 72: Press
Escape to close your Swing dialog windows
Java Tip 72: Press
Escape to close your Swing dialog windows |
Scripting power
saves the day
for your Java
apps
Scripting power
saves the day
for your Java
apps |
Enhance your Java application with Java Native Interface (JNI)
Enhance your Java application with Java Native Interface (JNI) |
Step into
the J2EE architecture and process
Step into
the J2EE architecture and process |
Quickly access files and
directories you use repeatedly
Quickly access files and
directories you use repeatedly |
Jini Starter Kit 2.0 tightens Jini's security framework
Jini Starter Kit 2.0 tightens Jini's security framework |
JHttpTunnel
JHttpTunnel is the implementation of GNU httptunnel\'s protocol in pure Java. |
Filtering and Transforming Digital Images
Filtering and Transforming Digital Images
In this Issue
Welcome to the Core Java Technologies Tech Tips for April 7, 2004. Here you\'ll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SE).
|
Data Models for Desktop Apps
Data Models for Desktop Apps
This is the third article in a series that presents the prototype of a Java desktop application called JImaging. The first article described the three major Java GUI toolkits: AWT, Swing, and SWT. In the second article, I int |
Clean Up Your Mess: Managing Temp Files in Java Apps
Clean Up Your Mess: Managing Temp Files in Java Apps
Creating and managing temporary files in a Java application can be a little tricky due to some open JVM bugs. Develop a workaround with some custom code and a clever design. |
Networking our whiteboard with servlets.
Find out how to easily replace the RMI and sockets networking layers with servlets. |
A first look at Apache Geronimo
Start developing and deploying J2EE apps on the first open source J2EE server. When released, Geronimo will be the first J2EE-certified open source server. This article will give you the basics you need for developing and deploying J2EE applications on Ge |
Power Messaging, Maps and more...
BuddySpace is an instant messenger with four novel twists: (1) it allows optional maps for geographical & office-plan visualizations in addition to standard 'buddy lists'; (2) it is built on open source Jabber, which makes it interoperable with ICQ, MSN, |
Understanding MIDP System Threads
Describes the multi-threaded aspects of the J2ME application environment. Understanding the interactions between systems threads, user-interface and application threads will help in avoiding MIDlet deadlock. |
Getting Started With Bluetooth
JSR-82 brings Bluetooth API's to the J2ME environment. Read the tech tip and begin experimenting with Bluetooth today using the Wireless Toolkit 2.2 Beta. |
Tech Tip: Using the Varargs Language Feature
Have you ever needed to pass in many instances of the same object type to a method, but you don't know at compile time how many instances there will be? Find out how the new varargs language feature makes it easy to handle situations like this. |
Running Wine on the Sun Java Desktop System, Release 2
A BigAdmin reader describes how to install and use Wine, an open source implementation of the Windows API, on the Sun Java Desktop System, Release 2. |
|
|
|