Java tutorial
/** * Copyright (c) 2015-2017, Chill Zhuang (smallchill@163.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.xl.core.aop; import java.lang.reflect.Method; 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.stereotype.Component; import cn.com.xl.core.annotation.Permission; import cn.com.xl.core.exception.NoPermissionException; import cn.com.xl.core.toolbox.Func; import cn.com.xl.core.toolbox.check.PermissionCheckManager; /** * AOP ?? */ @Aspect @Component public class PermissionAop { @Pointcut(value = "@annotation(cn.com.xl.core.annotation.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.length == 1 && Func.toStr(permissions[0]).equals("ALL")) || 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(); } } } }