Search in sources :

Example 1 with FormToken

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.chao.cloud.common.exception.BusinessException) ValidationException(javax.validation.ValidationException) FormToken(com.chao.cloud.common.extra.token.annotation.FormToken) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Aggregations

BusinessException (com.chao.cloud.common.exception.BusinessException)1 FormToken (com.chao.cloud.common.extra.token.annotation.FormToken)1 Method (java.lang.reflect.Method)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ValidationException (javax.validation.ValidationException)1 Around (org.aspectj.lang.annotation.Around)1