通过Redis和AOP实现防重提交

下面的代码是使用shiro做安全控制的后台管理系统的防重提交。
可根据实际情况进行修改

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
 * @类名称 ResubmitAop
 * @类描述 定义切面,进行重复提交拦截
 */
@Aspect
@Component
public class ResubmitAop {

    private static final Logger logger = LoggerFactory.getLogger(ResubmitAop.class);

    @Around("execution(* com.controller..*(..)) && @annotation(resubmit)")
    public Object doInterceptor(ProceedingJoinPoint pjp, Resubmit resubmit) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        String token = principals.toString();// 访问令牌
        String path = request.getServletPath();// 访问路径
        String key = PREFIX + path + ":" + token;
        if (CacheUtils.exists(key)) {// 如果缓存中有这个url视为重复提交
            logger.info("重复提交拦截,account:{},path:{}", JwtUtil.getAccount(token), path);
            throw new Exception("");
        }
        CacheUtils.set(key, value, EXPIRE_TIME);
        return pjp.proceed();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值