shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
package com.iplatform.base;
 
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 第三方管理器默认实现。
 * @author 时克英
 * @date 2023-07-03
 */
public class ThirdPartyManager {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private final Map<String, ThirdPartyAuthentication> authenticationMap = new ConcurrentHashMap<>(8);
 
    /**
     * 注册一个第三方认证对象到平台。
     * <pre>
     *     1)其中,name属性通常是第三方请求登录路径中的后缀部分,如:/third_party/login/aaa
     *     2)平台支持多种第三方认证同时使用,只要在业务中注册即可。
     * </pre>
     * @param thirdPartyAuthentication
     * @date 2023-07-03
     */
    public void register(ThirdPartyAuthentication thirdPartyAuthentication){
        if(thirdPartyAuthentication == null){
            throw new IllegalArgumentException("注册第三方认证对象为空");
        }
        String key = thirdPartyAuthentication.getName();
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("自定义第三方认证对象名称不存在:getName()");
        }
        if(this.authenticationMap.get(key) != null){
            throw new IllegalArgumentException("已经存在第三方认证定义:" + key);
        }
        this.authenticationMap.put(key, thirdPartyAuthentication);
        logger.info("注册'第三方认证对象' = " + key + ", " + thirdPartyAuthentication.getDescription());
    }
 
    /**
     * 返回一个第三方认证对象
     * @param key 认证对象的名称,参考:{@linkplain ThirdPartyAuthentication#getName()}
     * @return
     * @date 2023-07-03
     */
    public ThirdPartyAuthentication getThirdPartyAuthentication(String key){
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("请提供第三方认证标识");
        }
        return this.authenticationMap.get(key);
    }
}