Announcement Announcement Module
Collapse
No announcement yet.
AspectJ - Pointcut to advise when an AccessDeniedException is thrown Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 11:39 PM.

  • #2
    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
                     }
              }

    Comment


    • #3
      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
      }

      Comment


      • #4
        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

        Comment

        Working...
        X