切面拦截
控制层使用
@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,"授权已过期,请联系管理员");
}
}
版权声明:本文为qq_40193942原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。