grails - 无法调用我的安全监听器中的服务

标签 grails

我正在使用 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

我调试后可以看到事件触发和事件方法被调用。我就是想不通为什么没有注入(inject) eventService。

这是我的服务

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

欢迎任何想法或提示?

最佳答案

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

beans = {
    productSecurityEventListener(ProductSecurityEventListener) { bean ->
        bean.autowire = 'byName'
    }
}

关于grails - 无法调用我的安全监听器中的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18572110/

相关文章:

grails - 我需要在Grails中返回 “render”吗?

json - 如何在 Grails 中将特定日期格式返回为 JSON?

grails - 创建Grails演示应用程序时出现未授权(401)错误

grails - 如何在另一个异步线程中使用 grails 模板渲染方法

flash - 执行时grails在 View 中显示Flash消息

json - <g:select>键具有json数据->在gsp垃圾中输出

Grails3 + mysql不使用 Camel 表示法创建表名和列名

grails - 使用Postfix配置Grails邮件插件

grails - 如何在 Grails 中实例化惰性列表?

javascript - 如何创建热键