GlobalAuthUtil.java 3.5 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4 5 6 7 8
package me.zhyd.oauth.utils;

import cn.hutool.core.codec.Base64;
import me.zhyd.oauth.exception.AuthException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
智布道's avatar
智布道 已提交
9
import java.net.URLDecoder;
智布道's avatar
智布道 已提交
10 11 12 13
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
智布道's avatar
智布道 已提交
14 15
import java.util.HashMap;
import java.util.Map;
智布道's avatar
智布道 已提交
16

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

28
    public static String generateDingTalkSignature(String secretKey, String timestamp) {
智布道's avatar
智布道 已提交
29
        try {
30
            byte[] signData = sign(secretKey.getBytes(DEFAULT_ENCODING), timestamp.getBytes(DEFAULT_ENCODING));
智布道's avatar
智布道 已提交
31
            return urlEncode(new String(Base64.encode(signData, false)));
智布道's avatar
智布道 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        } catch (UnsupportedEncodingException ex) {
            throw new AuthException("Unsupported algorithm: " + DEFAULT_ENCODING, ex);
        }
    }

    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
智布道 已提交
49
    private static String urlEncode(String value) {
智布道's avatar
智布道 已提交
50 51 52 53 54
        if (value == null) {
            return "";
        }

        try {
智布道's avatar
智布道 已提交
55
            String encoded = URLEncoder.encode(value, GlobalAuthUtil.DEFAULT_ENCODING);
智布道's avatar
智布道 已提交
56 57 58
            return encoded.replace("+", "%20").replace("*", "%2A")
                    .replace("~", "%7E").replace("/", "%2F");
        } catch (UnsupportedEncodingException e) {
智布道's avatar
智布道 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            throw new AuthException("Failed To Encode Uri", e);
        }
    }

    public static String urlDecode(String value) {
        if (value == null) {
            return "";
        }
        try {
            return URLDecoder.decode(value, GlobalAuthUtil.DEFAULT_ENCODING);
        } 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
智布道 已提交
84
        }
智布道's avatar
智布道 已提交
85
        return res;
智布道's avatar
智布道 已提交
86
    }
87 88 89 90 91 92 93 94 95 96 97 98 99 100

    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://");
    }
101 102 103 104 105

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

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