您好,登录后才能下订单哦!
本篇内容主要讲解“Jetty嵌入开发怎么配置SSL”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Jetty嵌入开发怎么配置SSL”吧!
一 使用java 自带工具 keytool 生成证书
keytool -genkey -alias server -keypass 123456 -storepass 123456 -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650 -dname "C=CN,ST=BJ,L=BJ,O=ca.wbq.com,OU=ca.wbq.com,CN=ca.wbq.com"
2 java程序
import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.util.ssl.SslContextFactory; public class JeetSslTest { public static void main(String[] args) { Server server = new Server(); HttpConfiguration https_config = new HttpConfiguration(); https_config.setSecureScheme("https"); SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); sslContextFactory.setKeyStoreType("PKCS12"); sslContextFactory.setKeyStorePath("e:/temp/key/keystore.p12"); sslContextFactory.setTrustStorePath("e:/temp/key/keystore.p12"); sslContextFactory.setKeyStorePassword("123456"); sslContextFactory.setKeyManagerPassword("123456"); try{ ServerConnector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config)); httpsConnector.setPort(8443); server.addConnector(httpsConnector); server.setHandler(new HelloHandler());//处理逻辑 server.start(); server.join(); }catch(Exception e){ e.printStackTrace(); } } }
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; public class HelloHandler extends AbstractHandler{ @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException { System.out.println(target); // 设置字符集,避免乱码 response.setContentType("text/html;charset=utf-8"); // 输出我们想要显示的问候语 response.getWriter().println("hello jetty"); // 表示请求处理完成 baseRequest.setHandled(true); } }
运行成功后使用浏览器访问 https://127.0.0.1:8443/ 测试
到此,相信大家对“Jetty嵌入开发怎么配置SSL”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。