-
Notifications
You must be signed in to change notification settings - Fork 2
/
JettyMtlsServer.java
77 lines (57 loc) · 2.56 KB
/
JettyMtlsServer.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package me.imlc;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;
public class JettyMtlsServer {
private static Logger logger = Logger.getLogger(JettyMtlsServer.class.getSimpleName());
private Server server;
public void start() throws Exception {
server = new Server();
HttpConfiguration https = new HttpConfiguration();
https.addCustomizer(new SecureRequestCustomizer());
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(this.getClass().getResource(
"/server.jks").toExternalForm());
sslContextFactory.setKeyStorePassword("changeit");
sslContextFactory.setTrustStorePath(this.getClass().getResource("/server_truststore.jks").toExternalForm());
sslContextFactory.setTrustStorePassword("changeit");
sslContextFactory.setNeedClientAuth(true);
ServerConnector connector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(https));
connector.setPort(8443);
server.setConnectors(new Connector[] { connector });
server.setHandler(new HelloWorldHandler());
server.start();
logger.info("Server is running at http://localhost:8443");
}
public void stop() throws Exception {
server.stop();
}
public static void main(String[] args) throws Exception {
// Comment out lines below to enable debug log
// System.setProperty("javax.net.debug", "all");
// System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG");
JettyMtlsServer app = new JettyMtlsServer();
app.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
app.stop();
} catch (Exception e) {
e.printStackTrace();
}
}));
}
private static class HelloWorldHandler extends AbstractHandler {
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
httpServletResponse.getWriter().print("Hello, world");
request.setHandled(true);
}
}
}