{"id":1430,"date":"2023-03-20T21:56:52","date_gmt":"2023-03-20T13:56:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1430"},"modified":"2023-04-28T21:02:08","modified_gmt":"2023-04-28T13:02:08","slug":"spring-security-user-authentication-success-failure-custom-implementation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/20\/spring-security-user-authentication-success-failure-custom-implementation\/","title":{"rendered":"Spring Security\u7528\u6237\u8ba4\u8bc1\u6210\u529f\u5931\u8d25\u81ea\u5b9a\u4e49\u5b9e\u73b0"},"content":{"rendered":"<p>\u5728<code>org.springframework.security.authentication.event<\/code>\u5305\u4e0b\u5b9a\u4e49\u4e86\u53d1\u751f\u8ba4\u8bc1\u65f6\u7684\u6240\u6709\u4e8b\u4ef6\u7c7b\u578b\uff0c\u5176\u4e2d<code>AbstractAuthenticationEvent<\/code>\u662f\u6240\u6709\u4e8b\u4ef6\u7684\u7236\u7c7b\uff0c\u5176\u5b83\u4e8b\u4ef6\u90fd\u7ee7\u627f\u4e8e<code>AbstractAuthenticationEvent<\/code>\uff0c\u5176\u5b50\u7c7b\u6709<\/p>\n<ul>\n<li><code>AbstractAuthenticationFailureEvent<\/code><\/li>\n<li><code>AuthenticationFailureBadCredentialsEvent<\/code><\/li>\n<li><code>AuthenticationFailureCredentialsExpiredEvent<\/code><\/li>\n<li><code>AuthenticationFailureDisabledEvent<\/code><\/li>\n<li><code>AuthenticationFailureExpiredEvent<\/code><\/li>\n<li><code>AuthenticationFailureLockedEvent<\/code><\/li>\n<li><code>AuthenticationFailureProviderNotFoundEvent<\/code><\/li>\n<li><code>AuthenticationFailureProxyUntrustedEvent<\/code><\/li>\n<li><code>AuthenticationFailureServiceExceptionEvent<\/code><\/li>\n<li><code>AuthenticationSuccessEvent<\/code><\/li>\n<li><code>InteractiveAuthenticationSuccessEvent<\/code><\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<p>\u800c<code>AbstractAuthenticationFailureEvent<\/code>\u53c8\u662f\u6240\u6709\u8ba4\u8bc1\u5f02\u5e38\u53d1\u5e03\u4e8b\u4ef6\u7684\u62bd\u8c61\u7c7b\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u5206\u5f00\u6210\u4e24\u4e2a\u76d1\u542c\u5668\uff1b<\/p>\n<h2>\u5b9a\u4e49\u8ba4\u8bc1\u6210\u529f\u53d1\u5e03\u4e8b\u4ef6\u76d1\u542c\u5668<\/h2>\n<pre><code class=\"language-java\">\/**\n * @Description: \u7528\u6237\u767b\u5f55\u6210\u529f\u76d1\u542c\u5668\u4e8b\u4ef6\n * @Package: cn.appblog.security.oauth2.event.listener.AuthencationSuccessListener\n * @Version: 1.0\n *\/\n@Component\npublic class AuthencationSuccessListener implements ApplicationListener&lt;AuthenticationSuccessEvent&gt; {\n\n    @Override\n    public void onApplicationEvent(AuthenticationSuccessEvent event) {\n        \/\/\u7528\u6237\u901a\u8fc7\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u767b\u5f55\u6210\u529f\n        System.out.println(&quot;---AuthenticationSuccessEvent---&quot;);\n    }\n\n}<\/code><\/pre>\n<p>\u5f53\u7136\u5982\u679c\u6709\u9700\u8981\u53ef\u4ee5\u5c06<code>AuthenticationSuccessEvent<\/code>\u66f4\u6362\u4e3a<code>InteractiveAuthenticationSuccessEvent<\/code>\uff0c\u90fd\u662f\u8ba4\u8bc1\u6210\u529f\uff0c\u4f46\u662f<code>InteractiveAuthenticationSuccessEvent<\/code>\u8868\u793a\u901a\u8fc7\u81ea\u52a8\u4ea4\u4e92\u7684\u624b\u6bb5\u6765\u767b\u5f55\u6210\u529f\uff0c\u6bd4\u5982cookie\u81ea\u52a8\u767b\u5f55<\/p>\n<h2>\u5b9a\u4e49\u8ba4\u8bc1\u5931\u8d25\u4e8b\u4ef6\u53d1\u5e03\u76d1\u542c\u5668<\/h2>\n<pre><code class=\"language-java\">\/**\n * @Description: \u7528\u6237\u767b\u5f55\u6210\u529f\u76d1\u542c\u5668\u4e8b\u4ef6\n * @Package: cn.appblog.security.oauth2.event.listener.AuthencationFailureListener\n * @Version: 1.0\n *\/\n@Component\npublic class AuthencationFailureListener implements ApplicationListener&lt;AbstractAuthenticationFailureEvent&gt; {\n    @Override\n    public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {\n        if (event instanceof AuthenticationFailureBadCredentialsEvent) {\n            \/\/\u63d0\u4f9b\u7684\u51ed\u636e\u662f\u9519\u8bef\u7684\uff0c\u7528\u6237\u540d\u6216\u8005\u5bc6\u7801\u9519\u8bef\n            System.out.println(&quot;---AuthenticationFailureBadCredentialsEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureCredentialsExpiredEvent) {\n            \/\/\u9a8c\u8bc1\u901a\u8fc7\uff0c\u4f46\u662f\u5bc6\u7801\u8fc7\u671f\n            System.out.println(&quot;---AuthenticationFailureCredentialsExpiredEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureDisabledEvent) {\n            \/\/\u9a8c\u8bc1\u8fc7\u4e86\u4f46\u662f\u8d26\u6237\u88ab\u7981\u7528\n            System.out.println(&quot;---AuthenticationFailureDisabledEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureExpiredEvent) {\n            \/\/\u9a8c\u8bc1\u901a\u8fc7\u4e86\uff0c\u4f46\u662f\u8d26\u53f7\u5df2\u7ecf\u8fc7\u671f\n            System.out.println(&quot;---AuthenticationFailureExpiredEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureLockedEvent) {\n            \/\/\u8d26\u6237\u88ab\u9501\u5b9a\n            System.out.println(&quot;---AuthenticationFailureLockedEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureProviderNotFoundEvent) {\n            \/\/\u914d\u7f6e\u9519\u8bef\uff0c\u6ca1\u6709\u5408\u9002\u7684AuthenticationProvider\u6765\u5904\u7406\u767b\u5f55\u9a8c\u8bc1\n            System.out.println(&quot;---AuthenticationFailureProviderNotFoundEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureProxyUntrustedEvent) {\n            \/\/\u4ee3\u7406\u4e0d\u53d7\u4fe1\u4efb\uff0c\u7528\u4e8eOauth\u3001CAS\u8fd9\u7c7b\u4e09\u65b9\u9a8c\u8bc1\u7684\u60c5\u5f62\uff0c\u591a\u5c5e\u4e8e\u914d\u7f6e\u9519\u8bef\n            System.out.println(&quot;---AuthenticationFailureProxyUntrustedEvent---&quot;);\n        } else if (event instanceof AuthenticationFailureServiceExceptionEvent) {\n            \/\/\u5176\u4ed6\u4efb\u4f55\u5728AuthenticationManager\u4e2d\u5185\u90e8\u53d1\u751f\u7684\u5f02\u5e38\u90fd\u4f1a\u88ab\u5c01\u88c5\u6210\u6b64\u7c7b\n            System.out.println(&quot;---AuthenticationFailureServiceExceptionEvent---&quot;);\n        }\n    }\n}<\/code><\/pre>\n<p>\u672c\u6587\u8f6c\u8f7d\u53c2\u8003 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/blog.csdn.net\/yaomingyang\/column\/info\/41645\" title=\"\u539f\u6587\">\u539f\u6587<\/a> \u5e76\u52a0\u4ee5\u8c03\u8bd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728org.springframework.security.authentication.event\u5305\u4e0b\u5b9a\u4e49\u4e86 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[354],"tags":[353],"class_list":["post-1430","post","type-post","status-publish","format-standard","hentry","category-spring-security","tag-oauth2"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1430"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1430\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}