- 充分的理解 HTTP、Websocket协议之间的关系和区别
- 了解 Redis 的基本用法,并且在实际项目中使用 Redis 处理用户的离线消息
- jdk 11
- mysql 8.0
- redis 6.0.9
- Maven
- sparkjava
- 权限验证
- 登录、注册
- 添加好友、群
- 修改个人信息
- 心跳检测
- 单聊
- 群聊
- 发送图片、视频、表情包
- 好友上线、下线通知
- 添加好友、群通知
- 离线消息
- 消息漫游
ws://127.0.0.1:9000/chat?email=email&token=token
Request
{
"email": "[email protected]",
"password": "hjm0819"
}
Response 200
{
"code": 0,
"msg": "",
"data": {
"id": 29,
"email": "[email protected]",
"uname": "原来是小民同学呀",
"avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"createAt": 1618914288981,
"deleteTimestamp": 0,
"token": "6E3E662DFD10ADB61206342FB8922BEA",
"online": 1
}
}
Request
{
"email": "[email protected]",
"password": "test2021",
"password2": "test2021",
"uname": "test123"
}
Response 200
{
"code": 0,
"data": {
"createAt": 1618993248102,
"deleteTimestamp": 0,
"email": "[email protected]",
"id": 0,
"online": 1,
"token": "548D7B319EF9AB24CD91FBB8031B44E1",
"uname": "test123"
},
"msg": ""
}
Request
{
"info": "hjm",
}
Response 200
{
"code": 0,
"data": [
{
"avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"createAt": 1618914288981,
"deleteTimestamp": 0,
"email": "[email protected]",
"id": 29,
"online": 0,
"uname": "原来是小民同学呀",
}
],
"msg": ""
}
Request
{
"avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"uname": "test456"
}
Response 200
{
"code": 0,
"data": {
"avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"createAt": 1618993248102,
"deleteTimestamp": 0,
"email": "[email protected]",
"id": 0,
"online": 1,
"token": "548D7B319EF9AB24CD91FBB8031B44E1",
"uname": "test456",
},
"msg": ""
}
获取用户信息(包括在线状态) GET /user/user_info
Request
{
"email": "[email protected]"
}
Response 200
{
"code": 0,
"data": {
"avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"createAt": 1618847453422,
"deleteTimestamp": 0,
"email": "[email protected]",
"id": 26,
"uname": "何同学",
"online": 0
},
"msg": ""
}
Request
{
"cmd": 15,
"dstObj": "[email protected]",
"msgType": 6,
"userEmail": "[email protected]"
}
Response 200
{
"cmd": 15,
"content": "[email protected] 添加 [email protected]为好友",
"createAt": 1618994048486,
"deleteTimestamp": 0,
"dstObj": "[email protected]",
"msgType": 6,
"userEmail": "[email protected]"
}
Request 登录成功
Response 200
{
"code": 0,
"data": {
{
"avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"createAt": 1618914288981,
"deleteTimestamp": 0,
"email": "[email protected]",
"id": 29,
"online": 1,
"uname": "原来是小民同学呀"
}
},
"msg": ""
}
Request 登录成功
Response 200
{
"code": 0,
"data": {
{
"cmd": 10,
"content": "@(呵呵)",
"createAt": 1618994297941,
"deleteTimestamp": 0,
"dstObj": "[email protected]",
"id": 1674,
"msgType": 1,
"userEmail": "[email protected]"
},
{
"cmd": 10,
"content": "this is a test",
"createAt": 1618994306254,
"deleteTimestamp": 0,
"dstObj": "[email protected]",
"id": 1675,
"msgType": 1,
"userEmail": "[email protected]"
}
},
"msg": ""
}
获取群消息列表 GET /msg/community_msg_list
Request
{
"dstObj": 1000
}
Response 200
{
"code": 0,
"data": {
{
"cmd": 11,
"content": "@(惊恐)",
"createAt": 1618994465192,
"deleteTimestamp": 0,
"dstObj": "1000",
"id": 1676,
"msgType": 1,
"userEmail": "[email protected]"
},
{
"cmd": 11,
"content": "hello world!",
"createAt": 1618994471825,
"deleteTimestamp": 0,
"dstObj": "1000",
"id": 1677,
"msgType": 1,
"userEmail": "[email protected]"
}
},
"msg": ""
}
Request
{
"info": "软件架构"
}
Response 200
{
"code": 0,
"data": [
{
"createAt": 1618847453422,
"deleteTimestamp": 0,
"icon": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"id": 1000,
"name": "软件架构课程交流群",
"ownerEmail": "[email protected]"
}
],
"msg": ""
}
Request 登录成功
Response 200
{
"code": 0,
"data": [
{
"createAt": 1618847453422,
"deleteTimestamp": 0,
"icon": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
"id": 1000,
"name": "软件架构课程交流群",
"ownerEmail": "[email protected]"
}
],
"msg": ""
}
-
权限验证
// api白名单 public static String[] allowPath = new String[] {"user/login", "user/register"}; public static void tokenAuthMiddleware(Request req, Response res) { if (Arrays.asList(TokenAuth.allowPath).contains(req.splat()[0])) return; // 得到header或query上的email和token var email = getArg(req, "email"); var token = getArg(req, "token"); // 若header和query都不存在email和token,直接401 if (token == null || email == null) halt(401, "未经授权的请求"); var redis = utils.Redis.getJedis(); // 通过email拿到redis用户信息 var json = redis.get(email); redis.close(); if (json == null) halt(401, "未经授权的请求"); var user = (new Gson().fromJson(json, UserModel.class)); // 验证token是否合法 if (!user.getToken().equals(token)) halt(401, "未经授权的请求"); }