(window.webpackJsonp=window.webpackJsonp||[]).push([[231],{654:function(t,e,n){"use strict";n.r(e);var i=n(56),a=Object(i.a)({},(function(){var t=this,e=t.$createElement,n=t._self._c||e;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"authentication-events"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#authentication-events"}},[t._v("#")]),t._v(" Authentication Events")]),t._v(" "),n("p",[t._v("For each authentication that succeeds or fails, a "),n("code",[t._v("AuthenticationSuccessEvent")]),t._v(" or "),n("code",[t._v("AbstractAuthenticationFailureEvent")]),t._v(" is fired, respectively.")]),t._v(" "),n("p",[t._v("To listen for these events, you must first publish an "),n("code",[t._v("AuthenticationEventPublisher")]),t._v(".\nSpring Security’s "),n("code",[t._v("DefaultAuthenticationEventPublisher")]),t._v(" will probably do fine:")]),t._v(" "),n("p",[t._v("Java")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Bean\npublic AuthenticationEventPublisher authenticationEventPublisher\n (ApplicationEventPublisher applicationEventPublisher) {\n return new DefaultAuthenticationEventPublisher(applicationEventPublisher);\n}\n")])])]),n("p",[t._v("Kotlin")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Bean\nfun authenticationEventPublisher\n (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {\n return DefaultAuthenticationEventPublisher(applicationEventPublisher)\n}\n")])])]),n("p",[t._v("Then, you can use Spring’s "),n("code",[t._v("@EventListener")]),t._v(" support:")]),t._v(" "),n("p",[t._v("Java")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Component\npublic class AuthenticationEvents {\n\t@EventListener\n public void onSuccess(AuthenticationSuccessEvent success) {\n\t\t// ...\n }\n\n @EventListener\n public void onFailure(AbstractAuthenticationFailureEvent failures) {\n\t\t// ...\n }\n}\n")])])]),n("p",[t._v("Kotlin")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Component\nclass AuthenticationEvents {\n @EventListener\n fun onSuccess(success: AuthenticationSuccessEvent?) {\n // ...\n }\n\n @EventListener\n fun onFailure(failures: AbstractAuthenticationFailureEvent?) {\n // ...\n }\n}\n")])])]),n("p",[t._v("While similar to "),n("code",[t._v("AuthenticationSuccessHandler")]),t._v(" and "),n("code",[t._v("AuthenticationFailureHandler")]),t._v(", these are nice in that they can be used independently from the servlet API.")]),t._v(" "),n("h2",{attrs:{id:"adding-exception-mappings"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#adding-exception-mappings"}},[t._v("#")]),t._v(" Adding Exception Mappings")]),t._v(" "),n("p",[n("code",[t._v("DefaultAuthenticationEventPublisher")]),t._v(" by default will publish an "),n("code",[t._v("AbstractAuthenticationFailureEvent")]),t._v(" for the following events:")]),t._v(" "),n("table",[n("thead",[n("tr",[n("th",[t._v("Exception")]),t._v(" "),n("th",[t._v("Event")])])]),t._v(" "),n("tbody",[n("tr",[n("td",[n("code",[t._v("BadCredentialsException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureBadCredentialsEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("UsernameNotFoundException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureBadCredentialsEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("AccountExpiredException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureExpiredEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("ProviderNotFoundException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureProviderNotFoundEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("DisabledException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureDisabledEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("LockedException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureLockedEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("AuthenticationServiceException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureServiceExceptionEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("CredentialsExpiredException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureCredentialsExpiredEvent")])])]),t._v(" "),n("tr",[n("td",[n("code",[t._v("InvalidBearerTokenException")])]),t._v(" "),n("td",[n("code",[t._v("AuthenticationFailureBadCredentialsEvent")])])])])]),t._v(" "),n("p",[t._v("The publisher does an exact "),n("code",[t._v("Exception")]),t._v(" match, which means that sub-classes of these exceptions won’t also produce events.")]),t._v(" "),n("p",[t._v("To that end, you may want to supply additional mappings to the publisher via the "),n("code",[t._v("setAdditionalExceptionMappings")]),t._v(" method:")]),t._v(" "),n("p",[t._v("Java")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Bean\npublic AuthenticationEventPublisher authenticationEventPublisher\n (ApplicationEventPublisher applicationEventPublisher) {\n Map,\n Class> mapping =\n Collections.singletonMap(FooException.class, FooEvent.class);\n AuthenticationEventPublisher authenticationEventPublisher =\n new DefaultAuthenticationEventPublisher(applicationEventPublisher);\n authenticationEventPublisher.setAdditionalExceptionMappings(mapping);\n return authenticationEventPublisher;\n}\n")])])]),n("p",[t._v("Kotlin")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Bean\nfun authenticationEventPublisher\n (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {\n val mapping: Map, Class> =\n mapOf(Pair(FooException::class.java, FooEvent::class.java))\n val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher)\n authenticationEventPublisher.setAdditionalExceptionMappings(mapping)\n return authenticationEventPublisher\n}\n")])])]),n("h2",{attrs:{id:"default-event"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#default-event"}},[t._v("#")]),t._v(" Default Event")]),t._v(" "),n("p",[t._v("And, you can supply a catch-all event to fire in the case of any "),n("code",[t._v("AuthenticationException")]),t._v(":")]),t._v(" "),n("p",[t._v("Java")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Bean\npublic AuthenticationEventPublisher authenticationEventPublisher\n (ApplicationEventPublisher applicationEventPublisher) {\n AuthenticationEventPublisher authenticationEventPublisher =\n new DefaultAuthenticationEventPublisher(applicationEventPublisher);\n authenticationEventPublisher.setDefaultAuthenticationFailureEvent\n (GenericAuthenticationFailureEvent.class);\n return authenticationEventPublisher;\n}\n")])])]),n("p",[t._v("Kotlin")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("@Bean\nfun authenticationEventPublisher\n (applicationEventPublisher: ApplicationEventPublisher?): AuthenticationEventPublisher {\n val authenticationEventPublisher = DefaultAuthenticationEventPublisher(applicationEventPublisher)\n authenticationEventPublisher.setDefaultAuthenticationFailureEvent(GenericAuthenticationFailureEvent::class.java)\n return authenticationEventPublisher\n}\n")])])]),n("p",[n("RouterLink",{attrs:{to:"/en/spring-security/logout.html"}},[t._v("Logout")]),n("RouterLink",{attrs:{to:"/en/authorization/index.html"}},[t._v("Authorization")])],1)])}),[],!1,null,null,null);e.default=a.exports}}]);