发表于: 2021-03-22 23:04:06
1 1214
今天完成的事情:
手动打日志记录耗时
从head取tokene验证
服务器被攻击,重装软件
明天计划的事情:
重装软件
tomcat调优
遇到的问题:
private final static Logger LOGGER = LoggerFactory.getLogger(TimeAspectJ.class);
@Pointcut("execution(* com.kbk.controller.*Controller.*(..))")
private void requestServer() {};
@Around("requestServer()")
private Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long start = System.currentTimeMillis();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
Object result = proceedingJoinPoint.proceed();
RequestInfo requestInfo = new RequestInfo();
requestInfo.setIp(request.getRemoteAddr());
requestInfo.setUrl(request.getRequestURL().toString());
requestInfo.setHttpMethod(request.getMethod());
requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
proceedingJoinPoint.getSignature().getName()));
requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint));
requestInfo.setResult(result);
requestInfo.setTimeCost(System.currentTimeMillis() - start);
LOGGER.info("Request Info : {}", JSON.toJSONString(requestInfo));
return result;
}
@AfterThrowing(pointcut = "requestServer()", throwing = "e")
private void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
RequestErrorInfo requestErrorInfo = new RequestErrorInfo();
requestErrorInfo.setIp(request.getRemoteAddr());
requestErrorInfo.setUrl(request.getRequestURL().toString());
requestErrorInfo.setHttpMethod(request.getMethod());
requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName()));
requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));
requestErrorInfo.setException(e);
LOGGER.info("Error Request Info : {}", JSON.toJSONString(requestErrorInfo));
}
/**
* 获取入参
* @param proceedingJoinPoint
*
* @return
* */
private Map<String, Object> getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
//参数名
String[] paramNames = ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();
//参数值
Object[] paramValues = proceedingJoinPoint.getArgs();
return buildRequestParam(paramNames, paramValues);
}
private Map<String, Object> getRequestParamsByJoinPoint(JoinPoint joinPoint) {
//参数名
String[] paramNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
//参数值
Object[] paramValues = joinPoint.getArgs();
return buildRequestParam(paramNames, paramValues);
}
private Map<String, Object> buildRequestParam(String[] paramNames, Object[] paramValues) {
Map<String, Object> requestParams = new HashMap<>();
for (int i = 0; i < paramNames.length; i++) {
Object value = paramValues[i];
//如果是文件对象
if (value instanceof MultipartFile) {
MultipartFile file = (MultipartFile) value;
value = file.getOriginalFilename(); //获取文件名
}
requestParams.put(paramNames[i], value);
}
return requestParams;
}
@Data
public class RequestInfo {
private String ip;
private String url;
private String httpMethod;
private String classMethod;
private Object requestParams;
private Object result;
private Long timeCost;
}
@Data
public class RequestErrorInfo {
private String ip;
private String url;
private String httpMethod;
private String classMethod;
private Object requestParams;
private RuntimeException exception;
}
使用这个打日志,出现java.lang.IllegalStateException: 如果当前请求不在异步模式下,则调用此方法是非法的(即isAsyncStarted()返回false)
这个报错,没解决,不懂异步模式是什么意思...
收获:
手动打日志记录耗时:
@RequestMapping(value = "/excellentStudent", method = RequestMethod.GET)
public String excellentStudent(Map<String,Object> map){
long startTimeMillis = System.currentTimeMillis();
List<ExcellentStudent> studentList = excellentStudentService.selectAll();
long execTimeMillis = System.currentTimeMillis() - startTimeMillis;
logger.info("方法名称 : StudentController-excellentStudent---> 方法用时 : " + execTimeMillis+"ms");
map.put("Exs",studentList);
return "excellentStudent";
}
@RequestMapping(value = "/excellentStudentJson", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> excellentStudentJson(){
long startTimeMillis = System.currentTimeMillis();
List<ExcellentStudent> studentList = excellentStudentService.selectAll();
long execTimeMillis = System.currentTimeMillis() - startTimeMillis;
logger.info("方法名称 : StudentController-excellentStudentJson---> 方法用时 : " + execTimeMillis+"ms");
if (null == studentList) {
return Restful.set(400, "查找优秀学员失败" );
} else {
return Restful.set(200, "查找优秀学员成功", studentList);
}
}
@RequestMapping(value = "/u/profession", method = RequestMethod.GET)
public String profession(Map<String,Object> map, HttpServletRequest request) throws Exception {
// String ip = WebUtil.getIpAddress(request);
long startTimeMillis = System.currentTimeMillis();
List<Profession> professionList = professionService.selectAll();
map.put("pro",professionList);
long execTimeMillis = System.currentTimeMillis() - startTimeMillis;
logger.info("方法名称 : StudentController-profession---> 方法用时 : " + execTimeMillis+"ms");
return "profession";
}
@RequestMapping(value = "/u/professionJson", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> professionJson() throws Exception {
long startTimeMillis = System.currentTimeMillis();
List<Profession> professionList = professionService.selectAll();
long execTimeMillis = System.currentTimeMillis() - startTimeMillis;
logger.info("方法名称 : StudentController-professionJson---> 方法用时 : " + execTimeMillis+"ms");
if (null == professionList) {
return Restful.set(400, "查找职业失败" );
} else {
return Restful.set(200, "查找职业成功", professionList);
}
}
运行:
========
========
从head取tokene验证:
//从请求中获取cookie, 判断当前用户是否已经登陆
Cookie cookie = CookieUtil.getCookie(request.getCookies(), "token");
//判断客户端是否有cookie
if (cookie != null) {
//从cookie得到token
String value = URLDecoder.decode(cookie.getValue(), "utf-8");
logger.info("从客户端得到的taken:" + value);
//解密token
Claims token = JWTUtil.parseJWT(value, SECRE);
if(token!=null){
return true;
}else {
return false;
}
} else {
String headertoken = request.getHeader("token");
logger.info("从客户端得到的taken:" + headertoken);
//解密token
Claims token = JWTUtil.parseJWT(headertoken, SECRE);
if (token != null){
return true;
}else {
logger.info("用户没有登录");
//如果没有检测到登录状态就重定向到登录界面
response.sendRedirect(request.getContextPath() + "/login");
return false;
}
}
第一步:
第二步:
第三步:
运行,token成功传入:
服务器被攻击,重装软件:
安装的时候,记得按照这些配置:
参考网址:https://help.aliyun.com/knowledge_list/60791.html?spm=a2c4g.11186623.6.556.6bc280544gic7J
安装jdk
评论