2012-07-14 66 views
3

如何隱藏我的Web服務器上的文件?我有一個目錄'files',其中我存儲了一些pdf文件。我不希望通過這樣的URL讓用戶訪問文件:限制用戶訪問Java EE應用程序中的文件夾

www.example.com/files/1.pdf 

的這個我要地圖與在DB的ID每個文件,並讓這樣的用戶訪問它,而不是:

www.example.com/fileId=5569a 

用戶應該無法訪問files目錄。

我該怎麼做?

回答

4

這是一個非常正向回答

首先,你應該使用部署描述符拒絕訪問的目錄(web.xml)中

<security-constraint> 
    <display-name>excluded</display-name> 
    <web-resource-collection> 
     <web-resource-name>No Access</web-resource-name> 
     <url-pattern>/files/*</url-pattern> 
    </web-resource-collection> 
    <web-resource-collection> 
     <web-resource-name>No Access</web-resource-name> 
     <url-pattern>/files/*</url-pattern> 
     <http-method>DELETE</http-method> 
     <http-method>PUT</http-method> 
     <http-method>HEAD</http-method> 
     <http-method>OPTIONS</http-method> 
     <http-method>TRACE</http-method> 
     <http-method>GET</http-method> 
     <http-method>POST</http-method> 
    </web-resource-collection> 
    <auth-constraint /> 
    <user-data-constraint> 
     <transport-guarantee>NONE</transport-guarantee> 
    </user-data-constraint> 
</security-constraint> 

現在,您的文件是安全的,你必須執行一個帶有url-mapping'/'的Servlet將檢查請求中的'fileId'參數。如果發現它,servlet會提供文件下載給用戶,否則它會將用戶重定向到主頁。

爲了實現一個servlet請參見後Call a servlet on click of hyperlink

爲了實現文件的下載,請參閱我的文章Is there a common way to download all types of files in jsp?

相關問題