fastdfs防盗链设置

为什么80%的码农都做不了架构师?>>>   hot3.png

fastdfs防盗链设置

一、/etc/fdfs/http.conf配置

http.default_content_type = application/octet-stream

http.mime_types_filename=mime.types

#是否做token检查,缺省为false

http.anti_steal.check_token=true

#即生成token的有效时长 秒

http.anti_steal.token_ttl=900

#生成token的密钥,尽量设置得长一些

http.anti_steal.secret_key=FastDFS1234567890

# token检查失败,返回的文件内容,需指定本地文件名

http.anti_steal.token_check_fail=/etc/fdfs/conf/error.jpg

配置完后,重启一下nginx

二、在traker中上传文件

# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/test.jpg

# group1/M00/00/00/wKgB2ViEMZOAeE4rAAF1DzcVmmk051.jpg

三、java生成token

package com.commnon;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
public class ProtoCommon {

	/**
	* get token for file URL
	* @param remoteFilename the filename return by FastDFS server
	* @param ts unix timestamp, unit: second
	* @param secretKey the secret key
	* @return token string
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	*/
	 public static String getToken(String remoteFilename, long ts, String secretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException{
	 	byte[] bsFilename = remoteFilename.getBytes("UTF-8");
	 	byte[] bsKey = secretKey.getBytes("UTF-8");
	 	byte[] bsTimestamp = (new Long(ts)).toString().getBytes("UTF-8");
	 	
	 	byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length];
	 	System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length);
	 	System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length);
	 	System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length);
	 	
	 	return md5(buff);
	 }
	 
	 /**
	 * md5 function
	 * @param source the input buffer
	 * @return md5 string
	 */
	 public static String md5(byte[] source) throws NoSuchAlgorithmException{
	  	char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',  'e', 'f'};
	    java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
	    md.update(source);
	    byte tmp[] = md.digest();
	    char str[] = new char[32];
	    int k = 0;
	    for (int i = 0; i < 16; i++)
	    {
	     str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
	     str[k++] = hexDigits[tmp[i] & 0xf];
	    }
	    
	  	return new String(str);
	 }
	 
	 public static void main(String[] args) {
		 try {
			long lts = System.currentTimeMillis()/1000L;
			String remoteFilename = "M00/00/00/wKgB2ViEMZOAeE4rAAF1DzcVmmk051.jpg";
			System.out.println("lts="+lts);
			String token = ProtoCommon.getToken(remoteFilename, lts, "FastDFS1234567890");
			System.out.println("token="+token);
			System.out.println("httpurl=http://192.168.1.217/group1/"+remoteFilename+"?token="+token+"&ts="+lts);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
}

生成的访问地址如下:

lts=1485063736

token=8ecdfa84ab9c37354d6a69b7bb2f09d3

httpurl=http://192.168.1.217/group1/M00/00/00/wKgB2ViEMZOAeE4rAAF1DzcVmmk051.jpg?token=8ecdfa84ab9c37354d6a69b7bb2f09d3&ts=1485063736 

四、输入访问地址

http://192.168.1.217/group1/M00/00/00/wKgB2ViEMZOAeE4rAAF1DzcVmmk051.jpg?token=8ecdfa84ab9c37354d6a69b7bb2f09d3&ts=1485063736  

参考:http://bbs.chinaunix.net/thread-1916999-1-1.html 

fdfs_client.conf

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = yes
http.secret_key = FastDFS1234567890

tracker_server = 192.168.0.116:22122
tracker_server = 192.168.0.119:22122

 

转载于:https://my.oschina.net/chaun/blog/829138

你可能感兴趣的:(fastdfs防盗链设置)