GlobalAuthUtil.java 3.9 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3
package me.zhyd.oauth.utils;

import cn.hutool.core.codec.Base64;
4 5
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
智布道's avatar
智布道 已提交
6 7 8 9 10
import me.zhyd.oauth.exception.AuthException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
智布道's avatar
智布道 已提交
11
import java.net.URLDecoder;
智布道's avatar
智布道 已提交
12
import java.net.URLEncoder;
13 14
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
智布道's avatar
智布道 已提交
15 16
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
17
import java.util.*;
智布道's avatar
智布道 已提交
18

智布道's avatar
智布道 已提交
19 20 21 22 23 24 25
/**
 * 全局的工具类
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
智布道's avatar
智布道 已提交
26
public class GlobalAuthUtil {
27
    private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
智布道's avatar
智布道 已提交
28 29
    private static final String ALGORITHM = "HmacSHA256";

30
    public static String generateDingTalkSignature(String secretKey, String timestamp) {
31 32
        byte[] signData = sign(secretKey.getBytes(DEFAULT_ENCODING), timestamp.getBytes(DEFAULT_ENCODING));
        return urlEncode(new String(Base64.encode(signData, false)));
智布道's avatar
智布道 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    }

    private static byte[] sign(byte[] key, byte[] data) {
        try {
            Mac mac = Mac.getInstance(ALGORITHM);
            mac.init(new SecretKeySpec(key, ALGORITHM));
            return mac.doFinal(data);
        } catch (NoSuchAlgorithmException ex) {
            throw new AuthException("Unsupported algorithm: " + ALGORITHM, ex);
        } catch (InvalidKeyException ex) {
            throw new AuthException("Invalid key: " + Arrays.toString(key), ex);
        }
    }

智布道's avatar
智布道 已提交
47
    private static String urlEncode(String value) {
智布道's avatar
智布道 已提交
48 49 50 51 52
        if (value == null) {
            return "";
        }

        try {
53 54
            String encoded = URLEncoder.encode(value, GlobalAuthUtil.DEFAULT_ENCODING.displayName());
            return encoded.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F");
智布道's avatar
智布道 已提交
55
        } catch (UnsupportedEncodingException e) {
智布道's avatar
智布道 已提交
56 57 58 59 60 61 62 63 64
            throw new AuthException("Failed To Encode Uri", e);
        }
    }

    public static String urlDecode(String value) {
        if (value == null) {
            return "";
        }
        try {
65
            return URLDecoder.decode(value, GlobalAuthUtil.DEFAULT_ENCODING.displayName());
智布道's avatar
智布道 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        } catch (UnsupportedEncodingException e) {
            throw new AuthException("Failed To Decode Uri", e);
        }
    }

    public static Map<String, String> parseStringToMap(String accessTokenStr) {
        Map<String, String> res = new HashMap<>();
        if (accessTokenStr.contains("&")) {
            String[] fields = accessTokenStr.split("&");
            for (String field : fields) {
                if (field.contains("=")) {
                    String[] keyValue = field.split("=");
                    res.put(GlobalAuthUtil.urlDecode(keyValue[0]), keyValue.length == 2 ? GlobalAuthUtil.urlDecode(keyValue[1]) : null);
                }
            }
智布道's avatar
智布道 已提交
81
        }
智布道's avatar
智布道 已提交
82
        return res;
智布道's avatar
智布道 已提交
83
    }
84

85 86 87 88 89 90 91 92 93 94 95 96 97
    public static String parseMapToString(Map<String, Object> params, boolean encode) {
        List<String> paramList = new ArrayList<>();
        params.forEach((k, v) -> {
            if (ObjectUtil.isNull(v)) {
                paramList.add(k + "=");
            } else {
                String valueString = v.toString();
                paramList.add(k + "=" + (encode ? urlEncode(valueString) : valueString));
            }
        });
        return CollUtil.join(paramList, "&");
    }

98 99 100 101 102 103 104 105 106 107 108 109 110
    public static boolean isHttpProtocol(String url) {
        if (StringUtils.isEmpty(url)) {
            return false;
        }
        return url.startsWith("http://");
    }

    public static boolean isHttpsProtocol(String url) {
        if (StringUtils.isEmpty(url)) {
            return false;
        }
        return url.startsWith("https://");
    }
111 112 113 114 115

    public static boolean isLocalHost(String url) {
        return StringUtils.isEmpty(url) || url.contains("127.0.0.1") || url.contains("localhost");
    }

智布道's avatar
智布道 已提交
116
}