{"id":1426,"date":"2023-03-20T21:53:15","date_gmt":"2023-03-20T13:53:15","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1426"},"modified":"2023-04-28T21:03:30","modified_gmt":"2023-04-28T13:03:30","slug":"publishing-and-listening-for-spring-security-oauth2-user-login-failure-events","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/20\/publishing-and-listening-for-spring-security-oauth2-user-login-failure-events\/","title":{"rendered":"Spring Security OAuth2 \u7528\u6237\u767b\u5f55\u5931\u8d25\u4e8b\u4ef6\u53d1\u5e03\u53ca\u76d1\u542c"},"content":{"rendered":"<h2>Spring\u4e8b\u4ef6\u7b80\u4ecb<\/h2>\n<p>Spring\u4e2d\u7684\u4e8b\u4ef6\u5206\u4e3a\u4e09\u90e8\u5206\uff1a\u4e8b\u4ef6\u3001\u76d1\u542c\u5668\u3001\u4e8b\u4ef6\u6e90\uff0c\u5176\u4e2d\u4e8b\u4ef6\u662f\u6838\u5fc3\uff0c\u6d89\u53ca\u5230ApplicationEventPublisher\u63a5\u53e3\u3001ApplicationEvent\u7c7b\u3001ApplicationListener\u63a5\u53e3<\/p>\n<h2>\u5b9a\u4e49\u7528\u6237\u767b\u5f55\u5931\u8d25\u4e8b\u4ef6<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * @Description: \u5b9a\u4e49\u7528\u6237\u767b\u5f55\u5931\u8d25\u4e8b\u4ef6\n * @Package: UserLoginFailedEvent\n * @Version: 1.0\n *\/\npublic class UserLoginFailedEvent extends ApplicationEvent {\n    public UserLoginFailedEvent(Authentication authentication) {\n        super(authentication);\n    }\n}<\/code><\/pre>\n<h2>\u5b9a\u4e49\u4e8b\u4ef6\u76d1\u542c\u5668<\/h2>\n<pre><code class=\"language-java\">\/**\n * @Description: \u7528\u6237\u767b\u5f55\u5931\u8d25\u76d1\u542c\u5668\n * @Package: cn.appblog.security.oauth2.event.listener.UserLoginFailedListener\n * @Version: 1.0\n *\/\n@Component\npublic class UserLoginFailedListener implements ApplicationListener&lt;UserLoginFailedEvent&gt; {\n    @Override\n    public void onApplicationEvent(UserLoginFailedEvent event) {\n        System.out.println(&quot;\u7528\u6237\u9a8c\u8bc1\u4fe1\u606f: failure&quot;);\n    }\n}<\/code><\/pre>\n<h2>\u4e8b\u4ef6\u53d1\u5e03<\/h2>\n<p>\u5b9a\u4e49ApplicationEventPublisher\u5bf9\u8c61\u5e76\u53d1\u5e03\u4e8b\u4ef6<\/p>\n<pre><code class=\"language-java\">\/**\n * @Description: \u7528\u6237\u81ea\u5b9a\u4e49\u8eab\u4efd\u8ba4\u8bc1\n * @Package: cn.appblog.security.oauth2.provider.UserAuthenticationProvider\n * @Version: 1.0\n *\/\n@Component\npublic class UserAuthenticationProvider implements AuthenticationProvider {\n    @Autowired\n    private UserAuthDetailsService authUserDetailsService;\n    @Autowired\n    private PasswordEncoder passwordEncoder;\n    @Autowired\n    private ApplicationEventPublisher publisher;\n\n    \/**\n     * \u8ba4\u8bc1\u5904\u7406\uff0c\u8fd4\u56de\u4e00\u4e2aAuthentication\u7684\u5b9e\u73b0\u7c7b\u5219\u4ee3\u8868\u8ba4\u8bc1\u6210\u529f\uff0c\u8fd4\u56denull\u5219\u4ee3\u8868\u8ba4\u8bc1\u5931\u8d25\n     *\/\n    @Override\n    public Authentication authenticate(Authentication authentication) throws AuthenticationException {\n        String username = authentication.getName();\n        String password = (String) authentication.getCredentials();\n        if (StringUtils.isBlank(username)) {\n            throw new UsernameNotFoundException(&quot;username\u7528\u6237\u540d\u4e0d\u53ef\u4ee5\u4e3a\u7a7a&quot;);\n        }\n        if (StringUtils.isBlank(password)) {\n            throw new BadCredentialsException(&quot;\u5bc6\u7801\u4e0d\u53ef\u4ee5\u4e3a\u7a7a&quot;);\n        }\n        \/\/\u83b7\u53d6\u7528\u6237\u4fe1\u606f\n        UserDetails user = authUserDetailsService.loadUserByUsername(username);\n        \/\/\u6bd4\u8f83\u524d\u7aef\u4f20\u5165\u7684\u5bc6\u7801\u660e\u6587\u548c\u6570\u636e\u5e93\u4e2d\u52a0\u5bc6\u7684\u5bc6\u7801\u662f\u5426\u76f8\u7b49\n        if (!passwordEncoder.matches(password, user.getPassword())) {\n            \/\/\u53d1\u5e03\u5bc6\u7801\u4e0d\u6b63\u786e\u4e8b\u4ef6\n            publisher.publishEvent(new UserLoginFailedEvent(authentication));\n            throw new BadCredentialsException(&quot;password\u5bc6\u7801\u4e0d\u6b63\u786e&quot;);\n        }\n        \/\/\u83b7\u53d6\u7528\u6237\u6743\u9650\u4fe1\u606f\n        Collection&lt;? extends GrantedAuthority&gt; authorities = user.getAuthorities();\n        return new UsernamePasswordAuthenticationToken(user, password, authorities);\n    }\n\n    \/**\n     * \u5982\u679c\u8be5AuthenticationProvider\u652f\u6301\u4f20\u5165\u7684Authentication\u5bf9\u8c61\uff0c\u5219\u8fd4\u56detrue\n     *\/\n    @Override\n    public boolean supports(Class&lt;?&gt; aClass) {\n        return aClass.equals(UsernamePasswordAuthenticationToken.class);\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u64cd\u4f5c\u5c31\u53ef\u4ee5\u5728\u7528\u6237\u767b\u5f55\u5931\u8d25\u540e\u6709\u6548\u7684\u76d1\u542c\u5230\u5931\u8d25\u72b6\u6001\uff0c\u5e76\u4e14\u5f88\u597d\u7684\u89e3\u8026\u4ee3\u7801<\/p>\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>Spring\u4e8b\u4ef6\u7b80\u4ecb Spring\u4e2d\u7684\u4e8b\u4ef6\u5206\u4e3a\u4e09\u90e8\u5206\uff1a\u4e8b\u4ef6\u3001\u76d1\u542c\u5668\u3001\u4e8b\u4ef6\u6e90\uff0c\u5176\u4e2d\u4e8b\u4ef6\u662f\u6838\u5fc3\uff0c\u6d89\u53ca\u5230Applic [&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-1426","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\/1426","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=1426"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1426\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}