ssl - Java https server -
i building application server in java using httpserver class. have server functioning using plain text communication on http. however, wish upgrade using ssl, using httpsserver class. used question basis work from: simple java https server
my server class follows:
public server(options options){ sslcontext sslcontext = null; try { server = httpsserver.create(new inetsocketaddress(8080), 0); sslcontext = sslcontext.getinstance("tls"); char[] password = options.getsslpassword().tochararray(); keystore ks = keystore.getinstance ("jks"); fileinputstream fis = new fileinputstream (options.getsslkeystore()); ks.load ( fis, password ); keymanagerfactory kmf = keymanagerfactory.getinstance ( "sunx509" ); kmf.init ( ks, password ); trustmanagerfactory tmf = trustmanagerfactory.getinstance ( "sunx509" ); tmf.init ( ks ); sslcontext.init ( kmf.getkeymanagers (), tmf.gettrustmanagers (), null ); } catch (exception e) { e.printstacktrace(); } httpsconfigurator httpsconfigurator = new httpsconfigurator(sslcontext) { @override public void configure(httpsparameters httpsparameters) { sslcontext sslcontext = getsslcontext(); sslparameters defaultsslparameters = sslcontext.getdefaultsslparameters(); httpsparameters.setsslparameters(defaultsslparameters); } }; server.createcontext("/", new httphandler() { @override public void handle(httpexchange t) throws ioexception { httpsexchange s = (httpsexchange)t; s.getsslsession(); string response = "<html><body>hello world.</body></html>"; t.sendresponseheaders(200, response.length()); outputstream os = t.getresponsebody(); os.write(response.getbytes()); os.close(); } }); server.setexecutor(executors.newcachedthreadpool()); system.out.println("starting server on port " + port + "..."); server.sethttpsconfigurator(httpsconfigurator); server.start(); system.out.println("server started successfully!"); }
this compiles , runs fine, when try connect through browser on localhost:8080 "no data received" , on https://localhost:8080 "webpage not available" there no exceptions being thrown , seems run no issues, apart fact nothing.
i used keytool program generate keystore, unfamiliar process perhaps incorrect? again, accepts setting keystore , keymanagers etc.
do need change httphandler or contexts handle ssl exchange or something?
i have been able program working ssl code provided in question. believe issue having because of keystore had generated. using command generate keystore worked:
keytool -genkey -alias alias -keyalg rsa -keystore keystore.jks -keysize 2048
Comments
Post a Comment