|
@@ -1,8 +1,9 @@
|
|
|
package com.jubotech.framework.netty.handler.websocket;
|
|
|
-
|
|
|
+import java.net.InetSocketAddress;
|
|
|
import java.util.Base64;
|
|
|
-
|
|
|
+import java.util.List;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
@@ -31,6 +32,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class DeviceAuthReqWebsocketHandler implements JsonMessageHandler{
|
|
|
+ @Value("#{'${internalcode.whitelist:}'.split(',')}")
|
|
|
+ private List<String> whitelist;
|
|
|
@Autowired
|
|
|
private CustomerService customerService;
|
|
|
@Autowired
|
|
@@ -55,52 +58,127 @@ public class DeviceAuthReqWebsocketHandler implements JsonMessageHandler{
|
|
|
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
return;
|
|
|
}
|
|
|
- // 1、校验用户信息
|
|
|
- if (!req.getAuthType().equals(EnumAuthType.Username)) {// 用户名密码方式(此方式Credential应传入base64(user:pwd))
|
|
|
+// // 1、校验用户信息
|
|
|
+// if (!req.getAuthType().equals(EnumAuthType.Username)) {// 用户名密码方式(此方式Credential应传入base64(user:pwd))
|
|
|
+// MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_VERIFYWAY);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// byte[] byteArray = Base64.getDecoder().decode(req.getCredential());
|
|
|
+// String str = new String(byteArray);
|
|
|
+// if (StringUtils.isEmpty(str)) {
|
|
|
+// MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// log.debug("账号密码登陆:name=" + str);
|
|
|
+// String[] strs = str.split(":");
|
|
|
+// if (strs == null || strs.length < 1 || strs[0] == null) {
|
|
|
+// MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// // pc客服端
|
|
|
+// AccountInfo user = accountService.clientlogin(strs[0], strs[1]);
|
|
|
+// if (null == user) {
|
|
|
+// MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_LOGINFAIL);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (user.getState() != 1) {// 账号状态正常的时候
|
|
|
+// MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight,Constant.ERROR_MSG_LOGINNORIGHT);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// // 判断是否有登陆,如果有登陆直接踢下线
|
|
|
+// ChannelHandlerContext chc = nettyConnectionUtil.getClientChannelHandlerContextByUserId(user.getAccount());
|
|
|
+// if (null != chc) {
|
|
|
+// MessageUtil.sendJsonErrMsg(chc, EnumErrorCode.NoRight,Constant.ERROR_MSG_ELSEWHERELOGINN);//账号已在别处登陆
|
|
|
+// chc.close();
|
|
|
+// }
|
|
|
+
|
|
|
+// // 生成用户token信息
|
|
|
+// String token = nettyConnectionUtil.getNettyId(ctx);
|
|
|
+//
|
|
|
+// // 存储 用户id 和 通道信息
|
|
|
+// nettyConnectionUtil.saveDeviceChannel(ctx, user.getAccount());
|
|
|
+// // 存储微信全局id 与通道
|
|
|
+// nettyConnectionUtil.registerUserid(user.getAccount(), ctx);
|
|
|
+//
|
|
|
+// CustomerInfo customer = customerService.getCustomerInfoByid(user.getCid());
|
|
|
+ if (req.getAuthType().equals(EnumAuthType.Username)){
|
|
|
+ // 1、校验用户信息
|
|
|
+// if (!req.getAuthType().equals(EnumAuthType.Username)) {// 用户名密码方式(此方式Credential应传入base64(user:pwd))
|
|
|
+// MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_VERIFYWAY);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+ byte[] byteArray = Base64.getDecoder().decode(req.getCredential());
|
|
|
+ String str = new String(byteArray);
|
|
|
+ if (StringUtils.isEmpty(str)) {
|
|
|
+ MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.debug("账号密码登录:name=" + str);
|
|
|
+ String[] strs = str.split(":");
|
|
|
+ if (strs == null || strs.length < 1 || strs[0] == null) {
|
|
|
+ MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // pc客服端
|
|
|
+ AccountInfo user = accountService.clientlogin(strs[0], strs[1]);
|
|
|
+ if (null == user) {
|
|
|
+ MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_LOGINFAIL);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (user.getState() != 1) {// 账号状态正常的时候
|
|
|
+ MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight,Constant.ERROR_MSG_LOGINNORIGHT);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 判断是否有登录,如果有登录直接踢下线
|
|
|
+ ChannelHandlerContext chc = nettyConnectionUtil.getClientChannelHandlerContextByUserId(user.getAccount());
|
|
|
+ if (null != chc) {
|
|
|
+ MessageUtil.sendJsonErrMsg(chc, EnumErrorCode.NoRight,Constant.ERROR_MSG_ELSEWHERELOGINN);//账号已在别处登录
|
|
|
+ chc.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成用户token信息
|
|
|
+ String token = nettyConnectionUtil.getNettyId(ctx);
|
|
|
+
|
|
|
+ // 存储 用户id 和 通道信息
|
|
|
+ nettyConnectionUtil.saveDeviceChannel(ctx, user.getAccount());
|
|
|
+ // 存储微信全局id 与通道
|
|
|
+ nettyConnectionUtil.registerUserid(user.getAccount(), ctx);
|
|
|
+
|
|
|
+ CustomerInfo customer = customerService.getCustomerInfoByid(user.getCid());
|
|
|
+
|
|
|
+ sendMsg(customer, user, token, ctx, vo);
|
|
|
+ } else if (req.getAuthType().equals(EnumAuthType.InternalCode)) {
|
|
|
+ // 获取客户端ip地址
|
|
|
+ InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
|
+ String clientIP = insocket.getAddress().getHostAddress();
|
|
|
+ Number clientPort = insocket.getPort();
|
|
|
+ String clientAddress = clientIP + ":" + clientPort;
|
|
|
+ log.debug("内部登录:address=" + clientAddress);
|
|
|
+ log.debug("白名单 :" + this.whitelist);
|
|
|
+
|
|
|
+ if (!this.whitelist.contains(clientIP)){
|
|
|
+ log.debug("非法设备");
|
|
|
+ MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_ILLEGALDEVICE);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成用户token信息
|
|
|
+ String token = nettyConnectionUtil.getNettyId(ctx);
|
|
|
+
|
|
|
+ // 存储 ip:port 和 通道信息
|
|
|
+ nettyConnectionUtil.saveDeviceChannel(ctx, clientAddress);
|
|
|
+
|
|
|
+ // 内部白名单,都认为是管理连接
|
|
|
+ nettyConnectionUtil.saveAdminChannel(ctx, clientAddress);
|
|
|
+
|
|
|
+
|
|
|
+ sendMsg(null, null, token, ctx, vo);
|
|
|
+ } else {
|
|
|
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_VERIFYWAY);
|
|
|
- return;
|
|
|
- }
|
|
|
- byte[] byteArray = Base64.getDecoder().decode(req.getCredential());
|
|
|
- String str = new String(byteArray);
|
|
|
- if (StringUtils.isEmpty(str)) {
|
|
|
- MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
- return;
|
|
|
- }
|
|
|
- log.debug("账号密码登陆:name=" + str);
|
|
|
- String[] strs = str.split(":");
|
|
|
- if (strs == null || strs.length < 1 || strs[0] == null) {
|
|
|
- MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
|
- return;
|
|
|
- }
|
|
|
- // pc客服端
|
|
|
- AccountInfo user = accountService.clientlogin(strs[0], strs[1]);
|
|
|
- if (null == user) {
|
|
|
- MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight, Constant.ERROR_MSG_LOGINFAIL);
|
|
|
- return;
|
|
|
}
|
|
|
-
|
|
|
- if (user.getState() != 1) {// 账号状态正常的时候
|
|
|
- MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.NoRight,Constant.ERROR_MSG_LOGINNORIGHT);
|
|
|
- return;
|
|
|
- }
|
|
|
- // 判断是否有登陆,如果有登陆直接踢下线
|
|
|
- ChannelHandlerContext chc = nettyConnectionUtil.getClientChannelHandlerContextByUserId(user.getAccount());
|
|
|
- if (null != chc) {
|
|
|
- MessageUtil.sendJsonErrMsg(chc, EnumErrorCode.NoRight,Constant.ERROR_MSG_ELSEWHERELOGINN);//账号已在别处登陆
|
|
|
- chc.close();
|
|
|
- }
|
|
|
-
|
|
|
- // 生成用户token信息
|
|
|
- String token = nettyConnectionUtil.getNettyId(ctx);
|
|
|
-
|
|
|
- // 存储 用户id 和 通道信息
|
|
|
- nettyConnectionUtil.saveDeviceChannel(ctx, user.getAccount());
|
|
|
- // 存储微信全局id 与通道
|
|
|
- nettyConnectionUtil.registerUserid(user.getAccount(), ctx);
|
|
|
-
|
|
|
- CustomerInfo customer = customerService.getCustomerInfoByid(user.getCid());
|
|
|
-
|
|
|
- sendMsg(customer, user, token, ctx, vo);
|
|
|
+// sendMsg(customer, user, token, ctx, vo);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
|
|
@@ -110,19 +188,41 @@ public class DeviceAuthReqWebsocketHandler implements JsonMessageHandler{
|
|
|
private void sendMsg(CustomerInfo customer, AccountInfo user, String token, ChannelHandlerContext ctx,
|
|
|
TransportMessage vo) {
|
|
|
|
|
|
- Integer supplierid = customer.getId();
|
|
|
- String suppliername = customer.getSuppliername();// 商家名称
|
|
|
- long unionid = user.getId();// 个人账号id
|
|
|
- String nickname = user.getNickname();// 昵称
|
|
|
+// Integer supplierid = customer.getId();
|
|
|
+// String suppliername = customer.getSuppliername();// 商家名称
|
|
|
+// long unionid = user.getId();// 个人账号id
|
|
|
+// String nickname = user.getNickname();// 昵称
|
|
|
|
|
|
ExtraMessage.Builder buider = ExtraMessage.newBuilder();
|
|
|
- buider.setSupplierId(supplierid);
|
|
|
- buider.setSupplierName(suppliername);
|
|
|
- buider.setAccountType(EnumAccountType.SubUser);// 账号类型 子账号
|
|
|
-
|
|
|
- if (0 != unionid && !StringUtils.isEmpty(nickname)) {
|
|
|
- buider.setUnionId(unionid);
|
|
|
- buider.setNickName(nickname);
|
|
|
+// buider.setSupplierId(supplierid);
|
|
|
+// buider.setSupplierName(suppliername);
|
|
|
+// buider.setAccountType(EnumAccountType.SubUser);// 账号类型 子账号
|
|
|
+//
|
|
|
+// if (0 != unionid && !StringUtils.isEmpty(nickname)) {
|
|
|
+// buider.setUnionId(unionid);
|
|
|
+// buider.setNickName(nickname);
|
|
|
+// }
|
|
|
+
|
|
|
+ if (null == customer) {
|
|
|
+ buider.setSupplierId(1982);
|
|
|
+ buider.setSupplierName("微信客服系统管理连接");
|
|
|
+ buider.setAccountType(EnumAccountType.UnknownAccountType);//账号类型 子账号
|
|
|
+ }else{
|
|
|
+ Integer supplierid = customer.getId();
|
|
|
+ String suppliername = customer.getSuppliername();// 商家名称
|
|
|
+
|
|
|
+
|
|
|
+ buider.setSupplierId(supplierid);
|
|
|
+ buider.setSupplierName(suppliername+"|"+user.getLevel());
|
|
|
+ buider.setAccountType(EnumAccountType.SubUser);// 账号类型 子账号
|
|
|
+ }
|
|
|
+ if (null != user) {
|
|
|
+ long unionid = user.getId();// 个人账号id
|
|
|
+ String nickname = user.getNickname();// 昵称
|
|
|
+ if (0 != unionid && !StringUtils.isEmpty(nickname)) {
|
|
|
+ buider.setUnionId(unionid);
|
|
|
+ buider.setNickName(nickname);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
ExtraMessage ext = buider.build();
|