Want to make a logical judgment before some methods are executed,
Originally I wanted to use an interceptor to intercept, but then I said that I had no idea.
I thought that using AOP's pre-enhancement and custom exception and custom exception capture can solve this problem,
I have used so much at one time, I just want to pick up the fuzzy things before
1. We first customize a note
1 /** 2 * @program: shiro-demo 3 * @description: 自定义注解 4 * @author: @DogElder 5 * @create: 2020-04-18 20:42 6 **/ 7 8 @Target(ElementType.METHOD) 9 @Retention(RetentionPolicy.RUNTIME) 10 public @interface TestAnnotation { 11 }
2. Customize an exception class
We must first import AOP dependencies
1 <!--aop--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-aop</artifactId> 5 </dependency>
. 1 Import lombok.Data; 2 . 3 / ** . 4 * @program: Shiro-Demo . 5 * @description: custom exception class . 6 * @author : @DogElder . 7 * @Create: 2020-04-18 21:11 . 8 * To inherit RuntimeException 9 * * / 10 @Data 11 public class TestExcption extends RuntimeException { 12 13 private String code; 14 private String message; 15 16 // This place needs to write a parameterized structure 17 public TestExcption(String code, String message) { 18 this.code = code; 19 this.message = message; 20 } 21 }
Exception catch
1 /** 2 * @program: shiro-demo 3 * @description: 异常处理 4 * @author: @DogElder 5 * @create: 2020-04-18 21:20 6 **/ 7 @RestControllerAdvice 8 public class TestExceptionHandler { 9 10 11 /** 12 * @Description: 异常捕获 13 * @Param: java.util.Map<java.lang.String,java.lang.Object> 14 * @return: java.util.Map<java.lang.String,java.lang.Object> 15 * @Author:@Dog_Elder 16 * @Date: 2020/4/18 17 */ 18 @ExceptionHandler(TestExcption.class) 19 public Map<String, Object> handleCustomException(TestExcption customException) { 20 Map<String, Object> error = new HashMap<>(); 21 error.put("code", customException.getCode()); 22 error.put("message", customException.getMessage()); 23 return error; 24 } 25 } 26
3. Then we customize a facet class
1 / ** 2 * @program: shiro-demo 3 * @description: custom aspect 4 * @author : @DogElder 5 * @create: 2020-04-18 20:49 6 * * / 7 @Configuration 8 @Aspect . 9 public class TestAop { 10 . 11 12 is / ** 13 is * @Description: declare a pointcut 14 * @Param: void 15 * @return : void 16 * @author: @Dog_Elder . 17 * @date: 2020/4/18 18 * @Pointcut declares a pointcut parameter as a pointcut expression 19 * Note: The pointcut here is the full name of the @annotation annotation class (see the pointcut you declared is to be replaced there) 20 * / 21 @Pointcut ( "@annotation (com.shiro.shiro_demo.config.annotation.TestAnnotation)" ) 22 is public void testPointCut () { 23 is 24 } 25 26 is / ** 27 * @Description: declare a front reinforcement 28 * @Param: void 29 * @return : void 30 * @Author: @Dog_Elder 31 * @Date: 2020/4/18 32 * @Before The pre-enhanced parameter is the pointcut method 33 * Note: throw exception throw exception throws throws RuntimeException and throw 34 * 35 * / 36 @Before ("testPointCut ()" ) 37 public void testBefore () throws RuntimeException { 38 // You can implement your logic code here (because it is demo) 39 // I will write a simple example 40 int n = 1 ; 41 if (n! = 0 ) { 42 // This place is the custom exception we wrote 43 throw new TestExcption ("401", "No permission" ); 44 } 45 } 46 47 }
test
END