Skip to content

Commit e3e89bc

Browse files
added AOP example
1 parent 5a331b4 commit e3e89bc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package guru.springframework.services.security;
2+
3+
import org.aspectj.lang.annotation.*;
4+
import org.springframework.security.core.Authentication;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* Created by jt on 1/6/16.
9+
*/
10+
@Aspect
11+
@Component
12+
public class LoginAspect {
13+
14+
@Pointcut("execution(* org.springframework.security.authentication.AuthenticationProvider.authenticate(..))")
15+
public void doAuthenticate(){
16+
17+
}
18+
19+
@Before("guru.springframework.services.security.LoginAspect.doAuthenticate() && args(authentication)")
20+
public void logBefore(Authentication authentication){
21+
22+
System.out.println("This is before the Authenticate Method: authentication: " + authentication.isAuthenticated());
23+
}
24+
25+
@AfterReturning(value = "guru.springframework.services.security.LoginAspect.doAuthenticate()",
26+
returning = "authentication")
27+
public void logAfterAuthenticate( Authentication authentication){
28+
System.out.println("This is after the Authenticate Method authentication: " + authentication.isAuthenticated());
29+
}
30+
31+
@AfterThrowing("guru.springframework.services.security.LoginAspect.doAuthenticate() && args(authentication)")
32+
public void logAuthenicationException(Authentication authentication){
33+
String userDetails = (String) authentication.getPrincipal();
34+
System.out.println("Login failed for user: " + userDetails);
35+
}
36+
}

0 commit comments

Comments
 (0)