package com.sinata.shop.core.aop;
|
|
import com.sinata.shop.core.common.annotion.Permission;
|
import com.sinata.shop.core.shiro.check.PermissionCheckManager;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.stereotype.Component;
|
|
import javax.naming.NoPermissionException;
|
import java.lang.reflect.Method;
|
|
/**
|
* AOP 权限自定义检查
|
*/
|
@Aspect
|
@Component
|
@Order(200)
|
public class PermissionAop {
|
|
@Pointcut(value = "@annotation(com.sinata.shop.core.common.annotion.Permission)")
|
private void cutPermission() {
|
|
}
|
|
@Around("cutPermission()")
|
public Object doPermission(ProceedingJoinPoint point) throws Throwable {
|
MethodSignature ms = (MethodSignature) point.getSignature();
|
Method method = ms.getMethod();
|
Permission permission = method.getAnnotation(Permission.class);
|
Object[] permissions = permission.value();
|
if (permissions == null || permissions.length == 0) {
|
//检查全体角色
|
boolean result = PermissionCheckManager.checkAll();
|
if (result) {
|
return point.proceed();
|
} else {
|
throw new NoPermissionException();
|
}
|
} else {
|
//检查指定角色
|
boolean result = PermissionCheckManager.check(permissions);
|
if (result) {
|
return point.proceed();
|
} else {
|
throw new NoPermissionException();
|
}
|
}
|
|
}
|
|
}
|