|
1 | 1 | package com.networknt.example;
|
2 | 2 |
|
3 | 3 | import io.javalin.Javalin;
|
4 |
| -import io.javalin.http.Context; |
5 |
| -import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory; |
6 |
| -import org.eclipse.jetty.http2.HTTP2Cipher; |
7 |
| -import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; |
8 |
| -import org.eclipse.jetty.server.HttpConfiguration; |
9 |
| -import org.eclipse.jetty.server.HttpConnectionFactory; |
10 |
| -import org.eclipse.jetty.server.SecureRequestCustomizer; |
11 |
| -import org.eclipse.jetty.server.Server; |
12 | 4 | import org.eclipse.jetty.server.ServerConnector;
|
13 |
| -import org.eclipse.jetty.server.SslConnectionFactory; |
14 | 5 | import org.eclipse.jetty.util.ssl.SslContextFactory;
|
15 | 6 |
|
16 | 7 | public class ExampleApplication {
|
17 | 8 |
|
18 | 9 | public static void main(String[] args) {
|
19 |
| - Javalin app = Javalin.create(config -> { |
20 |
| - // Manual Jetty configuration required for SSL/TLS/HTTP2 |
21 |
| - config.server(ExampleApplication::createServer); |
22 |
| - }).start(); |
23 |
| - app.get("/", ExampleApplication::helloWorld); |
24 |
| - } |
25 |
| - |
26 |
| - public static void helloWorld(Context ctx) { |
27 |
| - ctx.result("Hello world!"); |
28 |
| - } |
29 |
| - |
30 |
| - private static Server createServer() { |
31 |
| - Server server = new Server(); |
32 |
| - |
33 |
| - // HTTP |
34 |
| - ServerConnector connector = new ServerConnector(server); |
35 |
| - connector.setPort(8080); |
36 |
| - server.addConnector(connector); |
37 |
| - |
38 |
| - // HTTPS |
39 |
| - ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory()); |
40 |
| - sslConnector.setPort(8443); |
41 |
| - server.addConnector(sslConnector); |
42 |
| - |
43 |
| - |
44 |
| - // HTTP/2 |
45 |
| - HttpConfiguration httpsConfig = createHttpsConfig(); |
46 |
| - |
47 |
| - SslContextFactory sslContextFactory = getSslContextFactory(); |
48 |
| - sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR); |
49 |
| - sslContextFactory.setProvider("Conscrypt"); |
50 |
| - |
51 |
| - HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpsConfig); |
52 |
| - |
53 |
| - HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpsConfig); |
54 |
| - ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory(); |
55 |
| - alpn.setDefaultProtocol(httpConnectionFactory.getProtocol()); |
56 |
| - |
57 |
| - SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol()); |
58 |
| - |
59 |
| - ServerConnector http2Connector = new ServerConnector( |
60 |
| - server, |
61 |
| - ssl, |
62 |
| - alpn, |
63 |
| - h2, |
64 |
| - httpConnectionFactory |
65 |
| - ); |
66 |
| - http2Connector.setPort(9443); |
67 |
| - server.addConnector(http2Connector); |
68 |
| - |
69 |
| - return server; |
70 |
| - } |
| 10 | + // Get example from https://github.com/javalin/javalin/blob/master/javalin/src/test/java/io/javalin/examples/HelloWorldSecure.java#L22-L30 |
| 11 | + Javalin.create(config -> { |
| 12 | + config.jetty.addConnector((server, httpConfiguration) -> { |
| 13 | + ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory()); |
| 14 | + sslConnector.setPort(443); |
| 15 | + return sslConnector; |
| 16 | + }); |
| 17 | + config.jetty.addConnector((server, httpConfiguration) -> { |
| 18 | + ServerConnector connector = new ServerConnector(server); |
| 19 | + connector.setPort(80); |
| 20 | + return connector; |
| 21 | + }); |
| 22 | + |
| 23 | + }) |
| 24 | + .start() |
| 25 | + .get("/", ctx -> ctx.result("Hello World")); |
71 | 26 |
|
72 |
| - private static HttpConfiguration createHttpsConfig() { |
73 |
| - HttpConfiguration httpConfig = new HttpConfiguration(); |
74 |
| - httpConfig.setSendXPoweredBy(false); |
75 |
| - httpConfig.setSendServerVersion(false); |
76 |
| - httpConfig.setSecureScheme("https"); |
77 |
| - httpConfig.addCustomizer(new SecureRequestCustomizer()); |
78 |
| - return httpConfig; |
79 | 27 | }
|
80 | 28 |
|
81 |
| - private static SslContextFactory getSslContextFactory() { |
82 |
| - SslContextFactory sslContextFactory = new SslContextFactory.Server(); |
83 |
| - sslContextFactory.setKeyStorePath(ExampleApplication.class.getResource("/server.keystore").toExternalForm()); |
| 29 | + private static SslContextFactory.Server getSslContextFactory() { |
| 30 | + SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); |
| 31 | + sslContextFactory.setKeyStorePath(ExampleApplication.class.getResource("/keystore.jks").toExternalForm()); |
84 | 32 | sslContextFactory.setKeyStorePassword("password");
|
85 |
| - sslContextFactory.setUseCipherSuitesOrder(true); |
86 | 33 | return sslContextFactory;
|
87 | 34 | }
|
88 | 35 | }
|
0 commit comments