2013-12-16 92 views
3

我正在開發購物網站應用程序。在我的應用程序中,我需要阻止直接訪問我的操作類的URL。舉個例子,我有一個名爲OrderAdd的動作。在這個動作類中,我從請求範圍獲取一些數據。如果有人直接訪問此操作類,則會顯示空指針異常。我如何在我的應用程序中防止這種情況。有沒有任何可能的辦法。我使用jsp來表達我的觀點。我的框架是struts2。操作是java。如何防止對操作類的直接URL訪問

在此先感謝!

+0

如果你不能阻止你自己處理NPE,異常攔截器可以爲你做。 –

+0

這是[XY問題]的示例(http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。你真正需要做的是防止顯示甚至拋出異常(或者只是顯示堆棧跟蹤)。您不需要「阻止直接訪問操作類的URL」,這沒有任何意義。 –

回答

0

j2ee內置了安全約束。您可以配置URL模式web.xml中,而您不希望允許直接訪問..

這裏閱讀 http://java.dzone.com/articles/understanding-web-security

Source :

使用案例:我們想排除一組Web來自任何訪問的資源。當Web應用程序的某個部分需要進行某種形式的維護或不適用於通用Web應用程序的特定物理部署時,可能會出現這種情況。我們將使用不指定角色的授權約束來實現此目的。

<security-constraint> 
    <display-name>excluded</display-name> 
    <web-resource-collection> 
     <web-resource-name>No Access</web-resource-name> 
     <url-pattern>/excluded/*</url-pattern> 
     <url-pattern>/restricted/employee/excluded/*</url-pattern> 
     <url-pattern>/restricted/partners/excluded/*</url-pattern> 
    </web-resource-collection> 
    <web-resource-collection> 
     <web-resource-name>No Access</web-resource-name> 
     <url-pattern>/restricted/*</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> 
+0

謝謝。這可能是有用的。 – begginerAll

+0

@ user3081492我已更新鏈接和答案..這可能會更有用。祝你好運 –

+0

好吧。謝謝! – begginerAll

0

我覺得這個問題是基於一種誤解。 Web應用程序的本質是用戶可以使用任何請求方法訪問任何URL。您需要允許用戶對有效的URL進行有效請求,因爲這是正常的,正確的應用程序工作流程所需的。如果您不希望用戶在嘗試訪問網址而未提供所需參數時看到您的NullPointerException,則應該使用try..catch來捕捉它們 - 或者更好的是,避免導致NullPointerException使用if或其他來測試缺失的參數。