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