定义注解
@Retention(RetentionPolicy.RUNTIME)//运行时有效
@Target(ElementType.METHOD)//作用于方法
public @interface PermissionLog {
// 方法名
String openId() default "";
String tournamentId() default "";
String applicationType() default "";
boolean isEntity() default true;
}
定义传实体需要的对象
@Data
@ApiModel(value = "权限管理VO")
public class AopPermissionLogVO<T> {
@ApiModelProperty(value = "数据实体")
private T entity;
@ApiModelProperty(value = "登录人openID")
private String openId;
@ApiModelProperty(value = "赛事ID")
private String tournamentId;
}
定义解析注解的切点
@Aspect
@Component
public class AnnotationAspect {
@Autowired
private ICompetitionClient competitionClient;
@Around(value = "@annotation(around)")
@ResponseBody
public Object processAuthority(ProceedingJoinPoint point, PermissionLog around) throws Throwable{
if (true){return point.proceed();}
int count =0;
if (around.isEntity()){
AopPermissionLogVO arg = (AopPermissionLogVO) point.getArgs()[0];
count = competitionClient.getPermissionLogCount(arg.getOpenId(),arg.getTournamentId(),around.applicationType());
}else {
MethodSignature signature = (MethodSignature) point.getSignature();
String fieldName;
Map<String,String> fieldValues = new HashMap(16);
String[] all = new String[]{around.openId(),around.tournamentId(),around.applicationType()};
for (String str:all){
if(!StringUtils.isBlank(fieldName = str)){
String[] parameterNames = signature.getParameterNames();
for (int i = 0; i < parameterNames.length; i++) {
String parameterName = parameterNames[i];
if (fieldName.equals(parameterName)) {
fieldValues.put(fieldName, String.valueOf(point.getArgs()[i]));
}
}
}
}
count = competitionClient.getPermissionLogCount(fieldValues.get(around.openId()),fieldValues.get(around.tournamentId()), around.applicationType());
}
if (count>0){
return point.proceed();
}else {
return R.fail("没有权限!");
}
}
}