切面拦截
控制层使用
@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 {
private final static Logger logger = LoggerFactory . getLogger ( RequestAspect . class ) ;
@Autowired
private LicenseToolsRedis licenseToolsRedis;
@Resource
public StringRedisTemplate stringRedisTemplate;
@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 {
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) ;
if ( status == 0 ) {
logger. info ( "拦截license 未激活" ) ;
String ctime = licenseInfo. get ( "ctime" ) ;
logger. info ( "初始化的日期是:" + ctime) ;
Calendar ca = Calendar . getInstance ( ) ;
ca. setTime ( format. parse ( ctime) ) ;
ca. add ( Calendar . DATE, 30 ) ;
String endDate = format. format ( ca. getTime ( ) ) ;
logger. info ( "增加天数以后的日期:" + endDate) ;
int i = endDate. compareTo ( 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 , "授权已过期,请联系管理员" ) ;
}
}