writer.close();
return;
} catch (SignatureException e) {
log.warn("解析JWT时出现异常:SignatureException");
String message = "非法访问,你的本次操作已经被记录!";
JsonResult jsonResult = JsonResult.fail(ServiceCode.ERR_JWT_SIGNATURE,
message);
PrintWriter writer = response.getWriter();
writer.println(JSON.toJSONString(jsonResult));
writer.close();
return;
} catch (MalformedJwtException e) {
log.warn("解析JWT时出现异常:MalformedJwtException");
String message = "非法访问,你的本次操作已经被记录!";
JsonResult jsonResult = JsonResult.fail(ServiceCode.ERR_JWT_MALFORMED,
message);
PrintWriter writer = response.getWriter();
writer.println(JSON.toJSONString(jsonResult));
writer.close();
return;
} catch (Throwable e) {
log.warn("解析JWT时出现异常:", e);
String message = "服务器忙,请稍后再试!【同学们,看到这句时,你应该检查服务器端的
控制台,并在JwtAuthorizationFilter中解析JWT时添加处理异常的catch代码块】";
JsonResult jsonResult = JsonResult.fail(ServiceCode.ERROR_UNKNOWN,
message);
PrintWriter writer = response.getWriter();
writer.println(JSON.toJSONString(jsonResult));
writer.close();
return;
}
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 从解析结果中获取用户的信息
Long userId = claims.get(CLAIM_USER_ID, Long.class);
String username = claims.get(CLAIM_USER_NAME, String.class);
String userAgent = claims.get(CLAIM_USER_AGENT, String.class);
String remoteAddr = claims.get(CLAIM_REMOTE_ADDR, String.class);
log.debug("JWT中的用户id = {}", userId);
log.debug("JWT中的用户名 = {}", username);
log.debug("JWT中的浏览器信息 = {}", userAgent);
log.debug("JWT中的IP地址 = {}", remoteAddr);
1
2
3
4
5
6
7
8
9