mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-24 16:51:45 +08:00
1、websocket鉴权失败时响应ReplyBody,告知客户端鉴权失败
This commit is contained in:
parent
7579845369
commit
461be0fab1
Binary file not shown.
@ -13,7 +13,7 @@ import java.util.function.Predicate;
|
||||
public class HandshakePredicate implements Predicate<HandshakeEvent> {
|
||||
|
||||
/**
|
||||
*
|
||||
* 验证身份信息,本方法切勿进行耗时操作!!!
|
||||
* @param event
|
||||
* @return true验证通过 false验证失败
|
||||
*/
|
||||
|
@ -29,18 +29,25 @@
|
||||
function onReplyReceived(reply)
|
||||
{
|
||||
console.log(reply);
|
||||
if(reply.key==='client_bind' && reply.code === "200" )
|
||||
{
|
||||
hideProcess();
|
||||
|
||||
$('#LoginDialog').fadeOut();
|
||||
if (reply.key === KEY_CLIENT_BIND && reply.code === CODE_OK) {
|
||||
hideProcess();
|
||||
|
||||
$('#LoginDialog').fadeOut();
|
||||
|
||||
$('#MessageDialog').fadeIn();
|
||||
$('#MessageDialog').addClass("in");
|
||||
$("#current_account").text($('#account').val());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接鉴权失败
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
hideProcess();
|
||||
showETip("鉴权失败");
|
||||
}
|
||||
|
||||
|
||||
$('#MessageDialog').fadeIn();
|
||||
$('#MessageDialog').addClass("in");
|
||||
$("#current_account").text($('#account').val());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** 当收到消息时候回调 **/
|
||||
|
@ -21,6 +21,16 @@ const REPLY_BODY = 4;
|
||||
const SENT_BODY = 3;
|
||||
const PING = 1;
|
||||
const PONG = 0;
|
||||
|
||||
/*
|
||||
* 握手鉴权常量
|
||||
*/
|
||||
const KEY_HANDSHAKE = "client_handshake";
|
||||
const CODE_UNAUTHORIZED = "401";
|
||||
|
||||
const CODE_OK = "200";
|
||||
const KEY_CLIENT_BIND = "client_bind";
|
||||
|
||||
/**
|
||||
* PONG字符串转换后
|
||||
* @type {Uint8Array}
|
||||
@ -54,7 +64,7 @@ CIMPushManager.bind = function (account) {
|
||||
|
||||
let browser = getBrowser();
|
||||
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
|
||||
body.setKey("client_bind");
|
||||
body.setKey(KEY_CLIENT_BIND);
|
||||
body.setTimestamp(new Date().getTime());
|
||||
body.getDataMap().set("uid", account);
|
||||
body.getDataMap().set("channel", APP_CHANNEL);
|
||||
@ -106,7 +116,8 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
|
||||
if (type === REPLY_BODY) {
|
||||
let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body);
|
||||
/**
|
||||
|
||||
/*
|
||||
* 将proto对象转换成json对象,去除无用信息
|
||||
*/
|
||||
let reply = {};
|
||||
@ -116,13 +127,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
reply.timestamp = message.getTimestamp();
|
||||
reply.data = {};
|
||||
|
||||
/**
|
||||
/*
|
||||
* 注意,遍历map这里的参数 value在前key在后
|
||||
*/
|
||||
message.getDataMap().forEach(function (v, k) {
|
||||
reply.data[k] = v;
|
||||
});
|
||||
|
||||
/*
|
||||
* 判断是否是握手鉴权失败
|
||||
* 终止后续自动重连
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
manualStop = true;
|
||||
}
|
||||
|
||||
onReplyReceived(reply);
|
||||
}
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*CIM服务器IP*/
|
||||
const CIM_HOST = "127.0.0.1";
|
||||
const CIM_HOST = window.location.hostname;
|
||||
/*
|
||||
*服务端 websocket端口
|
||||
*/
|
||||
@ -7,7 +7,7 @@ const CIM_PORT = 34567;
|
||||
const CIM_URI = "ws://" + CIM_HOST + ":" + CIM_PORT;
|
||||
|
||||
const APP_VERSION = "1.0.0";
|
||||
const APP_CHANNEL = "browser";
|
||||
const APP_CHANNEL = "web";
|
||||
const APP_PACKAGE = "com.farsunset.cim";
|
||||
|
||||
/*
|
||||
@ -18,6 +18,25 @@ const DATA_HEADER_LENGTH = 1;
|
||||
|
||||
const MESSAGE = 2;
|
||||
const REPLY_BODY = 4;
|
||||
const SENT_BODY = 3;
|
||||
const PING = 1;
|
||||
const PONG = 0;
|
||||
|
||||
/*
|
||||
* 握手鉴权常量
|
||||
*/
|
||||
const KEY_HANDSHAKE = "client_handshake";
|
||||
const CODE_UNAUTHORIZED = "401";
|
||||
|
||||
const CODE_OK = "200";
|
||||
const KEY_CLIENT_BIND = "client_bind";
|
||||
|
||||
/**
|
||||
* PONG字符串转换后
|
||||
* @type {Uint8Array}
|
||||
*/
|
||||
const PONG_BODY = new Uint8Array([80,79,78,71]);
|
||||
|
||||
|
||||
let socket;
|
||||
let manualStop = false;
|
||||
@ -33,27 +52,28 @@ CIMPushManager.connect = function () {
|
||||
socket.onclose = CIMPushManager.innerOnConnectionClosed;
|
||||
};
|
||||
|
||||
CIMPushManager.bindAccount = function (account) {
|
||||
CIMPushManager.bind = function (account) {
|
||||
|
||||
window.localStorage.account = account;
|
||||
|
||||
let deviceId = window.localStorage.deviceIddeviceId;
|
||||
if (deviceId == '' || deviceId == undefined) {
|
||||
let deviceId = window.localStorage.deviceId;
|
||||
if (deviceId === '' || deviceId === undefined) {
|
||||
deviceId = generateUUID();
|
||||
window.localStorage.deviceId = deviceId;
|
||||
}
|
||||
|
||||
let browser = getBrowser();
|
||||
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
|
||||
body.setKey("client_bind");
|
||||
body.setKey(KEY_CLIENT_BIND);
|
||||
body.setTimestamp(new Date().getTime());
|
||||
body.getDataMap().set("account", account);
|
||||
body.getDataMap().set("uid", account);
|
||||
body.getDataMap().set("channel", APP_CHANNEL);
|
||||
body.getDataMap().set("appVersion", APP_VERSION);
|
||||
body.getDataMap().set("osVersion", browser.version);
|
||||
body.getDataMap().set("packageName", APP_PACKAGE);
|
||||
body.getDataMap().set("deviceId", deviceId);
|
||||
body.getDataMap().set("device", browser.name);
|
||||
body.getDataMap().set("deviceName", browser.name);
|
||||
body.getDataMap().set("language", navigator.language);
|
||||
CIMPushManager.sendRequest(body);
|
||||
};
|
||||
|
||||
@ -83,15 +103,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
let type = data[0];
|
||||
let body = data.subarray(DATA_HEADER_LENGTH, data.length);
|
||||
|
||||
if (type == MESSAGE) {
|
||||
if (type === PING) {
|
||||
CIMPushManager.pong();
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === MESSAGE) {
|
||||
let message = proto.com.farsunset.cim.sdk.web.model.Message.deserializeBinary(body);
|
||||
onInterceptMessageReceived(message.toObject(false));
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == REPLY_BODY) {
|
||||
if (type === REPLY_BODY) {
|
||||
let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body);
|
||||
/**
|
||||
|
||||
/*
|
||||
* 将proto对象转换成json对象,去除无用信息
|
||||
*/
|
||||
let reply = {};
|
||||
@ -101,13 +127,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
reply.timestamp = message.getTimestamp();
|
||||
reply.data = {};
|
||||
|
||||
/**
|
||||
/*
|
||||
* 注意,遍历map这里的参数 value在前key在后
|
||||
*/
|
||||
message.getDataMap().forEach(function (v, k) {
|
||||
reply.data[k] = v;
|
||||
});
|
||||
|
||||
/*
|
||||
* 判断是否是握手鉴权失败
|
||||
* 终止后续自动重连
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
manualStop = true;
|
||||
}
|
||||
|
||||
onReplyReceived(reply);
|
||||
}
|
||||
};
|
||||
@ -123,16 +157,24 @@ CIMPushManager.innerOnConnectionClosed = function (e) {
|
||||
|
||||
CIMPushManager.sendRequest = function (body) {
|
||||
let data = body.serializeBinary();
|
||||
let protobuf = new Uint8Array(data.length);
|
||||
protobuf.set(data, 0);
|
||||
let protobuf = new Uint8Array(data.length + 1);
|
||||
protobuf[0] = SENT_BODY;
|
||||
protobuf.set(data, 1);
|
||||
socket.send(protobuf);
|
||||
};
|
||||
|
||||
CIMPushManager.pong = function () {
|
||||
let pong = new Uint8Array(PONG_BODY.byteLength + 1);
|
||||
pong[0] = PONG;
|
||||
pong.set(PONG_BODY,1);
|
||||
socket.send(pong);
|
||||
};
|
||||
|
||||
function onInterceptMessageReceived(message) {
|
||||
/*
|
||||
*被强制下线之后,不再继续连接服务端
|
||||
*/
|
||||
if (message.action == ACTION_999) {
|
||||
if (message.action === ACTION_999) {
|
||||
manualStop = true;
|
||||
}
|
||||
/*
|
||||
@ -174,7 +216,7 @@ function generateUUID() {
|
||||
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
let r = (d + Math.random() * 16) % 16 | 0;
|
||||
d = Math.floor(d / 16);
|
||||
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
||||
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
||||
});
|
||||
return uuid.replace(/-/g, '');
|
||||
}
|
||||
|
@ -33,4 +33,6 @@ public interface CIMConstant {
|
||||
|
||||
String CLIENT_CONNECT_CLOSED = "client_closed";
|
||||
|
||||
String CLIENT_HANDSHAKE = "client_handshake";
|
||||
|
||||
}
|
||||
|
@ -21,12 +21,13 @@
|
||||
*/
|
||||
package com.farsunset.cim.handshake;
|
||||
|
||||
import com.farsunset.cim.constant.CIMConstant;
|
||||
import com.farsunset.cim.model.ReplyBody;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
@ -36,12 +37,12 @@ import java.util.function.Predicate;
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class HandshakeHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
/*
|
||||
客户端接收到的CloseEvent事件类型
|
||||
客户端可通过该code判断是握手鉴权失败
|
||||
*认证失败,返回replyBody给客户端
|
||||
*/
|
||||
private static final int UNAUTHORIZED_CODE = 4001;
|
||||
private final ReplyBody failedBody = ReplyBody.make(CIMConstant.CLIENT_HANDSHAKE,
|
||||
HttpResponseStatus.UNAUTHORIZED.code(),
|
||||
HttpResponseStatus.UNAUTHORIZED.reasonPhrase());
|
||||
|
||||
private final Predicate<HandshakeEvent> handshakePredicate;
|
||||
|
||||
@ -67,10 +68,10 @@ public class HandshakeHandler extends ChannelInboundHandlerAdapter {
|
||||
}
|
||||
|
||||
/*
|
||||
* 鉴权不通过,关闭链接
|
||||
* 鉴权不通过,发送响应体并关闭链接
|
||||
*/
|
||||
if (!handshakePredicate.test(HandshakeEvent.of(event))) {
|
||||
context.channel().writeAndFlush(new CloseWebSocketFrame(UNAUTHORIZED_CODE,HttpResponseStatus.UNAUTHORIZED.reasonPhrase())).addListener(ChannelFutureListener.CLOSE);
|
||||
context.channel().writeAndFlush(failedBody).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
}
|
||||
}
|
@ -148,4 +148,12 @@ public class ReplyBody implements Serializable, Transportable {
|
||||
public DataType getType() {
|
||||
return DataType.REPLY;
|
||||
}
|
||||
|
||||
public static ReplyBody make(String key,int code,String message){
|
||||
ReplyBody body = new ReplyBody();
|
||||
body.key = key;
|
||||
body.code = String.valueOf(code);
|
||||
body.message = message;
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
@ -26,18 +26,24 @@
|
||||
function onReplyReceived(reply)
|
||||
{
|
||||
console.log(reply);
|
||||
if(reply.key==='client_bind' && reply.code === "200" )
|
||||
{
|
||||
hideProcess();
|
||||
|
||||
$('#LoginDialog').fadeOut();
|
||||
if (reply.key === KEY_CLIENT_BIND && reply.code === CODE_OK) {
|
||||
hideProcess();
|
||||
|
||||
|
||||
$('#MessageDialog').fadeIn();
|
||||
$('#MessageDialog').addClass("in");
|
||||
$("#current_account").text($('#account').val());
|
||||
|
||||
}
|
||||
$('#LoginDialog').fadeOut();
|
||||
|
||||
$('#MessageDialog').fadeIn();
|
||||
$('#MessageDialog').addClass("in");
|
||||
$("#current_account").text($('#account').val());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接鉴权失败
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
hideProcess();
|
||||
showETip("鉴权失败");
|
||||
}
|
||||
}
|
||||
|
||||
/** 当收到消息时候回调 **/
|
||||
|
@ -21,6 +21,16 @@ const REPLY_BODY = 4;
|
||||
const SENT_BODY = 3;
|
||||
const PING = 1;
|
||||
const PONG = 0;
|
||||
|
||||
/*
|
||||
* 握手鉴权常量
|
||||
*/
|
||||
const KEY_HANDSHAKE = "client_handshake";
|
||||
const CODE_UNAUTHORIZED = "401";
|
||||
|
||||
const CODE_OK = "200";
|
||||
const KEY_CLIENT_BIND = "client_bind";
|
||||
|
||||
/**
|
||||
* PONG字符串转换后
|
||||
* @type {Uint8Array}
|
||||
@ -53,7 +63,7 @@ CIMPushManager.bind = function (account) {
|
||||
|
||||
let browser = getBrowser().name;
|
||||
let body = {};
|
||||
body.key ="client_bind";
|
||||
body.key = KEY_CLIENT_BIND;
|
||||
body.timestamp=new Date().getTime();
|
||||
body.data = {};
|
||||
body.data.uid = account;
|
||||
@ -107,6 +117,13 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
|
||||
if (type === REPLY_BODY) {
|
||||
let reply = JSON.parse(body);
|
||||
/*
|
||||
* 判断是否是握手鉴权失败
|
||||
* 终止后续自动重连
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
manualStop = true;
|
||||
}
|
||||
onReplyReceived(reply);
|
||||
}
|
||||
};
|
||||
|
@ -29,18 +29,24 @@
|
||||
function onReplyReceived(reply)
|
||||
{
|
||||
console.log(reply);
|
||||
if(reply.key==='client_bind' && reply.code === "200" )
|
||||
{
|
||||
hideProcess();
|
||||
|
||||
$('#LoginDialog').fadeOut();
|
||||
if (reply.key === KEY_CLIENT_BIND && reply.code === CODE_OK) {
|
||||
hideProcess();
|
||||
|
||||
|
||||
$('#MessageDialog').fadeIn();
|
||||
$('#MessageDialog').addClass("in");
|
||||
$("#current_account").text($('#account').val());
|
||||
|
||||
}
|
||||
$('#LoginDialog').fadeOut();
|
||||
|
||||
$('#MessageDialog').fadeIn();
|
||||
$('#MessageDialog').addClass("in");
|
||||
$("#current_account").text($('#account').val());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接鉴权失败
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
hideProcess();
|
||||
showETip("鉴权失败");
|
||||
}
|
||||
}
|
||||
|
||||
/** 当收到消息时候回调 **/
|
||||
|
@ -21,6 +21,16 @@ const REPLY_BODY = 4;
|
||||
const SENT_BODY = 3;
|
||||
const PING = 1;
|
||||
const PONG = 0;
|
||||
|
||||
/*
|
||||
* 握手鉴权常量
|
||||
*/
|
||||
const KEY_HANDSHAKE = "client_handshake";
|
||||
const CODE_UNAUTHORIZED = "401";
|
||||
|
||||
const CODE_OK = "200";
|
||||
const KEY_CLIENT_BIND = "client_bind";
|
||||
|
||||
/**
|
||||
* PONG字符串转换后
|
||||
* @type {Uint8Array}
|
||||
@ -54,7 +64,7 @@ CIMPushManager.bind = function (account) {
|
||||
|
||||
let browser = getBrowser();
|
||||
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
|
||||
body.setKey("client_bind");
|
||||
body.setKey(KEY_CLIENT_BIND);
|
||||
body.setTimestamp(new Date().getTime());
|
||||
body.getDataMap().set("uid", account);
|
||||
body.getDataMap().set("channel", APP_CHANNEL);
|
||||
@ -63,6 +73,7 @@ CIMPushManager.bind = function (account) {
|
||||
body.getDataMap().set("packageName", APP_PACKAGE);
|
||||
body.getDataMap().set("deviceId", deviceId);
|
||||
body.getDataMap().set("deviceName", browser.name);
|
||||
body.getDataMap().set("language", navigator.language);
|
||||
CIMPushManager.sendRequest(body);
|
||||
};
|
||||
|
||||
@ -105,7 +116,8 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
|
||||
if (type === REPLY_BODY) {
|
||||
let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body);
|
||||
/**
|
||||
|
||||
/*
|
||||
* 将proto对象转换成json对象,去除无用信息
|
||||
*/
|
||||
let reply = {};
|
||||
@ -115,13 +127,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
|
||||
reply.timestamp = message.getTimestamp();
|
||||
reply.data = {};
|
||||
|
||||
/**
|
||||
/*
|
||||
* 注意,遍历map这里的参数 value在前key在后
|
||||
*/
|
||||
message.getDataMap().forEach(function (v, k) {
|
||||
reply.data[k] = v;
|
||||
});
|
||||
|
||||
/*
|
||||
* 判断是否是握手鉴权失败
|
||||
* 终止后续自动重连
|
||||
*/
|
||||
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
|
||||
manualStop = true;
|
||||
}
|
||||
|
||||
onReplyReceived(reply);
|
||||
}
|
||||
};
|
||||
@ -200,4 +220,4 @@ function generateUUID() {
|
||||
});
|
||||
return uuid.replace(/-/g, '');
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user