{"id":23541,"date":"2015-07-23T16:04:20","date_gmt":"2015-07-23T10:34:20","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23541"},"modified":"2015-07-24T13:52:28","modified_gmt":"2015-07-24T08:22:28","slug":"override-login-and-logout-of-spring-security-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/override-login-and-logout-of-spring-security-in-grails\/","title":{"rendered":"Override login and logout of Spring Security in Grails"},"content":{"rendered":"<p>What if our use case is to perform any custom task for login and logout while keeping the beauty of spring security intact.\u00a0My use case\u00a0was to make a third party SOAP API call to perform login\/logout sending user&#8217;s detail as parameter.<\/p>\n<p><strong>Override Login<\/strong><\/p>\n<p>Write own Authentication Provider class that extends <em>AbstractUserDetailsAuthenticationProvider<\/em> and override <em>authenticate<\/em> method. For reference we can see <a href=\"https:\/\/github.com\/spring-projects\/spring-security\/blob\/master\/core\/src\/main\/java\/org\/springframework\/security\/authentication\/dao\/AbstractUserDetailsAuthenticationProvider.java\" target=\"_blank\"><em>AbstractUserDetailsAuthenticationProvider<\/em><\/a>.<\/p>\n<p>Next we need to :<\/p>\n<p>1. Create bean of our custom Authentication Provider. It can be done in <em>Resources.groovy<\/em> by writing<\/p>\n<p>[java] beans = {<br \/>\n       &#8230;.<br \/>\n       customAuthenticationProvider(CustomAuthenticationProvider)<br \/>\n       &#8230;.<br \/>\n } [\/java]<\/p>\n<p>2. Register bean of our custom AuthenticationProvider . It can be done in <em>BootStrap.groovy<\/em> by writing :<\/p>\n<p>[java]<br \/>\ndef init {<br \/>\n    &#8230;<br \/>\n    SpringSecurityUtils.registerProvider(&quot;customAuthenticationProvider&quot;)<br \/>\n     &#8230;<br \/>\n}[\/java]<\/p>\n<p><strong>Override\u00a0Logout<\/strong><\/p>\n<p>We have come to know three ways to do this :<\/p>\n<p>1. In most of the cases, we can write our custom task in target (action of corresponding link i.e. Logout, for example &#8216;index&#8217; action of &#8216;LogoutController&#8217;) then redirect flow to\u00a0<em>j_spring_security_logout.<\/em><\/p>\n<p>2. Write own LogoutHandler class that extends <em>LogoutHandler<\/em> and override <em>logout<\/em> method. For reference we can see <a href=\"https:\/\/github.com\/spring-projects\/spring-security\/blob\/master\/web\/src\/main\/java\/org\/springframework\/security\/web\/authentication\/logout\/SecurityContextLogoutHandler.java\" target=\"_blank\"><em>SecurityContextLogoutHandler<\/em><\/a>.<\/p>\n<p>Next we need to create bean and register same of our custom LogoutHandler as we did for our custom AuthenticationProvider. Do I need to say that this time we need to write &#8220;SpringSecurityUtils.registerLogoutHandler&#8221; instead of &#8220;SpringSecurityUtils.registerProvider&#8221;? \u00a0 :-P.<\/p>\n<p>3. If we need to be sure that we are making third party API call once Spring&#8217;s logout was completed successfully, we can write our own Session Timeout Listener. It can be done in two ways :<\/p>\n<p>a) Implementing <em>HttpSessionListener<\/em> :<\/p>\n<p>[java] <\/p>\n<p>public class MySessionListener implements HttpSessionListener {<br \/>\n      public void sessionDestroyed(HttpSessionEvent sessionEvent) {<br \/>\n      \/* Do whatever we want *\/<br \/>\n      }<br \/>\n}[\/java]<\/p>\n<p>To see this listener working, we need to add this listener in servlet context. To do this, In BootStrap.groovy, add<\/p>\n<p>[java] def init = { servletContext &#8211; &gt;<br \/>\n servletContext.addListener(MySessionListener)<br \/>\n }[\/java]<\/p>\n<p>b) Implementing <em>ApplicationListener<\/em> :<\/p>\n<p>[java]<br \/>\npublic class SessionTimeoutListener implements ApplicationListener&lt;SessionDestroyedEvent&gt; {<br \/>\n    @Override<br \/>\n    public void onApplicationEvent(SessionDestroyedEvent event) {<br \/>\n       \/*Do whatever we want to do. For example, To get Username of logged out        user, we can write next block of code*\/<br \/>\n       SecurityContext lstSecurityContext = event.getSecurityContext();<br \/>\n       UserDetails ud;<br \/>\n       for (SecurityContext securityContext : lstSecurityContext) {<br \/>\n         ud = (UserDetails) securityContext.getAuthentication().getPrincipal();<br \/>\n         System.out.println(&quot;Username of logged out user :&quot; + ud.username)<br \/>\n      }<br \/>\n    }<br \/>\n } [\/java]<\/p>\n<p>To see it working, we just need to create it&#8217;s bean in Resources.groovy (As we did for CustomAuthenticationProvider and CustomLogoutHandler above).<\/p>\n<p>Hope this helps \ud83d\ude42 If it doesn&#8217;t then let us help you with your use case. Comment your use case below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What if our use case is to perform any custom task for login and logout while keeping the beauty of spring security intact.\u00a0My use case\u00a0was to make a third party SOAP API call to perform login\/logout sending user&#8217;s detail as parameter. Override Login Write own Authentication Provider class that extends AbstractUserDetailsAuthenticationProvider and override authenticate method. [&hellip;]<\/p>\n","protected":false},"author":230,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":54},"categories":[1],"tags":[2018,4840,672],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23541"}],"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\/230"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23541"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23541\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}