加密暂未开放
parent
97e8e434aa
commit
716b51693a
|
@ -30,6 +30,11 @@ public class Config {
|
||||||
*/
|
*/
|
||||||
public static ChannelHandlerContext ctx;
|
public static ChannelHandlerContext ctx;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密方式
|
||||||
|
*/
|
||||||
|
public static final String[] CIPHER_ARRAY = {"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256"};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 车辆VIN
|
* 车辆VIN
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -4,6 +4,7 @@ package com.muyu.netty.client;
|
||||||
import com.muyu.common.Common;
|
import com.muyu.common.Common;
|
||||||
import com.muyu.common.Config;
|
import com.muyu.common.Config;
|
||||||
import com.muyu.netty.bean.NettyClientBean;
|
import com.muyu.netty.bean.NettyClientBean;
|
||||||
|
import com.muyu.netty.ssl.SslContextFactory;
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
|
@ -15,10 +16,14 @@ import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
import io.netty.handler.codec.string.StringEncoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
|
import io.netty.handler.ssl.SslHandler;
|
||||||
import io.netty.handler.timeout.IdleStateHandler;
|
import io.netty.handler.timeout.IdleStateHandler;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLEngine;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 牧鱼
|
* @author 牧鱼
|
||||||
|
@ -45,40 +50,47 @@ public class NettyClientInit {
|
||||||
try {
|
try {
|
||||||
Bootstrap b = new Bootstrap();
|
Bootstrap b = new Bootstrap();
|
||||||
mClientHandler = new NettyClientHandler();
|
mClientHandler = new NettyClientHandler();
|
||||||
b.group(Config.workerGroup).channel(NioSocketChannel.class)
|
b.group(Config.workerGroup);
|
||||||
// KeepAlive
|
b.channel(NioSocketChannel.class);
|
||||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||||
// Handler
|
b.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
.handler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void initChannel(SocketChannel channel) throws Exception {
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void initChannel(SocketChannel channel) throws Exception {
|
||||||
|
// SSLContext sslCtx = SslContextFactory.getServerContext();
|
||||||
|
// SSLEngine sslEngine = sslCtx.createSSLEngine();
|
||||||
|
//设置加密套件
|
||||||
|
// sslEngine.setEnabledCipherSuites(Config.CIPHER_ARRAY);
|
||||||
|
// sslEngine.setUseClientMode(false);
|
||||||
|
// sslEngine.setNeedClientAuth(true);
|
||||||
|
// channel.pipeline().addFirst("SslEstablish", new SslHandler(sslEngine));
|
||||||
// SSLEngine sslEngine = sslContext.createSSLEngine();
|
// SSLEngine sslEngine = sslContext.createSSLEngine();
|
||||||
// sslEngine.setUseClientMode(false); //服务器端模式
|
// sslEngine.setUseClientMode(false); //服务器端模式
|
||||||
// sslEngine.setNeedClientAuth(false); //不需要验证客户端
|
// sslEngine.setNeedClientAuth(false); //不需要验证客户端
|
||||||
// channel.pipeline().addFirst("ssl", new SslHandler(sslEngine));
|
// channel.pipeline().addFirst("ssl", new SslHandler(sslEngine));
|
||||||
//分包器
|
//分包器
|
||||||
channel.pipeline().addLast(
|
channel.pipeline().addLast(
|
||||||
new DelimiterBasedFrameDecoder(
|
new DelimiterBasedFrameDecoder(
|
||||||
1024,
|
1024,
|
||||||
Unpooled.copiedBuffer(Config.DATA_PACK_SEPARATOR.getBytes()
|
Unpooled.copiedBuffer(Config.DATA_PACK_SEPARATOR.getBytes()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
// 心跳
|
// 心跳
|
||||||
channel.pipeline().addLast("HBeat", new IdleStateHandler(
|
channel.pipeline().addLast("HBeat", new IdleStateHandler(
|
||||||
20,
|
20,
|
||||||
10, 0));
|
10, 0));
|
||||||
//编码器
|
//编码器
|
||||||
channel.pipeline().addLast("encoder", new StringEncoder());
|
channel.pipeline().addLast("encoder", new StringEncoder());
|
||||||
//解码器
|
//解码器
|
||||||
channel.pipeline().addLast("decoder", new StringDecoder());
|
channel.pipeline().addLast("decoder", new StringDecoder());
|
||||||
|
|
||||||
//处理器
|
//处理器
|
||||||
channel.pipeline().addLast(mClientHandler);
|
channel.pipeline().addLast(mClientHandler);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// KeepAlive
|
||||||
|
// Handler
|
||||||
future = b.connect(nettyClientBean.getHost(), nettyClientBean.getPort()).sync();
|
future = b.connect(nettyClientBean.getHost(), nettyClientBean.getPort()).sync();
|
||||||
if (future.isSuccess()) {
|
if (future.isSuccess()) {
|
||||||
log.info("Client,链接服务端成功");
|
log.info("Client,链接服务端成功");
|
||||||
|
|
|
@ -43,7 +43,6 @@ public class NettyClientMsg {
|
||||||
* 销毁netty
|
* 销毁netty
|
||||||
*/
|
*/
|
||||||
public static void destroy(){
|
public static void destroy(){
|
||||||
log.info("发送断开连接消息:"+Config.NETTY_CLOSE);
|
|
||||||
sendMsg(Config.NETTY_WILL_CLOSE + Config.VIN);
|
sendMsg(Config.NETTY_WILL_CLOSE + Config.VIN);
|
||||||
Config.ctx = null;
|
Config.ctx = null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
package com.muyu.netty.ssl;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.net.ssl.KeyManagerFactory;
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
|
||||||
|
public class SslContextFactory {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(SslContextFactory.class);
|
||||||
|
|
||||||
|
private static final String PROTOCOL = "TLS";
|
||||||
|
|
||||||
|
private static volatile SSLContext SERVER_CONTEXT = null;
|
||||||
|
|
||||||
|
private static final String DEFAULT_PROPERTIES = "application.properties";
|
||||||
|
|
||||||
|
private static final String SSL_KEY_STORE_TYPE = "JKS";
|
||||||
|
|
||||||
|
private static final String SSL_KEY_STORE_PASSWORD = "vehicle";
|
||||||
|
|
||||||
|
private static final String SSL_KEY_STORE = System.getProperty("user.dir")+ File.separator + "src" +File.separator + "main" + File.separator +
|
||||||
|
"resources" + File.separator+"ssl"+File.separator+"cVehicleChat.jks";
|
||||||
|
|
||||||
|
private static void init(){
|
||||||
|
InputStream keyStore = null;
|
||||||
|
InputStream trustStore = null;
|
||||||
|
try {
|
||||||
|
//初始化keyManagerFactory
|
||||||
|
KeyStore ks = KeyStore.getInstance(SSL_KEY_STORE_TYPE);
|
||||||
|
keyStore = new FileInputStream(SSL_KEY_STORE);
|
||||||
|
ks.load(keyStore, SSL_KEY_STORE_PASSWORD.toCharArray());
|
||||||
|
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||||
|
kmf.init(ks, SSL_KEY_STORE_PASSWORD.toCharArray());
|
||||||
|
//初始化TrustManagerFacotry
|
||||||
|
KeyStore ts = KeyStore.getInstance(SSL_KEY_STORE_TYPE);
|
||||||
|
trustStore = new FileInputStream(SSL_KEY_STORE);
|
||||||
|
ts.load(trustStore, SSL_KEY_STORE_PASSWORD.toCharArray());
|
||||||
|
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||||
|
tmf.init(ts);
|
||||||
|
//生成SSLContext
|
||||||
|
SERVER_CONTEXT = SSLContext.getInstance(PROTOCOL);
|
||||||
|
SERVER_CONTEXT.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (null != keyStore) {
|
||||||
|
keyStore.close();
|
||||||
|
}
|
||||||
|
if (null != trustStore) {
|
||||||
|
trustStore.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
public static SSLContext getServerContext() {
|
||||||
|
return SERVER_CONTEXT;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue