Java Reference
In-Depth Information
Example 16-9. JSSEWebServer0
/**
* JSSEWebServer - subclass trivial WebServer0 to make it use SSL.
* N.B. You MUST have set up a server certificate (see the
* accompanying book text), or you will get the dreaded
* javax.net.ssl.SSLHandshakeException: no cipher suites in common
* (because without it JSSE can't use any of its built-in ciphers!).
*/
public
public class
class
JSSEWebServer0
JSSEWebServer0
extends
extends
WebServer0
{
public
public static
static final
final
int
int
HTTPS
=
8443
;
public
public static
throws
Exception
{
iif
(
System
.
getProperty
(
"javax.net.ssl.keyStore"
) ==
null
static
void
void
main
(
String
[]
args
)
throws
null
) {
System
.
err
.
println
(
"You must pass in a keystore via -D; see the documentation!"
);
System
.
exit
(
1
);
}
System
.
out
.
println
(
"DarwinSys JSSE Server 0.0 starting..."
);
JSSEWebServer0 w
=
new
new
JSSEWebServer0
();
w
.
runServer
(
HTTPS
);
// never returns!!
}
/** Get an HTTPS ServerSocket using JSSE.
* @see WebServer0#getServerSocket(int)
* @throws ClassNotFoundException if the SecurityProvider cannot be instantiated.
*/
protected
protected
ServerSocket
getServerSocket
(
int
int
port
)
throws
throws
Exception
{
SSLServerSocketFactory ssf
=
(
SSLServerSocketFactory
)
SSLServerSocketFactory
.
getDefault
();
return
return
ssf
.
createServerSocket
(
port
);
}
}
That is, indeed, all the Java code one needs to write. You do have to set up an SSL Certific-
ate. For demonstration purposes, this can be a self-signed certificate; the steps in
Signing
Your JAR File
(Steps 1-4) will suffice. You have to tell the JSSE layer where to find your
keystore:
java -Djavax.net.ssl.keyStore=/home/ian/.keystore -Djavax.net.ssl.
keyStorePassword=secrit JSSEWebServer0