簽名驗證使用示例(MD5)

1.定義常量:

# 鑑權開關
signature.enable=false
signature.secretkey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
2.引用常量:

private
@Autowired
AccessConfigProperties accessConfigProperties;

@Value("${signature.enable}")
private boolean signatureEnable;
3.簽名驗證:

if (signatureEnable) {
    String appid = request.getHeader("appid");
    String signature = request.getHeader("signature");
    String timestamp = request.getHeader("timestamp");
    String secretKey = accessConfigProperties.getSecretKey(appid);
    if (secretKey == null) { // 接入產品非法
        throw new SignatureException(ErrorCode.ERR_SIGNATURE_FAIL);
    }
    if (StringUtils.isEmpty(signature) || StringUtils.isEmpty(timestamp) || StringUtils.isEmpty(appid)) {
        throw new SignatureException(ErrorCode.ERR_SIGNATURE_FAIL, "參數缺失");
    }
    String key = timestamp + uid + appid + secretKey;
    String result = DigestUtils.md5DigestAsHex(key.getBytes());
    if (result.equals(signature)) {
        filterChain.doFilter(request, response);
        return;
    } else {
        throw new SignatureException(ErrorCode.ERR_SIGNATURE_FAIL);
    }
}
附:DigestUtils工具類

DigestUtils是一個算法工具類,在package org.apache.commons.codec.digest;這個包下。

該類中常用的方法有:

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest  
  7.      */  
  8.     public static byte[] md5(byte[] data) {  
  9.         return getMd5Digest().digest(data);  
  10.     }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest  
  7.      * @throws IOException  
  8.      *             On error reading from the stream  
  9.      * @since 1.4  
  10.      */  
  11.     public static byte[] md5(InputStream data) throws IOException {  
  12.         return digest(getMd5Digest(), data);  
  13.     }  


