Results 1 to 4 of 4

Thread: AspectJ - Pointcut to advise when an AccessDeniedException is thrown

  1. #1
    Join Date
    Jan 2010
    Posts
    4

    Default AspectJ - Pointcut to advise when an AccessDeniedException is thrown

    I'm trying write an aspectJ pointcut that is called every time an AccessDeniedException is thrown in my webapp. I'm using @PreAuthorize on all my service methods, and I simply want to write to the database each time it returns an AccessDeniedException. I cannot seem to get it right.

    I am using compile-time aspectJ weaving. Spring 3.1.0

    I have tried these (and more) with no luck. I have other aspects in my app that work with no problem, so my setup should be correct. Any help is greatly appreciated.

    Code:
    @Before("call(org.springframework.security.access.AccessDeniedException.new(..))")
    Code:
    @AfterThrowing(pointcut="execution(* *.AffirmativeBased.decide(..))", throwing="ex")
    Code:
    @After("handler(org.springframework.security.access.AccessDeniedException)")
    Here is my maven plugin for aspectJ.

    Code:
    <plugin>
    				<groupId>org.codehaus.mojo</groupId>
    				<artifactId>aspectj-maven-plugin</artifactId>
    				<version>1.4</version>
    				<configuration>
    					<source>1.6</source>
    					<weaveDependencies>
                            <weaveDependency>
                                <groupId>org.springframework.security</groupId>
    							<artifactId>spring-security-web</artifactId>
                            </weaveDependency>
                              <weaveDependency>
                                <groupId>org.springframework.security</groupId>
    							<artifactId>spring-security-core</artifactId>
                            </weaveDependency>
                        </weaveDependencies>
    					<aspectLibraries>
    						<aspectLibrary>
    							<groupId>org.springframework</groupId>
    			           				<artifactId>spring-aspects</artifactId>
    						</aspectLibrary>
    					</aspectLibraries>
    				</configuration>
    				<executions>
    				  <execution>
    				    <goals>
    				      <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
    				    </goals>
    				  </execution>
    				</executions>
    			</plugin>
    Last edited by zeke2003; Jul 12th, 2012 at 10:39 PM.

  2. #2
    Join Date
    Jan 2010
    Posts
    4

    Default

    The solution that I wound up going with was to use the below pointcut, and check to see if the exception was of type AccessDeniedException.

    Code:
    @AfterThrowing(pointcut="call(public * *..*Service*.*(..))", throwing="ex")
    Code:
    public void accessDenied(JoinPoint joinPoint, Exception ex)  {
    		if(ex instanceof AccessDeniedException) {
                             //log it
                     }
              }

  3. #3
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    Correct, but you can further improve your code by getting rid of the 'if' statement:

    Code:
    public void accessDenied(JoinPoint joinPoint, AccessDeniedException ex)  {
                  //log it
    }

  4. #4
    Join Date
    Jan 2010
    Posts
    4

    Default

    Quote Originally Posted by Enrico Pizzi View Post
    Correct, but you can further improve your code by getting rid of the 'if' statement:

    Code:
    public void accessDenied(JoinPoint joinPoint, AccessDeniedException ex)  {
                  //log it
    }
    Great, thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •