Jetty 9.2.1 redirect http to https

前端 未结 2 1412
眼角桃花
眼角桃花 2021-02-06 16:29

can someone help migrating the code from jetty 8 to 9.2.1.

I need to have jetty listening on port 80 (http) and redirect every request to 443 (https).

this is th

2条回答
  •  情话喂你
    2021-02-06 16:54

    In jetty 9.3 use SecuredRedirectHandler:

    public class Server extends org.eclipse.jetty.server.Server {
    
        public Server(int httpPort, boolean enableSsl, int httpsPort, String keystorePath, String keystorePassword, String keyManagerPassword, ...) {
            initConnector(httpPort, enableSsl, httpsPort, keystorePath, keystorePassword, keyManagerPassword);
            ...
        }
    
        private void initConnector(int httpPort, boolean enableSsl, int httpsPort, String keystorePath, String keystorePassword, String keyManagerPassword) {
            if (enableSsl) {
                final HttpConfiguration httpConfig = getHttpConfig(httpsPort);
                final HttpConfiguration httpsConfig = getHttpsConfig(httpConfig);
                final ServerConnector httpConnector = getHttpConnector(httpConfig, httpPort);
                final ServerConnector httpsConnector = getHttpsConnector(httpsConfig, httpsPort, keystorePath, keystorePassword, keyManagerPassword);
    
                setConnectors(httpConnector, httpsConnector);
                addHandler(new SecuredRedirectHandler());
            } else {
                final ServerConnector serverConnector = new ServerConnector(this);
    
                serverConnector.setPort(httpPort);
    
                addConnector(serverConnector);
            }
        }
    
        private void setConnectors(ServerConnector httpConnector, ServerConnector httpsConnector) {
            setConnectors(new Connector[]{httpConnector, httpsConnector});
        }
    
        private ServerConnector getHttpsConnector(HttpConfiguration httpsConfig, int httpsPort, String keystorePath, String keystorePassword, String keyManagerPassword) {
            final SslContextFactory sslContextFactory = new SslContextFactory();
    
            sslContextFactory.setKeyStorePath(keystorePath);
            sslContextFactory.setKeyStorePassword(keystorePassword);
            sslContextFactory.setKeyManagerPassword(keyManagerPassword);
            sslContextFactory.setTrustStorePath(keystorePath);
            sslContextFactory.setTrustStorePassword(keystorePassword);
    
            final ServerConnector httpsConnector = new ServerConnector(this,
                    new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
                    new HttpConnectionFactory(httpsConfig));
    
            httpsConnector.setPort(httpsPort);
    
            return httpsConnector;
        }
    
        private ServerConnector getHttpConnector(HttpConfiguration httpConfig, int httpPort) {
            final ServerConnector httpConnector = new ServerConnector(this);
    
            httpConnector.addConnectionFactory(new HttpConnectionFactory(httpConfig));
            httpConnector.setPort(httpPort);
    
            return httpConnector;
        }
    
        private HttpConfiguration getHttpsConfig(HttpConfiguration httpConfig) {
            final HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
    
            httpsConfig.addCustomizer(new SecureRequestCustomizer());
    
            return httpsConfig;
        }
    
        private HttpConfiguration getHttpConfig(int httpsPort) {
            final HttpConfiguration httpConfig = new HttpConfiguration();
    
            httpConfig.addCustomizer(new SecureRequestCustomizer());
            httpConfig.setSecureScheme(HttpScheme.HTTPS.asString());
            httpConfig.setSecurePort(httpsPort);
    
            return httpConfig;
        }
    
        private void addHandler(Handler handler) {
            final Handler currentHandler = getHandler();
            if (currentHandler == null) {
                setHandler(handler);
            } else {
                if (currentHandler instanceof HandlerList) {
                    ((HandlerList) currentHandler).addHandler(handler);
                } else {
                    final HandlerList handlerList = new HandlerList();
    
                    handlerList.addHandler(currentHandler);
                    handlerList.addHandler(handler);
    
                    setHandler(handlerList);
                }
            }
        }
    }
    

提交回复
热议问题