Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengtianlai3
ioc_sixiang_license
Commits
0b24889f
Commit
0b24889f
authored
Jun 30, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2.3.9 异常处理:泛化的捕获异常
parent
fd93efa6
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
119 additions
and
165 deletions
+119
-165
DeviceDecoder.java
...c/main/java/iot/sixiang/license/device/DeviceDecoder.java
+21
-27
DeviceEncoder.java
...c/main/java/iot/sixiang/license/device/DeviceEncoder.java
+7
-12
ForwardDecoder.java
...main/java/iot/sixiang/license/forward/ForwardDecoder.java
+24
-30
ForwardEncoder.java
...main/java/iot/sixiang/license/forward/ForwardEncoder.java
+7
-12
JwtUtil.java
license/src/main/java/iot/sixiang/license/jwt/JwtUtil.java
+10
-16
LogAspect.java
license/src/main/java/iot/sixiang/license/log/LogAspect.java
+19
-23
OperateDecoder.java
...main/java/iot/sixiang/license/operate/OperateDecoder.java
+20
-27
OperateEncoder.java
...main/java/iot/sixiang/license/operate/OperateEncoder.java
+7
-13
CommonUtil.java
...se/src/main/java/iot/sixiang/license/util/CommonUtil.java
+4
-5
No files found.
license/src/main/java/iot/sixiang/license/device/DeviceDecoder.java
View file @
0b24889f
...
...
@@ -22,38 +22,32 @@ public class DeviceDecoder extends ByteToMessageDecoder {
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
try
{
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
short
stx
=
buffer
.
readShort
();
short
len
=
buffer
.
readShortLE
();
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
int
real_len
=
len
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
short
stx
=
buffer
.
readShort
();
short
len
=
buffer
.
readShortLE
();
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
int
real_len
=
len
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
// buffer.resetReaderIndex();
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
byte
end
=
buffer
.
readByte
();
byte
end
=
buffer
.
readByte
();
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"DeviceDecoder error!"
);
}
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
}
}
\ No newline at end of file
license/src/main/java/iot/sixiang/license/device/DeviceEncoder.java
View file @
0b24889f
...
...
@@ -14,21 +14,16 @@ public class DeviceEncoder extends MessageToByteEncoder<DeviceProtocol> {
@Override
protected
void
encode
(
ChannelHandlerContext
tcx
,
DeviceProtocol
msg
,
ByteBuf
out
)
{
try
{
out
.
writeShort
(
msg
.
getStx
());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeShort
(
msg
.
getStx
());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
if
(
msg
.
getContent
()
!=
null
)
{
out
.
writeBytes
(
msg
.
getContent
());
}
out
.
writeByte
(
msg
.
getEnd
());
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"解码器异常"
);
if
(
msg
.
getContent
()
!=
null
)
{
out
.
writeBytes
(
msg
.
getContent
());
}
out
.
writeByte
(
msg
.
getEnd
());
}
}
license/src/main/java/iot/sixiang/license/forward/ForwardDecoder.java
View file @
0b24889f
...
...
@@ -17,49 +17,43 @@ public class ForwardDecoder extends ByteToMessageDecoder {
@Override
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
// 可读长度必须大于等于基本长度
try
{
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
short
stx
=
buffer
.
readShort
();
//55AA->21930
short
len
=
buffer
.
readShortLE
();
short
stx
=
buffer
.
readShort
();
//55AA->21930
short
len
=
buffer
.
readShortLE
();
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
////stx:21930,len:52,cmd:-112,ack:0
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
////stx:21930,len:52,cmd:-112,ack:0
int
real_len
=
len
;
//注意,透传前已经去掉了END一个字符
int
cmd_ack_len
=
2
;
int
real_len
=
len
;
//注意,透传前已经去掉了END一个字符
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
// buffer.resetReaderIndex();//复位
// 读取data数据
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
// 读取data数据
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
byte
end
=
buffer
.
readByte
();
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
byte
end
=
buffer
.
readByte
();
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"Decoder error"
);
}
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
}
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardEncoder.java
View file @
0b24889f
...
...
@@ -11,20 +11,15 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
@Override
protected
void
encode
(
ChannelHandlerContext
tcx
,
DeviceProtocol
msg
,
ByteBuf
out
)
{
try
{
out
.
writeShort
(
msg
.
getStx
());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
out
.
writeShort
(
msg
.
getStx
());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
if
(
msg
.
getContent
()
!=
null
)
{
out
.
writeBytes
(
msg
.
getContent
());
}
out
.
writeByte
(
msg
.
getEnd
());
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"解码器异常"
);
if
(
msg
.
getContent
()
!=
null
)
{
out
.
writeBytes
(
msg
.
getContent
());
}
out
.
writeByte
(
msg
.
getEnd
());
}
}
license/src/main/java/iot/sixiang/license/jwt/JwtUtil.java
View file @
0b24889f
...
...
@@ -22,22 +22,23 @@ public class JwtUtil {
**/
private
static
final
long
EXPIRATION
=
3600L
;
//单位为秒
private
static
HashMap
<
String
,
String
>
tokens
=
new
HashMap
<>();
private
static
HashMap
<
String
,
String
>
tokens
=
new
HashMap
<>();
/**
* 生成用户token,设置token超时时间
*/
public
static
String
createToken
(
LoginUser
user
)
{
public
static
String
createToken
(
LoginUser
user
)
{
//过期时间
Date
expireDate
=
new
Date
(
System
.
currentTimeMillis
()
+
EXPIRATION
*
1000
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"alg"
,
"HS256"
);
map
.
put
(
"typ"
,
"JWT"
);
String
token
=
JWT
.
create
()
String
token
=
JWT
.
create
()
.
withHeader
(
map
)
//添加头部
//可以把数据存在claim中
.
withClaim
(
"userId"
,
user
.
getUserId
())
.
withClaim
(
"userName"
,
user
.
getUserName
())
.
withClaim
(
"password"
,
user
.
getPassword
())
.
withClaim
(
"userId"
,
user
.
getUserId
())
.
withClaim
(
"userName"
,
user
.
getUserName
())
.
withClaim
(
"password"
,
user
.
getPassword
())
.
withExpiresAt
(
expireDate
)
//超时设置,设置过期的日期
.
withIssuedAt
(
new
Date
())
//签发时间
.
sign
(
Algorithm
.
HMAC256
(
SECRET
));
//SECRET加密
...
...
@@ -63,16 +64,9 @@ public class JwtUtil {
/**
* 检验token并解析token
*/
public
static
DecodedJWT
verifyToken
(
String
token
){
DecodedJWT
jwt
=
null
;
try
{
JWTVerifier
verifier
=
JWT
.
require
(
Algorithm
.
HMAC256
(
SECRET
)).
build
();
jwt
=
verifier
.
verify
(
token
);
}
catch
(
Exception
e
){
log
.
error
(
e
.
getMessage
());
log
.
error
(
"解析编码异常"
);
}
public
static
DecodedJWT
verifyToken
(
String
token
)
{
JWTVerifier
verifier
=
JWT
.
require
(
Algorithm
.
HMAC256
(
SECRET
)).
build
();
DecodedJWT
jwt
=
verifier
.
verify
(
token
);
return
jwt
;
}
}
license/src/main/java/iot/sixiang/license/log/LogAspect.java
View file @
0b24889f
...
...
@@ -57,32 +57,28 @@ public class LogAspect {
}
protected
void
handleLog
(
final
JoinPoint
joinPoint
,
final
Exception
e
,
Object
jsonResult
)
{
try
{
// 获得注解
MyLog
controllerLog
=
getAnnotationLog
(
joinPoint
);
if
(
controllerLog
==
null
)
{
return
;
}
SysOperLog
operLog
=
new
SysOperLog
();
operLog
.
setStatus
(
0
);
operLog
.
setOperTime
(
new
Date
());
if
(
e
!=
null
)
{
operLog
.
setStatus
(
1
);
operLog
.
setErrorMsg
(
StringUtils
.
substring
(((
IotLicenseException
)
e
).
getMsg
(),
0
,
2000
));
}
// 获得注解
MyLog
controllerLog
=
getAnnotationLog
(
joinPoint
);
if
(
controllerLog
==
null
)
{
return
;
}
String
uri
=
UserUtils
.
getUri
();
operLog
.
setUri
(
uri
);
// operLog.setMethod(className + "." + methodName + "()");
// 处理设置注解上的参数
getControllerMethodDescription
(
joinPoint
,
controllerLog
,
operLog
);
// 保存数据库
sysOperLogService
.
addOperlog
(
operLog
.
getTitle
(),
operLog
.
getBusinessType
(),
operLog
.
getUri
(),
operLog
.
getStatus
(),
operLog
.
getOptParam
(),
operLog
.
getErrorMsg
(),
operLog
.
getOperTime
());
}
catch
(
Exception
exp
)
{
log
.
error
(
"==前置通知异常=="
);
log
.
error
(
"日志异常信息 {}"
,
exp
);
SysOperLog
operLog
=
new
SysOperLog
();
operLog
.
setStatus
(
0
);
operLog
.
setOperTime
(
new
Date
());
if
(
e
!=
null
)
{
operLog
.
setStatus
(
1
);
operLog
.
setErrorMsg
(
StringUtils
.
substring
(((
IotLicenseException
)
e
).
getMsg
(),
0
,
2000
));
}
String
uri
=
UserUtils
.
getUri
();
operLog
.
setUri
(
uri
);
// operLog.setMethod(className + "." + methodName + "()");
// 处理设置注解上的参数
getControllerMethodDescription
(
joinPoint
,
controllerLog
,
operLog
);
// 保存数据库
sysOperLogService
.
addOperlog
(
operLog
.
getTitle
(),
operLog
.
getBusinessType
(),
operLog
.
getUri
(),
operLog
.
getStatus
(),
operLog
.
getOptParam
(),
operLog
.
getErrorMsg
(),
operLog
.
getOperTime
());
}
/**
...
...
license/src/main/java/iot/sixiang/license/operate/OperateDecoder.java
View file @
0b24889f
...
...
@@ -16,44 +16,37 @@ public class OperateDecoder extends ByteToMessageDecoder {
@Override
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
// 可读长度必须大于等于基本长度
try
{
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
short
stx
=
buffer
.
readShort
();
short
len
=
buffer
.
readShortLE
();
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
short
stx
=
buffer
.
readShort
();
short
len
=
buffer
.
readShortLE
();
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
int
real_len
=
len
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
int
real_len
=
len
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
// 读取data数据
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
// 读取data数据
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
byte
end
=
buffer
.
readByte
();
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
byte
end
=
buffer
.
readByte
();
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"Decoder error"
);
}
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
}
...
...
license/src/main/java/iot/sixiang/license/operate/OperateEncoder.java
View file @
0b24889f
...
...
@@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.MessageToByteEncoder
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.util.Util
;
import
lombok.extern.slf4j.Slf4j
;
@Slf4j
...
...
@@ -12,20 +11,15 @@ public class OperateEncoder extends MessageToByteEncoder<DeviceProtocol> {
@Override
protected
void
encode
(
ChannelHandlerContext
tcx
,
DeviceProtocol
msg
,
ByteBuf
out
)
{
try
{
out
.
writeShort
(
msg
.
getStx
());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
out
.
writeShort
(
msg
.
getStx
());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
if
(
msg
.
getContent
()
!=
null
)
{
out
.
writeBytes
(
msg
.
getContent
());
}
out
.
writeByte
(
msg
.
getEnd
());
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"解码器异常"
);
if
(
msg
.
getContent
()
!=
null
)
{
out
.
writeBytes
(
msg
.
getContent
());
}
out
.
writeByte
(
msg
.
getEnd
());
}
}
license/src/main/java/iot/sixiang/license/util/CommonUtil.java
View file @
0b24889f
...
...
@@ -2,15 +2,14 @@ package iot.sixiang.license.util;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.
*
;
import
java.io.
UnsupportedEncodingException
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.SecureRandom
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.Date
;
import
java.util.Locale
;
@Slf4j
public
class
CommonUtil
{
public
class
CommonUtil
{
/**
* 随机生成指定长度的字符串
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment