use of com.chao.cloud.common.extra.token.annotation.FormToken in project chao-cloud by chaojunzi.
the class SessionFormTokenProxy method around.
// 环绕拦截
@Around(value = "@annotation(com.chao.cloud.common.extra.token.annotation.FormToken)")
public Object around(ProceedingJoinPoint pdj) throws Exception {
Object obj = null;
HttpServletRequest request = getRequest();
log.info("[token]----start----------");
Method method = getMethod(pdj);
FormToken annotation = method.getAnnotation(FormToken.class);
String token = null;
if (annotation != null) {
boolean needSaveSession = annotation.save();
// 添加token
if (needSaveSession) {
request.getSession(true).setAttribute(FROM_TOKEN_KEY, IdUtil.fastUUID());
}
boolean needRemoveSession = annotation.remove();
// 删除token
if (needRemoveSession) {
boolean isRepeat = isRepeatSubmit(request);
// 获取token
token = (String) request.getSession(true).getAttribute(FROM_TOKEN_KEY);
request.getSession(true).removeAttribute(FROM_TOKEN_KEY);
if (isRepeat) {
log.warn("请不要重复提交,url:{}", request.getServletPath());
throw new BusinessException("请不要重复提交");
}
}
}
// 执行
try {
obj = pdj.proceed();
} catch (Throwable e) {
if (e instanceof ValidationException && StrUtil.isNotBlank(token) && annotation.remove()) {
// 参数校验回退
request.getSession(true).setAttribute(FROM_TOKEN_KEY, token);
}
throw new BusinessException(ExceptionUtil.getMessage(e));
}
log.info("[token]------end----------");
return obj;
}
Aggregations