实现切面拦截

切面拦截

控制层使用

@RequiredLicense(value = "license")

异常类

public class BizException extends RuntimeException {
    protected int code;

    public BizException() {
    }

    public BizException(String msg) {
        super(msg);
    }

    public BizException(int code, String msg) {
        super(msg);
        this.code = code;
    }

    public BizException(Throwable e) {
        super(e);
    }

    public BizException(int code, String msg, Throwable e) {
        super(msg, e);
        this.code = code;
    }

    public BizException(IReturnCode returnCode) {
        super(returnCode.getMsg());
        this.code = returnCode.getCode();
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.getMessage();
    }
}

切面

@Aspect
@Component
public class RequestAspect {

    //使用org.slf4j.Logger,这是spring实现日志的方法
    private final static Logger logger = LoggerFactory.getLogger(RequestAspect.class);

    @Autowired
    private LicenseToolsRedis licenseToolsRedis;

    @Resource
    public StringRedisTemplate stringRedisTemplate;

    /**
     * 表示在执行被@RequiredLicense注解修饰的方法之前会执行licenseVerity方法
     * @param proceedingJoinPoint 被拦截的点
     */
    @Around(value = "@annotation(com.vcloud.license.RequiredLicense) && @annotation(requiredLicense)")
    public Object licenseVerity(ProceedingJoinPoint proceedingJoinPoint, RequiredLicense requiredLicense) throws Exception {
        logger.info("进入拦截切面");
        try {
            //获取注解参数
            String annotationParam = requiredLicense.value();
            logger.info("进入license拦截切面"+annotationParam);
            if (annotationParam.equals("license")) {
                logger.info("拦截license成功");
                try {
//                    LicenseRedis licenseRedis = new LicenseRedis();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Map<String,String> licenseInfo = licenseToolsRedis.licenseInfoGet();
                    int status = Integer.valueOf(licenseInfo.get("status")).intValue();
                    String licenseTime = licenseInfo.get("licenseTime");
                    Date redisLicenseTime =format.parse(licenseTime);
                    //判断license激活状态 0表示未激活
                    if (status == 0){
                        logger.info("拦截license  未激活");
                        //获取用户创建时间
                        String ctime = licenseInfo.get("ctime");
                        logger.info("初始化的日期是:" + ctime);
                        Calendar ca = Calendar.getInstance();
                        ca.setTime(format.parse(ctime));
                        // 30为增加的天数,可以改变的
                        ca.add(Calendar.DATE, 30);
//                        Date endDate = ca.getTime();
                        //enddate 是license结束时间。
                        String endDate = format.format(ca.getTime());
                        logger.info("增加天数以后的日期:" + endDate);
                        int i = endDate.compareTo(licenseTime);
                        //redisLicenseTime = format.parse(licenseTime);
                        if (i >= 0){
                            Date endTime = format.parse(endDate);
                            Long day = (endTime.getTime() - redisLicenseTime.getTime())/(24*60*60*1000);
                            if (day > 0 && day <= 30){
                                return proceedingJoinPoint.proceed();
                            }
                        }else {
                            throw new BizException(31011,"授权已过期,请联系管理员");
                        }
                    }else if (status == 1){
                        logger.info("拦截license  已激活");
                        Date time=null;
                        Boolean bool = stringRedisTemplate.opsForHash().hasKey("redis_system_time","system_time");
                        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        if(bool){
                            String systemTime = (String) stringRedisTemplate.opsForHash().get("redis_system_time","system_time");
                            time=sdf.parse(systemTime);
                        }else{
                            time=new Date();
                        }
                        Date day=time.before(new Date())? time:new Date();
                        if(redisLicenseTime.before(day)){
                            throw new BizException(31011,"授权已过期,请联系管理员");
                        }
                        return proceedingJoinPoint.proceed();
                    }
                }catch (Exception e){
                    logger.error("license验证出现异常 {}",e);
                    e.printStackTrace();
                    e.printStackTrace();
                }
            } else {
                logger.info("license未拦截");
            }
        } catch (Throwable e) {
            logger.error("拦截请求的时候抛出异常 {}", e);
            e.printStackTrace();
        }
        throw new BizException(31011,"授权已过期,请联系管理员");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值