2014-05-05 13 views
2

我在我的webapp中使用Spring Web Flow,我想知道是否有一種通用的方法來防止直接訪問某些子流。這些子流程只能從某些流程訪問,而不是直接通過url訪問,所以在這些子流程中,我想檢查「我是否從流程中調用」。Spring Webflow - 防止子流被直接訪問

有沒有什麼機制可以實現它?我正在考慮春季安全,但我找不到任何有用的功能來進行這種限制。

謝謝你!

回答

0

您可以使用輸入映射器屬性來檢查它,如下所示。假設你有一個調用「子流程」的父流程。您需要將包含作爲父流的名稱的輸入值:

<?xml version="1.0" encoding="UTF-8"?> 
    <flow xmlns="http://www.springframework.org/schema/webflow" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/webflow 
      http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"> 
     ... 
     <subflow-state id="subflow-flow" subflow="subflow-flow"> 
      <!--flowInitiatedBy should be different for each different parent flow--> 
      <input name="flowInitiatedBy" value="'parentFlowName'"/> 
      <transition on="invalidAccess" to="someViewWithMessage"/> 
      <transition on="processedSubflow" to="someOtherState"/> 
     </subflow-state> 
     ... 
    </flow> 

然後在子流可以檢索parentflowname和執行yourAction類的檢查。您可以定義爲子流:

<?xml version="1.0" encoding="UTF-8"?> 
    <flow xmlns="http://www.springframework.org/schema/webflow" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/webflow 
      http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd" 
      start-state="start">    
     <input name="flowInitiatedBy" type="java.lang.String"/> 
     <action-state id="start"> 
      <evaluate expression="yourAction.checkAccessibility(flowRequestContext)"/> 
      <transition on="invalidAccess" to="verifyWhetherDraftsExists"/> 
      <transition on="continue" to="continueToState"/> 
     </action-state> 
     <!--define other states for continue transition --> 
     ... 
     <end-state id="invalidAccess"/> 
     <end-state id="processedSubflow"/> 
    </flow> 

在你的Action類:

public class YourAction{ 
    ... 
    public String checkAccessibility(RequestContext context){ 
     String flowInitiatedBy = context.getFlowScope().get("flowInitiatedBy"); 
     //flowInitiatedBy will be empty if initiated by url. 
     if(flowInitiatedBy is empty){ 
      return "invalidAccess"; 
     }else{ 
      // dosomething 
      return "continue"; 
     } 
    } 
    ... 
    } 

希望這有助於。

+0

好的,這有幫助!然而,我期待一個更通用的解決方案,而不涉及額外的代碼。但我認爲沒有這樣的解決方案!謝謝! – sebarocker

0

我也希望有某種內置的簡單機制。不過,我只是做了一個與我分享的略有不同的方式。也就是說,因爲我的子流程都知道自己的名字,我只是在流程配置檢查第一狀態的流ID(名稱)是否不是硬編碼名稱匹配:

<decision-state id="subOnly"> 
    <if test="flowExecutionContext.definition.id == 'thisSubflowFlowName'" then="invalidAccess" else="mySubflowFirstState"/> 
</decision-state> 

其中「invalidAccess」是end-state像之前的答案所示。