summaryrefslogtreecommitdiff
path: root/spring-framework/24-aop-aspectj-annotations2/src/main/java/com/example/spring/aop/security/AccessChecker.java
blob: dff17bc084e9cdfd6079473e6f9ebfd4cdad333a (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.spring.aop.security;

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.");
//	    }
//    }
    
    
    @Before("MyPointcuts.abc()")
    public void checkDepositPermission() {
	    System.out.println("Aspect is checking deposit permission...");
    }
    
	@Before("MyPointcuts.checkWithAmount(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.");
		}
	}
	
	@Before("MyPointcuts.depositAndWithdraw()")
	public void depositAndWithdraw() {
		System.out.println("depositAndWithdraw()");
	}
}