Custom annotation
PermissionCheck:
com.mgdd.sys.annotation Package; Import Classes in java.lang.annotation. * ; / * * * @author the LWW * @site www.lww.com * @company * @Create 2019-12-16 14:08 * / / / annotation location of this class which can be annotated @Target ({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) // for labeling the retention period annotation annotation @Retention (RetentionPolicy.RUNTIME) // whether to generate document annotations @Documented public @interface the PermissionCheck { // custom roles value, if a plurality of characters, separated by commas. public String Role () default "" ; }
aop class section, cut to a custom annotations on the PermissionCheck, when the method will jump to a comment in the logic processing
PermissionCheckAspect:
package com.mgdd.sys.aspect; import com.mgdd.sys.annotation.PermissionCheck; import com.mgdd.sys.service.PermissionService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; 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 javax.annotation.Resource; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; / * * * @Author LWW * @site www.lww.com * @company * @Create 2019-12-16 14:20 * / the @Aspect @Component @ SLF4J public class PermissionCheckAspect { @Resource Private PermissionService permissionService; // cut point expression determines the cut or be cut for all classes and methods in a path by way of the annotated method, the method must be void return type @Pointcut (value = " @annotation (com.mgdd.sys.annotation.PermissionCheck) " ) Private void permissionCheckCut () {}; // defines the processing logic section. I.e., on the plus @PermissionCheck method @Around ( " permissionCheckCut () ") public Object around(ProceedingJoinPoint pjp) throws Throwable{ log.info("====================进入AOP============================"); //1.记录日志信息 Signature signature = pjp.getSignature(); String className = pjp.getTarget().getClass().getSimpleName(); String methodName = signature.getName(); log.info("className:{},methodName:{}",className,methodName); //2.角色权限校验 MethodSignature methodSignature = (MethodSignature)signature; The targetMethod Method, =methodSignature.getMethod (); IF (targetMethod.isAnnotationPresent (the PermissionCheck. Class )) { // Get the method indicated in the annotation permissions the PermissionCheck = targetMethod.getAnnotation permission (the PermissionCheck. Class ); String Role = permission.role () ; log.info ( " current interface request user roles role: {} " , role); IF (StringUtils.isNotEmpty (role)) { String [] roles = role.split ( " , " ); // interface allows the Roles List <String> list = Arrays.asList (the Roles); // based on the id query from the database administrator privileges (available foreground pass over the id as a parameter) List <String> listPermission = permissionService.queryPermission ( 1 ); // Print administrator rights log.info ( " administrators have the authority: " + String.valueOf (listPermission)); // will be indicated on the notes permissions check out privileges than for (String L: List) { IF ( ! listPermission.contains (L)) { log.error ( " no authority " ); return null; } } Log.info ( " the AOP permission verification by character, into the service layer processing! " ); // 3. execute business logic, release return pjp.proceed (); } } return " O (∩ _ ∩) O ha ~ " ; } }
Use in the method of the above plus comment on the line
/ * * * Paging query * * @param request parameter set the params * @return result set an encapsulated object * / @GetMapping ( " queryPager " ) @PermissionCheck (Role = " SYS: User: View " ) public PageUtils queryPager (@RequestParam the Map <String, Object> the params ) { Query Query = new new Query ( the params ); List <the Permission> List = permissionService.queryPager (Query); return new new PageUtils (List, query.getTotal ()); }
effect: