隨筆 - 0, 文章 - 264, 評論 - 170, 引用 - 0
          數據加載中……

          nginx添加key訪問限制,并且對鏈接加超時失效策略

          實現key訪問限制,nginx通過accessKey來實現。
          具體參考:
          http://www.lamppr.com/node/620

          實現鏈接的超時失效策略,nginx通過Secure Link來實現。
          具體參考:
          http://www.xsdou.com/1734.html

          由于網上說的實現代碼都是基于PHP的,所以在這我用JAVA實現了下。
          <?php
          $secret 
          = 'password'; # 密鑰
          $path 
          = '/download/she.flv'; # 下載文件
          $ipkey
          = md5("password".$_SERVER['REMOTE_ADDR']); #加密IP
           # 下載到期時間,time是當前時間,300表示300秒,也就是說從現在到300秒之內文件不過期
           $expire 
          = time()+300;
          # 用文件路徑、密鑰、過期時間生成加密串
           $md5 
          = base64_encode(md5($secret . $path . $expire, true));
           $md5 
          = strtr($md5, '+/''-_');
           $md5 
          = str_replace('=''', $md5);
          # 加密后的下載地址
           echo 
          '<a href=http://s1.xsdou.com/download/she.flv?key='.$ipkey.'&st='.$md5.'&e='.$expire.'>she.flv</a>';
           echo 
          '<br>http://s1.xsdou.com/download/she.flv?key='.$ipkey.'&st='.$md5.'&e='.$expire;
           
          ?>


          public static void main(String[] args) {
                  String secret 
          = "password";
                  String path 
          = "/mp4files/1162000000062C6B/221.194.64.19/2/45a594b6938698f63b5771c12ff5b912.mp4";
                  String expire 
          = String.valueOf(System.currentTimeMillis() / 1000 + 600);
                  String remoteIp 
          = "192.168.1.145";
                  
                  String ipkey 
          = DigestUtils.md5Hex(secret + remoteIp);
                  String st 
          = new sun.misc.BASE64Encoder().encode(DigestUtils.md5(secret + path + expire)).replaceAll("\\+/""-_").replaceAll("=""");        
                  
                  System.out.println(
          "ipkey: " + ipkey);
                  System.out.println(
          "st: " + st);
                  System.out.println(
          "e: " + expire);
              }

          posted on 2013-12-24 10:20 小一敗涂地 閱讀(789) 評論(0)  編輯  收藏 所屬分類: nginx、squid、haproxy、varnish等

          主站蜘蛛池模板: 南平市| 麦盖提县| 灵山县| 鄂温| 沙洋县| 商河县| 呼伦贝尔市| 乐山市| 台东市| 丹凤县| 长兴县| 抚宁县| 阿拉善右旗| 永登县| 会理县| 六枝特区| 德令哈市| 古丈县| 西华县| 樟树市| 江津市| 芮城县| 民乐县| 同心县| 庐江县| 奎屯市| 宁远县| 临江市| 绥江县| 霍山县| 云安县| 奈曼旗| 大冶市| 东城区| 广德县| 北流市| 古丈县| 南木林县| 新沂市| 上饶县| 博白县|