summaryrefslogtreecommitdiff
path: root/spring-framework/23-aop-aspectj-annotations/src/main/java/com/example/spring/aop/security/AccessChecker.java
blob: cd721f7555e95392510f58f319df9d7c14c93f6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.spring.aop.security;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AccessChecker {

	/*
	 * In-place pointcut expressions are used here (i.e. the pointcut
	 * expression is just declared with the advice directly rather than
	 * elsewhere).
	 */
    @Before("execution(public * com.example.spring.aop.Account.deposit(..))")
    public void checkDepositPermission() {
	    System.out.println("Aspect is checking deposit permission...");
    }
   
    @After("execution(public * com.example.spring.aop.Account.deposit(..))")
    public void checkDepositPermissionAfterDemo() {
	    System.out.println("checkDepositPermissionAfterDemo()");
    }
	 
    @Before("execution(public * com.example.spring.aop.Account.withdraw(..)) && args(amount)")
    public void checkWithdrawPermission(double amount) {
	    System.out.println("Aspect is checking withdraw permission for " + amount);
	    if (amount > 1000.0) {
		    throw new AccessException("Withdrawing more than 1000.0 is not allowed.");
	    }
    }
}