基于jetty9 编程构建嵌入式https 服务器

基于jetty9 编程构建嵌入式https 服务器

您的评价:

 收藏该经验    

第一步,命令行下使用jdk的keytool工具生成keystore

?

1
keytool -keystore keystore - alias jetty -genkey -keyalg RSA

第二,生成证书

?

1
keytool - export - alias jetty - file jetty.crt -keystore keystore

第三,生成OBA文件,这里的yoursslpassword是第一步填写的密码

?

1
java - cp jetty-all-9.0.5.v20130815.jar org.eclipse.jetty.util.security.Password yoursslpassword

第四,编码实现https服务器

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Server server =  new Server();
 
HttpConfiguration https_config =  new HttpConfiguration();
https_config.setSecureScheme( "https" );
https_config.setSecurePort( 8443 );
https_config.setOutputBufferSize( 32768 );
https_config.addCustomizer( new SecureRequestCustomizer());
 
SslContextFactory sslContextFactory =  new SslContextFactory();
sslContextFactory.setKeyStorePath( "keystore" );
sslContextFactory.setKeyStorePassword( "OBF:1xtb1uo71wg41y0q1y7z1y101wfu1unr1xu7" );
sslContextFactory.setKeyManagerPassword( "OBF:1xtb1uo71wg41y0q1y7z1y101wfu1unr1xu7" );
 
ServerConnector httpsConnector =  new ServerConnector(server,
         new SslConnectionFactory(sslContextFactory, "http/1.1" ),
         new HttpConnectionFactory(https_config));
httpsConnector.setPort( 8443 );
httpsConnector.setIdleTimeout( 500000 );
server.addConnector(httpsConnector);
 
WebAppContext webApp =  new WebAppContext();
webApp =  new WebAppContext();
webApp.setContextPath( "/myapp" );
webApp.setResourceBase( "WebRoot" );
webApp.setInitParameter( "org.eclipse.jetty.servlet.Default.dirAllowed" ,  "false" );
webApp.setInitParameter( "org.eclipse.jetty.servlet.Default.useFileMappedBuffer" ,  "false" );
server.setHandler(webApp);
 
try {
     server.start();
     server.join();
}  catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}


第五步,通过浏览器访问 https://localhost:8443/myapp访问应用。浏览器会提示证书不可信任,是否继续访问,点是即可。

你可能感兴趣的:(基于jetty9 编程构建嵌入式https 服务器)