AOP切面示例
import java.lang.annotation.*;
/**
* @author: 木杉
* @email: 819236727@qq.com
* @Date: 2019/10/17 20:54
* @describe:
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UserAuthority {
/**
* 多个 私有权限码/公共权限码 都可以
*/
String[] code() default "";
/**
* 0:只要有一个权限通过则通过
* 1:要全部通过才能通过
*/
int type() default 0;
}
/**
* @author: 木杉
* @email: 819236727@qq.com
* @Date: 2019/10/17 20:56
* @describe:
*/
@Aspect
@Slf4j
public class UserAuthorityAspect {
@Around("@annotation(userAuthority)")
public Object around(ProceedingJoinPoint point, UserAuthority userAuthority) throws Throwable {
//具体的业务逻辑 如果都没有错误记得return point.proceeed(); 不要return 其他的 否则具体的方法会获取不到参数
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
List<String> codes = Arrays.asList(userAuthority.code());
List<Boolean> flags = SessionUtil.privateAuthority(codes, request);
int type = userAuthority.type();
if (type == 0) {
if (flags.contains(true)) {
Object obj = point.proceed();
return obj;
}
} else if (type == 1) {
if (!flags.contains(false)) {
Object obj = point.proceed();
return obj;
}
} else {
throw new GraphqlParameterWrongException("错误的类型");
}
throw new GraphqlNotPermissionException("您没有操作权限!", codes.get(0));
}
}
正文到此结束