加密暂未开放
parent
97e8e434aa
commit
716b51693a
|
@ -30,6 +30,11 @@ public class Config {
|
|||
*/
|
||||
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
|
||||
*/
|
||||
|
|
|
@ -4,6 +4,7 @@ package com.muyu.netty.client;
|
|||
import com.muyu.common.Common;
|
||||
import com.muyu.common.Config;
|
||||
import com.muyu.netty.bean.NettyClientBean;
|
||||
import com.muyu.netty.ssl.SslContextFactory;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.Unpooled;
|
||||
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.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
|
||||
/**
|
||||
* @author 牧鱼
|
||||
|
@ -45,15 +50,20 @@ public class NettyClientInit {
|
|||
try {
|
||||
Bootstrap b = new Bootstrap();
|
||||
mClientHandler = new NettyClientHandler();
|
||||
b.group(Config.workerGroup).channel(NioSocketChannel.class)
|
||||
// KeepAlive
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
// Handler
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
b.group(Config.workerGroup);
|
||||
b.channel(NioSocketChannel.class);
|
||||
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||
b.handler(new ChannelInitializer<SocketChannel>() {
|
||||
|
||||
@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.setUseClientMode(false); //服务器端模式
|
||||
// sslEngine.setNeedClientAuth(false); //不需要验证客户端
|
||||
|
@ -79,6 +89,8 @@ public class NettyClientInit {
|
|||
channel.pipeline().addLast(mClientHandler);
|
||||
}
|
||||
});
|
||||
// KeepAlive
|
||||
// Handler
|
||||
future = b.connect(nettyClientBean.getHost(), nettyClientBean.getPort()).sync();
|
||||
if (future.isSuccess()) {
|
||||
log.info("Client,链接服务端成功");
|
||||
|
|
|
@ -43,7 +43,6 @@ public class NettyClientMsg {
|
|||
* 销毁netty
|
||||
*/
|
||||
public static void destroy(){
|
||||
log.info("发送断开连接消息:"+Config.NETTY_CLOSE);
|
||||
sendMsg(Config.NETTY_WILL_CLOSE + Config.VIN);
|
||||
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