测试环境中Springboot如何同时使用https协议和http协议

本文介绍了在Springboot项目中如何创建自签名的数字证书,并配置应用程序同时支持HTTPS和HTTP协议。首先,通过keytool生成PKCS12格式的密钥库文件,然后将其引入到Springboot项目中。接着,配置application.yml以启用HTTPS,并在测试中确认HTTPS访问。为了同时支持HTTP,需自定义TomcatServletWebServerFactory并配置额外的HTTP端口,最终实现两种协议的并存。
摘要由CSDN通过智能技术生成

1. 创建自定义数字证书,打开命令行提示符

2.输入如下代码,其中“d:/creat/”是你存放生成数字证书的地方,需要提前建立对应文件夹

 keytool -genkeypair -alias springboot-https -keypass 123456 -keyalg RSA -keysize 2048 -validity 365 -keystore d:/creat/springboot.keystore -storepass 123456

执行结果如下图,询问的信息可以随便填

会出现一个这样的警告,需要更改一下证书密钥库的格式才可以。

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore d:/creat/springboot.keystore -destkeystore d:/creat/springboot.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

输入如下代码:

keytool -importkeystore -srckeystore d:/creat/springbo

下面是使用Netty实现WebSocket协议的示例代码,配合使用Spring Boot的WebSocketController: ``` // NettyWebSocket协议实现示例代码 public class WebSocketServer extends ChannelInboundHandlerAdapter { private WebSocketServerHandshaker handshaker; private static final String WEB_SOCKET_URL = "ws://localhost:8888/websocket"; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof FullHttpRequest) { // 处理HTTP请求,完成WebSocket握手 handleHttpRequest(ctx, (FullHttpRequest) msg); } else if (msg instanceof WebSocketFrame) { // 处理WebSocket请求 handleWebSocketFrame(ctx, (WebSocketFrame) msg); } } private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { if (!req.decoderResult().isSuccess() || !"websocket".equals(req.headers().get("Upgrade"))) { // 如果不是WebSocket请求,则返回400 Bad Request响应 sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return; } // 构造握手响应返回,本机测试 WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(WEB_SOCKET_URL, null, false); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); } } private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // 判断是否是关闭链路的指令 if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } // 判断是否是Ping消息 if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } // 本例仅支持文本消息,不支持二进制消息 if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } // 处理WebSocket消息,这里会调用Spring Boot的WebSocketController String request = ((TextWebSocketFrame) frame).text(); WebSocketController.handleWebSocketMessage(ctx, request); } private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // 返回应答给客户端 if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // 如果是非Keep-Alive,关闭连接 ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } } } // Spring Boot的WebSocketController示例代码 @Controller public class WebSocketController { @OnOpen public void onOpen(Session session) { // 连接建立时触发 } @OnClose public void onClose(Session session) { // 连接关闭时触发 } @OnMessage public void onMessage(String message, Session session) { // 接收到客户端消息时触发 } public static void handleWebSocketMessage(ChannelHandlerContext ctx, String message) { // 处理WebSocket消息 // 将WebSocket消息发送到Spring Boot的WebSocketController处理 // 这里使用Spring Boot的WebSocketTemplate发送消息 WebSocketSessionManager.getInstance().getSession().sendMessage(new TextMessage(message)); } } ``` 以上是一个简单的使用Netty实现WebSocket协议,配合使用Spring Boot的WebSocketController的示例代码,具体实现方法可能会因需求不同有所变化。其,`WebSocketController`类可以根据具体的需求进行修改,例如添加消息广播等功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值