[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest  
  7.      */  
  8.     public static byte[] md5(String data) {  
  9.         return md5(getBytesUtf8(data));  
  10.     }  
[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.     * Calculates the MD5 digest and returns the value as a 32 character hex string.  
  3.     *   
  4.     * @param data  
  5.     *            Data to digest  
  6.     * @return MD5 digest as a hex string  
  7.     */  
  8.    public static String md5Hex(byte[] data) {  
  9.        return Hex.encodeHexString(md5(data));  
  10.    }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 32 character hex string.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest as a hex string  
  7.      * @throws IOException  
  8.      *             On error reading from the stream  
  9.      * @since 1.4  
  10.      */  
  11.     public static String md5Hex(InputStream data) throws IOException {  
  12.         return Hex.encodeHexString(md5(data));  
  13.     }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.     * Calculates the MD5 digest and returns the value as a 32 character hex string.  
  3.     *   
  4.     * @param data  
  5.     *            Data to digest  
  6.     * @return MD5 digest as a hex string  
  7.     */  
  8.    public static String md5Hex(String data) {  
  9.        return Hex.encodeHexString(md5(data));  
  10.    }  


[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.    * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.  
  3.    *   
  4.    * @param data  
  5.    *            Data to digest  
  6.    * @return SHA-1 digest  
  7.    */  
  8.   public static byte[] sha(byte[] data) {  
  9.       return getShaDigest().digest(data);  
  10.   }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return SHA-1 digest  
  7.      * @throws IOException  
  8.      *             On error reading from the stream  
  9.      * @since 1.4  
  10.      */  
  11.     public static byte[] sha(InputStream data) throws IOException {  
  12.         return digest(getShaDigest(), data);  
  13.     }  
[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return SHA-1 digest  
  7.      */  
  8.     public static byte[] sha(String data) {  
  9.         return sha(getBytesUtf8(data));  
  10.     }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.  
  3.      * <p>  
  4.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  5.      * </p>  
  6.      *   
  7.      * @param data  
  8.      *            Data to digest  
  9.      * @return SHA-256 digest  
  10.      * @since 1.4  
  11.      */  
  12.     public static byte[] sha256(byte[] data) {  
  13.         return getSha256Digest().digest(data);  
  14.     }  
  15.   
  16.     /**  
  17.      * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.  
  18.      * <p>  
  19.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  20.      * </p>  
  21.      *   
  22.      * @param data  
  23.      *            Data to digest  
  24.      * @return SHA-256 digest  
  25.      * @throws IOException  
  26.      *             On error reading from the stream  
  27.      * @since 1.4  
  28.      */  
  29.     public static byte[] sha256(InputStream data) throws IOException {  
  30.         return digest(getSha256Digest(), data);  
  31.     }  
  32.   
  33.     /**  
  34.      * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.  
  35.      * <p>  
  36.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  37.      * </p>  
  38.      *   
  39.      * @param data  
  40.      *            Data to digest  
  41.      * @return SHA-256 digest  
  42.      * @since 1.4  
  43.      */  
  44.     public static byte[] sha256(String data) {  
  45.         return sha256(getBytesUtf8(data));  
  46.     }  
  47.   
  48.     /**  
  49.      * Calculates the SHA-256 digest and returns the value as a hex string.  
  50.      * <p>  
  51.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  52.      * </p>  
  53.      *   
  54.      * @param data  
  55.      *            Data to digest  
  56.      * @return SHA-256 digest as a hex string  
  57.      * @since 1.4  
  58.      */  
  59.     public static String sha256Hex(byte[] data) {  
  60.         return Hex.encodeHexString(sha256(data));  
  61.     }  
  62.   
  63.     /**  
  64.      * Calculates the SHA-256 digest and returns the value as a hex string.  
  65.      * <p>  
  66.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  67.      * </p>  
  68.      *   
  69.      * @param data  
  70.      *            Data to digest  
  71.      * @return SHA-256 digest as a hex string  
  72.      * @throws IOException  
  73.      *             On error reading from the stream  
  74.      * @since 1.4  
  75.      */  
  76.     public static String sha256Hex(InputStream data) throws IOException {  
  77.         return Hex.encodeHexString(sha256(data));  
  78.     }  
  79.   
  80.     /**  
  81.      * Calculates the SHA-256 digest and returns the value as a hex string.  
  82.      * <p>  
  83.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  84.      * </p>  
  85.      *   
  86.      * @param data  
  87.      *            Data to digest  
  88.      * @return SHA-256 digest as a hex string  
  89.      * @since 1.4  
  90.      */  
  91.     public static String sha256Hex(String data) {  
  92.         return Hex.encodeHexString(sha256(data));  
  93.     }  
[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.  
  3.      * <p>  
  4.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  5.      * </p>  
  6.      *   
  7.      * @param data  
  8.      *            Data to digest  
  9.      * @return SHA-384 digest  
  10.      * @since 1.4  
  11.      */  
  12.     public static byte[] sha384(byte[] data) {  
  13.         return getSha384Digest().digest(data);  
  14.     }  
  15.   
  16.     /**  
  17.      * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.  
  18.      * <p>  
  19.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  20.      * </p>  
  21.      *   
  22.      * @param data  
  23.      *            Data to digest  
  24.      * @return SHA-384 digest  
  25.      * @throws IOException  
  26.      *             On error reading from the stream  
  27.      * @since 1.4  
  28.      */  
  29.     public static byte[] sha384(InputStream data) throws IOException {  
  30.         return digest(getSha384Digest(), data);  
  31.     }  
  32.   
  33.     /**  
  34.      * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.  
  35.      * <p>  
  36.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  37.      * </p>  
  38.      *   
  39.      * @param data  
  40.      *            Data to digest  
  41.      * @return SHA-384 digest  
  42.      * @since 1.4  
  43.      */  
  44.     public static byte[] sha384(String data) {  
  45.         return sha384(getBytesUtf8(data));  
  46.     }  
  47.   
  48.     /**  
  49.      * Calculates the SHA-384 digest and returns the value as a hex string.  
  50.      * <p>  
  51.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  52.      * </p>  
  53.      *   
  54.      * @param data  
  55.      *            Data to digest  
  56.      * @return SHA-384 digest as a hex string  
  57.      * @since 1.4  
  58.      */  
  59.     public static String sha384Hex(byte[] data) {  
  60.         return Hex.encodeHexString(sha384(data));  
  61.     }  
  62.   
  63.     /**  
  64.      * Calculates the SHA-384 digest and returns the value as a hex string.  
  65.      * <p>  
  66.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  67.      * </p>  
  68.      *   
  69.      * @param data  
  70.      *            Data to digest  
  71.      * @return SHA-384 digest as a hex string  
  72.      * @throws IOException  
  73.      *             On error reading from the stream  
  74.      * @since 1.4  
  75.      */  
  76.     public static String sha384Hex(InputStream data) throws IOException {  
  77.         return Hex.encodeHexString(sha384(data));  
  78.     }  
  79.   
  80.     /**  
  81.      * Calculates the SHA-384 digest and returns the value as a hex string.  
  82.      * <p>  
  83.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  84.      * </p>  
  85.      *   
  86.      * @param data  
  87.      *            Data to digest  
  88.      * @return SHA-384 digest as a hex string  
  89.      * @since 1.4  
  90.      */  
  91.     public static String sha384Hex(String data) {  
  92.         return Hex.encodeHexString(sha384(data));  
  93.     }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.      * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.  
  3.      * <p>  
  4.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  5.      * </p>  
  6.      *   
  7.      * @param data  
  8.      *            Data to digest  
  9.      * @return SHA-512 digest  
  10.      * @since 1.4  
  11.      */  
  12.     public static byte[] sha512(byte[] data) {  
  13.         return getSha512Digest().digest(data);  
  14.     }  
  15.   
  16.     /**  
  17.      * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.  
  18.      * <p>  
  19.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  20.      * </p>  
  21.      *   
  22.      * @param data  
  23.      *            Data to digest  
  24.      * @return SHA-512 digest  
  25.      * @throws IOException  
  26.      *             On error reading from the stream  
  27.      * @since 1.4  
  28.      */  
  29.     public static byte[] sha512(InputStream data) throws IOException {  
  30.         return digest(getSha512Digest(), data);  
  31.     }  
  32.   
  33.     /**  
  34.      * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.  
  35.      * <p>  
  36.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  37.      * </p>  
  38.      *   
  39.      * @param data  
  40.      *            Data to digest  
  41.      * @return SHA-512 digest  
  42.      * @since 1.4  
  43.      */  
  44.     public static byte[] sha512(String data) {  
  45.         return sha512(getBytesUtf8(data));  
  46.     }  
  47.   
  48.     /**  
  49.      * Calculates the SHA-512 digest and returns the value as a hex string.  
  50.      * <p>  
  51.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  52.      * </p>  
  53.      *   
  54.      * @param data  
  55.      *            Data to digest  
  56.      * @return SHA-512 digest as a hex string  
  57.      * @since 1.4  
  58.      */  
  59.     public static String sha512Hex(byte[] data) {  
  60.         return Hex.encodeHexString(sha512(data));  
  61.     }  
  62.   
  63.     /**  
  64.      * Calculates the SHA-512 digest and returns the value as a hex string.  
  65.      * <p>  
  66.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  67.      * </p>  
  68.      *   
  69.      * @param data  
  70.      *            Data to digest  
  71.      * @return SHA-512 digest as a hex string  
  72.      * @throws IOException  
  73.      *             On error reading from the stream  
  74.      * @since 1.4  
  75.      */  
  76.     public static String sha512Hex(InputStream data) throws IOException {  
  77.         return Hex.encodeHexString(sha512(data));  
  78.     }  
  79.   
  80.     /**  
  81.      * Calculates the SHA-512 digest and returns the value as a hex string.  
  82.      * <p>  
  83.      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.  
  84.      * </p>  
  85.      *   
  86.      * @param data  
  87.      *            Data to digest  
  88.      * @return SHA-512 digest as a hex string  
  89.      * @since 1.4  
  90.      */  
  91.     public static String sha512Hex(String data) {  
  92.         return Hex.encodeHexString(sha512(data));  
  93.     }  
  94.   
  95.     /**  
  96.      * Calculates the SHA-1 digest and returns the value as a hex string.  
  97.      *   
  98.      * @param data  
  99.      *            Data to digest  
  100.      * @return SHA-1 digest as a hex string  
  101.      */  
  102.     public static String shaHex(byte[] data) {  
  103.         return Hex.encodeHexString(sha(data));  
  104.     }  

[plain] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. /**  
  2.     * Calculates the SHA-1 digest and returns the value as a hex string.  
  3.     *   
  4.     * @param data  
  5.     *            Data to digest  
  6.     * @return SHA-1 digest as a hex string  
  7.     * @throws IOException  
  8.     *             On error reading from the stream  
  9.     * @since 1.4  
  10.     */  
  11.    public static String shaHex(InputStream data) throws IOException {  
  12.        return Hex.encodeHexString(sha(data));  
  13.    }  
  14.   
  15.    /**  
  16.     * Calculates the SHA-1 digest and returns the value as a hex string.  
  17.     *   
  18.     * @param data  
  19.     *            Data to digest  
  20.     * @return SHA-1 digest as a hex string  
  21.     */  
  22.    public static String shaHex(String data) {  
  23.        return Hex.encodeHexString(sha(data));  
  24.    }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章