Jetty9 Embedded從http升級到https

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   sp   java   for   on   

什麼是https

之前我在這篇文章裡頭說過了https

造公開金鑰和私密金鑰
keytool -genkey -alias sitename -keyalg RSA -keystore keystore.jks -keysize 2048

這個檔案是一個公開金鑰和私密金鑰對

建立Connector

這一點很關鍵,說白了,就是當發生http請求的時候,返回一個!403,告訴他不安全,讓他重新導向到安全的連接埠

具體的做法:

  1. 對於不安全的請求返回!403

其實這個是加到web.xml裡頭的,只是這裡用代碼展現出來

ConstraintSecurityHandler security = new ConstraintSecurityHandler();Constraint constraint = new Constraint();constraint.setDataConstraint(Constraint.DC_CONFIDENTIAL);//makes the constraint apply to all uri pathsConstraintMapping mapping = new ConstraintMapping();mapping.setPathSpec("/*");mapping.setConstraint(constraint);security.addConstraintMapping(mapping);// Web app handlersWebAppContext app = new WebAppContext(server, base, "/");app.setHandler(security);
  1. 對於http的Connector,告訴它安全的連接埠和協議是什麼

    private static ServerConnector getHttpConnector(int port) {HttpConfiguration config = new HttpConfiguration();config.setSecureScheme("https");config.setSecurePort(port + 443);ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(config));connector.setPort(port);return connector;}
  2. 加入https的Connector

    private static ServerConnector getHttpsConnector(int port) {HttpConfiguration https = new HttpConfiguration();https.setSecurePort(port);https.setSecureScheme("https");https.addCustomizer(new SecureRequestCustomizer());SslContextFactory sslContextFactory = new SslContextFactory();sslContextFactory.setKeyStorePath(ControllerWebServer.class.getResource(        "/keystore.jks").toExternalForm());sslContextFactory.setKeyStorePassword("123456");sslContextFactory.setKeyManagerPassword("123456");ServerConnector sslConnector = new ServerConnector(server,        new SslConnectionFactory(sslContextFactory, "http/1.1"),        new HttpConnectionFactory(https));sslConnector.setPort(port);return sslConnector;}
  3. server 啟動

server.setConnectors(new Connector[]{httpsConnector, httpConnector});// Web app handlersWebAppContext app = new WebAppContext(server, base, "/");app.setHandler(security);// Start appserver.start();logger.info(LoggerServer.CU, "Start updater web server success");server.join();

Jetty9 Embedded從http升級到https

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.