2

我正在使用 SecurityListener 来监听身份验证事件。

class ProductSecurityEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    def eventService;

    void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
         def user = event.getAuthentication().getPrincipal();
         def secUser = SecUser.findByUsername(user.username)
         eventService.logEventLogin(secUser);
}

我在 resources.groovy 中定义了我的监听器 bean

beans = {
    productSecurityEventListener(ProductSecurityEventListener);
}

我在我的 config.groovy 中启用它

grails.plugins.springsecurity.useSecurityEventListener = true

我不断在 eventservice.logEventLogin 上获得一个空指针

java.lang.NullPointerException: Cannot invoke method logEventLogin() on null object

我调试,我可以看到事件触发和事件方法被调用。我一生都无法弄清楚为什么不注入 eventService 。

这是我的服务

class EventService {
    def logEventLogin(SecUser secUser) {
        event event = new Event(eventType: EventEnum.LOGIN,  secUser: secUser, eventDate: new Date());
        event.save();
    }

欢迎任何想法或提示?

4

2 回答 2

4

作为 Igor 方法的替代方案,您可以为您的 bean 启用自动装配,因此您需要添加的任何未来依赖项(例如,如果您将来发现您需要def grailsApplication)也将自动装配:

beans = {
    productSecurityEventListener(ProductSecurityEventListener) { bean ->
        bean.autowire = 'byName'
    }
}
于 2013-09-02T12:34:06.240 回答
2

您必须设置eventService属性:

beans = {
    productSecurityEventListener(ProductSecurityEventListener) {
       eventService = ref('eventService')
    }
}
于 2013-09-02T11:45:51.413 回答