Implementation of a simple permission control annotation based on SpringBoot

Annotations are an annotation mechanism introduced in JDK 5.0. Annotations can be effect in type (class, interface, enums, etc.), properties, methods, different parameters such as position, supported by a specific version of the JDK refer to Java annotations position. Lang. The annotation. ElementType. There is also an annotated policy, called RetentionPolicy, which I won’t go into here.

Annotations can do a lot of things, but one of the main things is to annotate code, so they are sometimes called annotations. Use also basically as the name implies, is to code annotation, simplify the logic of part of the code.

Let’s start by implementing a simple permission control annotation to get a basic understanding of annotations.

To prepare

@Permissionannotations

The code for the annotation itself is simple. The following implementation is an @Permission annotation. For ease of use, only one attribute of value is provided, because if an annotation has a property named value and you only want to set the value attribute (i.e. all other attributes are default or you only have one value attribute), Then you can omit the “value=” part.

import java.lang.annotation.*; @Target({elementTye.parameter}) // The annotation is used for @Retention(RetentionPolicy.Runtime) // The annotation is read by the JVM at RUNTIME into @Documented Public @interface Permission { String value() default ""; }

User

A simple User class that contains permissions to save the User’s permissions.

import lombok.Data;

@Data
public class User {
    private String id;
    private String name;

    private Set<String> permissions;
}

UserService

A simple Service class that determines permissions.

@service public class UserService {public Boolean checkCreatePermission(@Permission(" create User ") User User) {return true; } public Boolean checkDeletePermission(@Permission(" delete User ") User User) {return true; }}

PermissionAspect

Simply set up the facets with SpringBoot, get the annotations, and use them. Here directly

@Aspect @Component public class PermissionAspect {// Change the Package of the actual Service @Pointcut(" Execution (public *)" tk.yubarimelon.MongoDemo.service.*.*(..) )") public void permissionCheck() { } @Around("permissionCheck()") public Object before(ProceedingJoinPoint joinPoint) throws Throwable { Object[] params = joinPoint.getArgs(); GetSignature = (methodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); / / gain parameter Annotation, 1 d is a parameter, 2 d is annotated Annotation [] [] parameterAnnotations = method. The getParameterAnnotations (); for (int i = 0; i < parameterAnnotations.length; i++) { Object param = params[i]; Annotation[] annotations = parameterAnnotations[i]; if (! (param instanceof User) || annotations.length == 0) { continue; } for (Annotation annotation : annotations) { if (annotation.annotationType().equals(Permission.class)) { Permission permission = (Permission) annotation; User user = (User) param; If (CollectionUtils.isEmpty(User.getPermissions ())) {log.error(User.getName () + "No permissions!" ); return false; } if (! StringUtils.hasLength((Permission. Value ()))) {log.error(JoinPoint.getSignature ().toString() + "Permission setting exception "); return false; } if (! user.getPermissions().contains(permission.value())) { log.error(joinPoint.getSignature().toString() +": "+ User.getName () + "No permission: "+ Permission. Value ())); return false; } return joinPoint.proceed(); } } } return joinPoint.proceed(); }}

ApplicationTests

Simple test class for testing code. The simple configuration here is that a user only has permission to create a user

@SpringBootTest class ApplicationTests { @Autowired UserService userService; @Test void contextLoads() { } @Test void checkUser() { User user = new User(); User. Elegantly-named setName (" Ming "); Set<String> permissions = new HashSet<>(); Permissions. Add (" create user "); user.setPermissions(permissions); System.out.println("checkCreatePermission " + userService.checkCreatePermission(user)); System.out.println("checkDeletePermission " + userService.checkDeletePermission(user)); }}

The following log is printed to prove that the permission setting worked.

CheckCreatePermission true 11:44:45 2021-01-31. 895 ERROR 12388 - [the main] T.Y.M ongoDemo. Aop. PermissionAspect: Boolean tk. Yubarimelon. MongoDemo. Service. UserService. CheckDeletePermission (User) : xiao Ming without permission: Delete user checkDeletePermission False