├── src
├── main
│ ├── resources
│ │ ├── keystores
│ │ │ ├── kserver.keystore
│ │ │ └── tserver.keystore
│ │ ├── config
│ │ │ ├── application.yaml
│ │ │ ├── application-dev.yaml
│ │ │ ├── application-pro.yaml
│ │ │ └── application-test.yaml
│ │ └── logback-spring.xml
│ └── java
│ │ └── com
│ │ └── lance
│ │ ├── api
│ │ ├── common
│ │ │ ├── constant
│ │ │ │ ├── Globals.java
│ │ │ │ ├── ServerCode.java
│ │ │ │ └── ResponseCode.java
│ │ │ ├── exception
│ │ │ │ └── BaseException.java
│ │ │ ├── core
│ │ │ │ └── annotation
│ │ │ │ │ ├── Validation.java
│ │ │ │ │ └── impl
│ │ │ │ │ └── ValidationUtils.java
│ │ │ ├── config
│ │ │ │ ├── ExecutorConfig.java
│ │ │ │ └── DruidConfig.java
│ │ │ ├── base
│ │ │ │ ├── model
│ │ │ │ │ ├── Communication.java
│ │ │ │ │ └── JsonResult.java
│ │ │ │ └── dao
│ │ │ │ │ └── BaseDao.java
│ │ │ └── util
│ │ │ │ ├── SpringContextUtil.java
│ │ │ │ └── SftpUtil.java
│ │ └── business
│ │ │ ├── dao
│ │ │ ├── ApiDao.java
│ │ │ └── SysDao.java
│ │ │ ├── pojo
│ │ │ ├── model
│ │ │ │ ├── FieldNameModel.java
│ │ │ │ ├── ReturnModel.java
│ │ │ │ ├── EntryModel.java
│ │ │ │ ├── EntryBodyModel.java
│ │ │ │ └── EntryHeadModel.java
│ │ │ └── dto
│ │ │ │ └── SampleModel.java
│ │ │ ├── service
│ │ │ ├── wrap
│ │ │ │ ├── core
│ │ │ │ │ ├── SameServiceWrapper.java
│ │ │ │ │ ├── BaseServiceWrapper.java
│ │ │ │ │ └── ServiceWrapperContainer.java
│ │ │ │ ├── config
│ │ │ │ │ └── InitServiceWrapper.java
│ │ │ │ └── business
│ │ │ │ │ ├── CheckWrapper.java
│ │ │ │ │ ├── SaveWrapper.java
│ │ │ │ │ └── InquiryWrapper.java
│ │ │ ├── ApiService.java
│ │ │ └── impl
│ │ │ │ └── ApiServiceImpl.java
│ │ │ ├── socketserver
│ │ │ ├── netty
│ │ │ │ ├── MessageEncoder.java
│ │ │ │ ├── MessageDecoder.java
│ │ │ │ ├── ServerHandler.java
│ │ │ │ └── NettyServer.java
│ │ │ └── nativesocket
│ │ │ │ └── ServerSocket.java
│ │ │ └── util
│ │ │ ├── ComposeUtil.java
│ │ │ ├── DealSocket.java
│ │ │ └── ByteDataBuffer.java
│ │ └── Application.java
└── test
│ └── java
│ └── com
│ ├── sslsocketclient
│ ├── res
│ │ ├── kclient.keystore
│ │ └── tclient.keystore
│ ├── 查询.java
│ ├── HexStringUtils.java
│ ├── ComposeDemo.java
│ ├── DeSplitDemo.java
│ └── ByteDataBuffer.java
│ └── lance
│ ├── BaseTests.java
│ └── ApplicationTests.java
├── README.md
└── pom.xml
/src/main/resources/keystores/kserver.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lance2038/springbootNettySslSocketDemo/HEAD/src/main/resources/keystores/kserver.keystore
--------------------------------------------------------------------------------
/src/main/resources/keystores/tserver.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lance2038/springbootNettySslSocketDemo/HEAD/src/main/resources/keystores/tserver.keystore
--------------------------------------------------------------------------------
/src/test/java/com/sslsocketclient/res/kclient.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lance2038/springbootNettySslSocketDemo/HEAD/src/test/java/com/sslsocketclient/res/kclient.keystore
--------------------------------------------------------------------------------
/src/test/java/com/sslsocketclient/res/tclient.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lance2038/springbootNettySslSocketDemo/HEAD/src/test/java/com/sslsocketclient/res/tclient.keystore
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/common/constant/Globals.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.common.constant;
2 |
3 |
4 | /**
5 | * 全局静态变量类
6 | */
7 | public class Globals
8 | {
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/common/exception/BaseException.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.common.exception;
2 |
3 | /**
4 | * 异常处理类的基类
5 | */
6 | public class BaseException extends RuntimeException
7 | {
8 | }
9 |
--------------------------------------------------------------------------------
/src/test/java/com/lance/BaseTests.java:
--------------------------------------------------------------------------------
1 | package com.lance;
2 |
3 | import org.junit.Test;
4 |
5 | public class BaseTests
6 | {
7 | @Test
8 | public void test()
9 | {
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/dao/ApiDao.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.dao;
2 |
3 |
4 | import com.lance.api.common.base.dao.BaseDao;
5 | import org.springframework.stereotype.Repository;
6 |
7 | /**
8 | *
业务处理dao层
9 | *
10 | * @author lance
11 | * @since 2018-09-25
12 | **/
13 | @Repository
14 | public class ApiDao extends BaseDao
15 | {
16 | }
17 |
--------------------------------------------------------------------------------
/src/test/java/com/lance/ApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.lance;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ApplicationTests
11 | {
12 | @Test
13 | public void contextLoads()
14 | {
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/pojo/model/FieldNameModel.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.pojo.model;
2 |
3 | /**
4 | * 返回信息model
5 | *
6 | * @author lance
7 | * @since 2018-10-17
8 | */
9 | public interface FieldNameModel
10 | {
11 | /**
12 | * 记录数
13 | */
14 | public static String RECORD_COUNT = "recordCount";
15 | /**
16 | * 金额
17 | */
18 | public static String RECORD_AMT = "recordAmt";
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/resources/config/application.yaml:
--------------------------------------------------------------------------------
1 | # spring config
2 | spring:
3 | profiles:
4 | active: dev
5 | main:
6 | banner-mode: console
7 | output:
8 | ansi:
9 | enabled: detect
10 | #set response encoding
11 | http:
12 | encoding:
13 | force: true
14 | # logging config
15 | logging:
16 | register-shutdown-hook: true
17 | # thread Pool
18 | executor:
19 | corePoolSize: 10
20 | maxPoolSize: 200
21 | queueCapacity: 10
22 | namePrefix: AsyncExecutor-
23 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/common/constant/ServerCode.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.common.constant;
2 |
3 | /**
4 | * 业务编码
5 | */
6 | public class ServerCode
7 | {
8 | /**
9 | * 查询
10 | */
11 | public static final String SERVER_CODE_QUERY = "200001";
12 |
13 | /**
14 | * 保存
15 | */
16 | public static final String SERVER_CODE_SAVE = "200002";
17 |
18 | /**
19 | * 校验
20 | */
21 | public static final String SERVER_CODE_CHECK = "200011";
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/pojo/model/ReturnModel.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.pojo.model;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 | import lombok.experimental.Accessors;
7 |
8 | import java.util.Map;
9 |
10 | @Data
11 | @NoArgsConstructor
12 | @AllArgsConstructor
13 | @Accessors(chain = true)
14 | public class ReturnModel
15 | {
16 | private String servCode;
17 | private String msgId;
18 | private Map map;
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/pojo/dto/SampleModel.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.pojo.dto;
2 |
3 |
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | import java.math.BigDecimal;
9 |
10 | /**
11 | * @author lance
12 | * @since 2018-09-25
13 | **/
14 | @Data
15 | @NoArgsConstructor
16 | @AllArgsConstructor
17 | public class SampleModel
18 | {
19 | /**
20 | * 总数
21 | */
22 | public int count;
23 | /**
24 | * 总额
25 | */
26 | public BigDecimal amt;
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/pojo/model/EntryModel.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.pojo.model;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 |
8 | /**
9 | * @author lance
10 | * @version v0.0.1
11 | * @describe 接收报文
12 | * @since 2018/10/11
13 | **/
14 | @Data
15 | @NoArgsConstructor
16 | @AllArgsConstructor
17 | public class EntryModel
18 | {
19 | private String head;
20 | private String body;
21 | private EntryHeadModel headModel;
22 | private EntryBodyModel bodyModel;
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/service/wrap/core/SameServiceWrapper.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.service.wrap.core;
2 |
3 |
4 | import com.lance.api.business.pojo.model.EntryModel;
5 |
6 | /**
7 | * 判断多个业务是否有相同的servCode,若存在相同情况需继承此类重写此方法,改为return true
8 | *
9 | * @author lance
10 | * @since 2018-10-15
11 | */
12 | public class SameServiceWrapper
13 | {
14 |
15 | /**
16 | * 判断是否存在servCode相同的情况
17 | *
18 | * @return
19 | */
20 | public Boolean hasSameService()
21 | {
22 | return false;
23 | }
24 |
25 |
26 | public boolean sameIsMyDo(EntryModel entryModel)
27 | {
28 | return false;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/common/core/annotation/Validation.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.common.core.annotation;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.Target;
5 |
6 | import static java.lang.annotation.ElementType.FIELD;
7 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
8 |
9 | @Target({FIELD})
10 | @Retention(RUNTIME)
11 | public @interface Validation
12 | {
13 | // 允许的值
14 | String[] allowValue() default {};
15 |
16 | // 限制的值
17 | String[] limitValue() default {};
18 |
19 | // 必须为空
20 | boolean mustEmpty() default false;
21 |
22 | // 若值为限制值的返回信息
23 | String limitMsg() default "校验未通过";
24 |
25 | }
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/service/ApiService.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.service;
2 |
3 |
4 | import com.lance.api.common.base.model.JsonResult;
5 |
6 | /**
7 | *
业务处理service接口
8 | *
9 | * @author lance
10 | * @since 2018-10-15
11 | **/
12 | public interface ApiService
13 | {
14 | /**
15 | * 获取信息
16 | *
17 | * @param key 标识符
18 | * @return 查询结果
19 | * @throws Exception
20 | */
21 | JsonResult queryInfo(String key) throws Exception;
22 |
23 |
24 | /**
25 | * 保存信息
26 | *
27 | * @param key 标识
28 | * @param msg 信息
29 | * @return 存储结果
30 | * @throws Exception
31 | */
32 | JsonResult saveData(String key, String msg) throws Exception;
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/dao/SysDao.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.dao;
2 |
3 | import com.lance.api.common.base.dao.BaseDao;
4 | import org.springframework.stereotype.Repository;
5 |
6 | @Repository
7 | public class SysDao extends BaseDao
8 | {
9 | /**
10 | * 获取当前系统时间
11 | *
12 | * @return
13 | */
14 | public String getDBCurrentTime()
15 | {
16 | String sql = "select to_char(sysdate,'yyyy-MM-dd hh24:mi:ss') from dual";
17 | return queryForString(sql);
18 | }
19 |
20 | /**
21 | * 获取当前系统日期
22 | *
23 | * @return
24 | */
25 | public String getDBCurrentDate()
26 | {
27 | String sql = "select to_char(sysdate,'yyyy-MM-dd') from dual";
28 | return queryForString(sql);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/pojo/model/EntryBodyModel.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.pojo.model;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 |
8 | /**
9 | * @author lance
10 | * @version v0.0.1
11 | * @describe 接收报文-包体:是报文数据主体内容,承载了报文的业务数据,具体每个报文包含了哪些业务数据将在具体的业务接口中详细描述
12 | * @since 2018/10/11
13 | **/
14 | @Data
15 | @NoArgsConstructor
16 | @AllArgsConstructor
17 | public class EntryBodyModel
18 | {
19 | /**
20 | * 查询条件的类别
21 | */
22 | private String queryType;
23 | /**
24 | * 对应查询类别的查询条件。
25 | */
26 | private String queryValue;
27 | /**
28 | * 唯一标识
29 | */
30 | private String key;
31 | /**
32 | * 预留
33 | */
34 | private String extend;
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/pojo/model/EntryHeadModel.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.pojo.model;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 |
8 | /**
9 | * @author lance
10 | * @version v0.0.1
11 | * @describe 接收报文--包头:是报文数据头信息,记录了一系列约定数据
12 | * @since 2018/10/11
13 | **/
14 | @Data
15 | @NoArgsConstructor
16 | @AllArgsConstructor
17 | public class EntryHeadModel
18 | {
19 | /**
20 | * 报文id
21 | */
22 | private String msgId;
23 | /**
24 | * 报文的发送时间,时间格式为 yyyyMMddHH24miss
25 | */
26 | private String msgTime;
27 | /**
28 | * 服务编号。详见各个接口
29 | */
30 | private String servCode;
31 | /**
32 | * 报文的版本号
33 | */
34 | private String version;
35 | /**
36 | * 预留字段
37 | */
38 | private String extend;
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/service/wrap/core/BaseServiceWrapper.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.service.wrap.core;
2 |
3 | import java.util.Map;
4 |
5 | /**
6 | *
包装类基类
7 | *
8 | * @author lance
9 | * @since 2018-10-15
10 | */
11 | public abstract class BaseServiceWrapper extends SameServiceWrapper
12 | {
13 | /**
14 | * 获取当前servCode
15 | *
16 | * @return
17 | */
18 | public abstract String doCode();
19 |
20 | /**
21 | * 传入的servCode是否为当前包装类的servCode
22 | *
23 | * @param doCode
24 | * @return
25 | */
26 | public Boolean isMyDo(String doCode)
27 | {
28 | return doCode.equals(doCode());
29 | }
30 |
31 | /**
32 | * 处理业务
33 | *
34 | * @param params
35 | * @return
36 | * @throws Exception
37 | */
38 | public abstract Map handle(Object... params) throws Exception;
39 | }
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # socket/sslSocket + Netty/原生socket 整合的Demo,可任意切换
2 | # CSDN地址:https://blog.csdn.net/sinat_30637097/article/details/87971873
3 | **关于项目中使用的4个证书,使用Java自带的keytool命令,在命令行生成:**
4 | _1、生成服务器端私钥kserver.keystore文件_
5 | `keytool -genkey -alias serverkey -validity 1 -keystore kserver.keystore`
6 | _2、根据私钥,导出服务器端安全证书_
7 | `keytool -export -alias serverkey -keystore kserver.keystore -file server.crt`
8 | _3、将服务器端证书,导入到客户端的Trust KeyStore中_
9 | `keytool -import -alias serverkey -file server.crt -keystore tclient.keystore`
10 | _4、生成客户端私钥kclient.keystore文件_
11 | `keytool -genkey -alias clientkey -validity 1 -keystore kclient.keystore`
12 | _5、根据私钥,导出客户端安全证书_
13 | `keytool -export -alias clientkey -keystore kclient.keystore -file client.crt`
14 | _6、将客户端证书,导入到服务器端的Trust KeyStore中_
15 | `keytool -import -alias clientkey -file client.crt -keystore tserver.keystore`
16 | **生成的文件分成两组,服务器端保存:kserver.keystore tserver.keystore 客户端保存:kclient.keystore tclient.kyestore**
17 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/socketserver/netty/MessageEncoder.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.socketserver.netty;
2 |
3 | import com.lance.api.business.pojo.model.ReturnModel;
4 | import com.lance.api.business.util.ComposeUtil;
5 | import io.netty.buffer.ByteBuf;
6 | import io.netty.channel.ChannelHandlerContext;
7 | import io.netty.handler.codec.MessageToByteEncoder;
8 | import org.springframework.stereotype.Component;
9 |
10 | /**
11 | * 报文组装
12 | *
13 | * @author lance
14 | */
15 | public class MessageEncoder extends MessageToByteEncoder
16 | {
17 | @Override
18 | protected void encode(ChannelHandlerContext channelHandlerContext, ReturnModel returnModel, ByteBuf out) throws Exception
19 | {
20 | byte[] bytes = null;
21 | try
22 | {
23 | bytes = ComposeUtil.doCanProcess(returnModel.getMap(), returnModel.getServCode(), returnModel.getMsgId());
24 | }
25 | catch (Exception e)
26 | {
27 | e.printStackTrace();
28 | }
29 | out.writeBytes(bytes);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/service/wrap/config/InitServiceWrapper.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.service.wrap.config;
2 |
3 | import com.lance.api.business.service.wrap.business.CheckWrapper;
4 | import com.lance.api.business.service.wrap.business.InquiryWrapper;
5 | import com.lance.api.business.service.wrap.business.SaveWrapper;
6 | import com.lance.api.business.service.wrap.core.ServiceWrapperContainer;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 |
10 | /**
11 | * 加载wrappers
12 | *
13 | * @author lance
14 | * @since 2018-10-15
15 | */
16 | @Configuration
17 | public class InitServiceWrapper
18 | {
19 | @Bean("serviceWrapperContainer")
20 | public ServiceWrapperContainer initWrapper(InquiryWrapper inquiryWrapper, SaveWrapper saveWrapper, CheckWrapper checkWrapper)
21 | {
22 | ServiceWrapperContainer serviceWrapper = new ServiceWrapperContainer();
23 | serviceWrapper.addService(inquiryWrapper);
24 | serviceWrapper.addService(saveWrapper);
25 | serviceWrapper.addService(checkWrapper);
26 | return serviceWrapper;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/service/wrap/business/CheckWrapper.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.service.wrap.business;
2 |
3 | import com.lance.api.business.service.wrap.core.BaseServiceWrapper;
4 | import com.lance.api.common.base.model.Communication;
5 | import com.lance.api.common.constant.ServerCode;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.springframework.stereotype.Component;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * 校验包装类
13 | *
14 | * @author lance
15 | */
16 | @Slf4j
17 | @Component
18 | public class CheckWrapper extends BaseServiceWrapper
19 | {
20 |
21 | /**
22 | * 此业务包装类的servCode
23 | */
24 | private final String servCode = ServerCode.SERVER_CODE_CHECK;
25 |
26 | /**
27 | * 获取当前servCode
28 | *
29 | * @return
30 | */
31 | @Override
32 | public String doCode()
33 | {
34 | return servCode;
35 | }
36 |
37 | /**
38 | * 处理业务--校验
39 | *
40 | * @param params
41 | * @return
42 | * @throws Exception
43 | */
44 | @Override
45 | public Map handle(Object... params) throws Exception
46 | {
47 | // 返回结果map
48 | return Communication.getInstance().getSuccessMap();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/lance/api/business/socketserver/netty/MessageDecoder.java:
--------------------------------------------------------------------------------
1 | package com.lance.api.business.socketserver.netty;
2 |
3 | import com.lance.api.business.pojo.model.EntryModel;
4 | import com.lance.api.business.util.ByteDataBuffer;
5 | import com.lance.api.business.util.ComposeUtil;
6 | import io.netty.buffer.ByteBuf;
7 | import io.netty.channel.ChannelHandler;
8 | import io.netty.channel.ChannelHandlerContext;
9 | import io.netty.handler.codec.ByteToMessageDecoder;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.stereotype.Component;
12 |
13 | import java.util.List;
14 |
15 | /**
16 | * 报文解码
17 | *
18 | * @author lance
19 | */
20 | public class MessageDecoder extends ByteToMessageDecoder
21 | {
22 |
23 | /**
24 | * 从ByteBuf中获取字节,转换成对象
25 | *
26 | * @param ctx
27 | * @param buffer
28 | * @param out
29 | * @throws Exception
30 | */
31 | @Override
32 | protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List