GlobalAuthUtil.java 4.2 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;
H
Hongwei Peng 已提交
6
import cn.hutool.http.HttpUtil;
智布道's avatar
智布道 已提交
7 8 9 10 11
import me.zhyd.oauth.exception.AuthException;

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

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

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

    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);
        }
    }

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

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

    public static String urlDecode(String value) {
        if (value == null) {
            return "";
        }
        try {
66
            return URLDecoder.decode(value, GlobalAuthUtil.DEFAULT_ENCODING.displayName());
智布道's avatar
智布道 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        } 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
智布道 已提交
82
        }
智布道's avatar
智布道 已提交
83
        return res;
智布道's avatar
智布道 已提交
84
    }
85

86

87 88 89 90 91 92 93 94 95 96 97 98
    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, "&");
    }
99
  
H
Hongwei Peng 已提交
100 101 102 103 104
    public static Map<String, Object> parseQueryToMap(String url) {
        Map<String, Object> paramMap = new HashMap<>();
        HttpUtil.decodeParamMap(url, "UTF-8").forEach(paramMap::put);
        return paramMap;
    }
105

106 107 108 109 110 111 112 113 114 115 116 117 118
    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://");
    }
119 120 121 122 123

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

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