2010-08-09 167 views
1

是否可以創建一個自定義@Aspect並將其應用於Spring Security(3.0.3)中的類/方法?Spring Security和AOP

我正在嘗試做一些日誌記錄的登錄/註銷請求,並沒有我的通知被觸發。

我使用@AspectJ註釋,這裏是我如何裝飾我的方法:

@After("execution (* org.springframework.security.authentication.ProviderManager.doAuthentication(..))") 

回答

2

而是使用ApplicationListener趕成功登錄。有關其他事件類型,請參閱Javadocs

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.context.ApplicationListener; 
import org.springframework.security.authentication.event.AuthenticationSuccessEvent; 
import org.springframework.stereotype.Component; 

@Component 
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { 
    private static final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessListener.class); 

    @Override 
    public void onApplicationEvent(final AuthenticationSuccessEvent event) { 
     logger.debug("User {} logged in", event.getAuthentication().getName()); 
    } 
} 
+0

好主意,謝謝!不過,我能夠讓自己的方面正常工作,但我也會嘗試這種方式來熟悉。 – 2010-08-09 12:02:24

+0

這看起來很有魅力。 – 2012-05-31 09:46:15