2011-05-19 68 views
0

我在struts.xml中阻止用戶直接訪問Struts2操作?

<action name="Search" method="prepareLookUpvalues" class="com.mycompany.actions.FrSearchAction"> 
     <result name="success" type="tiles">search.layout</result> 
    </action> 

    <action name="List" class="com.mycompany.actions.FrSearchAction"> 
     <result name="success" type="tiles">results.layout</result> 
     <result name="input" type="tiles">search.layout</result> 
    </action> 

    <action name="SearchDetails" class="com.mycompany.actions.FrSearchDetailsAction"> 
     <result name="success" type="tiles">details.layout</result> 
    </action> 

    <action name="Logoff" class="com.mycompany.actions.LogoffAction" > 
     <result name="success" type="tiles">logoff.layout</result> 
    </action> 

定義以下操作假設一個用戶直接進入我的頁面主頁http://localhost:8080/fr/Search.action一切工作正常,但它已被發現的帽子有些用戶直接訪問http://localhost:8080/fr/List.action沒有首先去到導致問題的搜索頁面。

當用戶轉到搜索頁面並輸入條件並提交時,只有通過struts表單的action屬性調用「List」操作。我基本上想阻止用戶直接訪問「List」,「SearchDetails」和「Logoff」動作,除非這些動作是從我的JSP或代碼中調用的。

我是新來維護/開發Struts2應用程序,我還沒有找到明確的答案。任何建議將不勝感激!

回答

2

有一些細節丟失,所以答案會有點含糊,但是列表操作可能會將表單提交值從搜索中提取出來?或從會話拉狀態?要麼...?

無論如何,無論如何,可能存儲,只需檢查,然後重定向用戶搜索如果狀態未設置爲預期。

有關在struts2中執行重定向的詳細信息,請參閱http://www.roseindia.net/struts/struts2/actions/struts-2-redirect-action.shtml

0

這不是一個優雅的解決方案,但您可以嘗試檢查referer以查看誰稱爲該操作。你的行動課將需要實施ServletRequestAware

String referrer = request.getHeader("referer"); 
if (referrer.equals("http://localhost:8080/fr/Search.action")) { 
// do the action 
} else { 
// handle unwanted access 
} 

請記住,引用者是客戶端控制的值,可以被欺騙或刪除。