{"id":37996,"date":"2017-01-03T19:17:24","date_gmt":"2017-01-03T13:47:24","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=37996"},"modified":"2017-01-05T09:33:14","modified_gmt":"2017-01-05T04:03:14","slug":"how-to-perform-event-on-successful-login-via-spring-security-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-perform-event-on-successful-login-via-spring-security-in-grails\/","title":{"rendered":"How to Perform event on successful login via Spring Security in Grails"},"content":{"rendered":"<p>Some applications require to store and show last login of the user which is quite common. With this feature a user can verify the last login date and time upon successful login.<\/p>\n<p>I would like to explain this through a <strong>use case<\/strong> &#8211; One of the administrative <a title=\"Grails Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">application on Grails<\/a> required to hold last login date and time of the user, so that it can be shown to the user at the time of successful login.<\/p>\n<p><strong>Problem:<\/strong> By default spring security does not provide such feature of capturing last login time. Moreover, we did not have such properties in our domain which could persist user specific last login information.<\/p>\n<p><b>Solution<\/b>: Fortunately Spring is quite flexible framework so it is not a big deal to achieve this.\u00a0<strong>Spring provides a authentication success listener which can perform specified action upon successful login of user<\/strong>.<\/p>\n<p>Hence with the help of following configurations we can store user\u2019s last login date time after successful authenticated login.<\/p>\n<ol>\n<li><b> Add following field&#8217;s in your User domain :-<\/b><\/li>\n<\/ol>\n<p style=\"text-align: left;\">\u00a0\u00a0\u00a0 class User<\/p>\n<p style=\"text-align: left;\">\u00a0 {<\/p>\n<p style=\"text-align: left;\">\u00a0\u00a0\u00a0 Date lastLoginDate<\/p>\n<p style=\"text-align: left;\">\u00a0\u00a0\u00a0 Date currentLoginDate<\/p>\n<p style=\"text-align: left;\">\u00a0\u00a0 }<\/p>\n<ol start=\"2\">\n<li><b> Add following configurations in your \/grails-app\/conf\/application.groovy (auto generated after installing Spring security Core plugin)<\/b><\/li>\n<\/ol>\n<p>Enable your spring security events:-<\/p>\n<p>grails.plugin.springsecurity.useSecurityEventListener = true<\/p>\n<p><b>\/\/An event to be triggered after successful authentication and login of user.<\/b><\/p>\n<p>Modify your spring security successful authentication events:-<\/p>\n<p>grails.plugin.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx -&gt;<\/p>\n<p>User.withTransaction {<\/p>\n<p>def user = User.findByIdAndIsDeleted(appCtx.springSecurityService.principal.id, false)<\/p>\n<p>if (!user.isAttached())\/\/ Checks whether the domain instance is attached to a currently active Hibernate session.<\/p>\n<p>user.attach()<\/p>\n<p>user.lastLoginDate = user.currentLoginTime \/\/ update last login date<\/p>\n<p>user.currentLoginDate = new Date() \/\/ update current login date<\/p>\n<p>user.save(flush: true, failOnError: true)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Incase, you don&#8217;t need to retrieve the logged in user details, you can use<\/p>\n<p><b>onAuthenticationSuccessEvent<\/b><\/p>\n<p><b>3.\u00a0That&#8217;s it, its done!<\/b><\/p>\n<p><b>Now use user.lastLoginDate to show last login time.<\/b><\/p>\n<p><b>Similarly, we can call an event on AuthenticationFailure\/Unsuccessful Login via AbstractAuthenticationFailureEvent:<\/b><\/p>\n<p><b>\/\/An event to be triggered on AuthenticationFailure\/Unsuccessful Login of user.<\/b><\/p>\n<p>grails.plugin.springsecurity.onAbstractAuthenticationFailureEvent = { event, appCtx -&gt;<\/p>\n<p>\/\/ Action to be performed (Example : send e-mail)<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some applications require to store and show last login of the user which is quite common. With this feature a user can verify the last login date and time upon successful login. I would like to explain this through a use case &#8211; One of the administrative application on Grails required to hold last login [&hellip;]<\/p>\n","protected":false},"author":685,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":29},"categories":[2026,7,1],"tags":[2270,4840,4841,672],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/37996"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/685"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=37996"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/37996\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=37996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=37996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=37996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}