86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2018-2020 by jason. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Vinay Sajip
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.
 * VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */
 
package cn.mb.cloud.auth.security.handler;
 
import cn.mb.cloud.auth.security.component.MbCloudAuthUser;
import cn.mb.cloud.auth.security.service.MbCloudUserAuthDetailsService;
import cn.mb.cloud.auth.security.service.impl.MbCloudRedisTokenStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Component;
 
import java.util.Collection;
 
/**
 * @author: jason
 * @Date: 2019-05-17 17:20
 * @Description:
 */
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class GlobalSuccessHandler implements ApplicationListener<AuthenticationSuccessEvent> {
 
    private ThreadLocal<Integer> lastEventHashCode = ThreadLocal.withInitial(() -> Integer.valueOf(0));
 
 
    @Autowired
    private MbCloudUserAuthDetailsService userAuthDetailsService;
 
    @Autowired
    private MbCloudRedisTokenStore removeAccessToken;
    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        final int currentEventHashCode = event.hashCode();
        log.info("监听到认证成功事件 ---> Last event hash code:{},current event hash code:{}", lastEventHashCode.get(),
                currentEventHashCode);
 
        Authentication authentication = event.getAuthentication();
        Object principal = authentication.getPrincipal();
 
        if (principal instanceof MbCloudAuthUser) {
 
            MbCloudAuthUser user = (MbCloudAuthUser)principal;
            Collection<OAuth2AccessToken> mbcloud = removeAccessToken.findTokensByClientIdAndUserName("mbcloud", user.getUsername());
            mbcloud.stream().forEach(item->{
                removeAccessToken.removeAccessToken(item.getValue());
            });
 
            MbCloudAuthUser authUser = (MbCloudAuthUser) principal;
            // 认证成功事件会触发两次,防止处理两次影响性能,所以判断Hash Code避免重复操作
            if (lastEventHashCode.get().intValue() != currentEventHashCode) {
                userAuthDetailsService.loadUserPermissions(authUser.getId());
            }
        }
    }
}