我在我的webapp中使用Spring Web Flow,我想知道是否有一种通用的方法来防止直接访问某些子流。这些子流程只能从某些流程访问,而不是直接通过url访问,所以在这些子流程中,我想检查“我是否从流程中调用”。Spring Webflow - 防止子流被直接访问
有没有什么机制可以实现它?我正在考虑春季安全,但我找不到任何有用的功能来进行这种限制。
谢谢你!
我在我的webapp中使用Spring Web Flow,我想知道是否有一种通用的方法来防止直接访问某些子流。这些子流程只能从某些流程访问,而不是直接通过url访问,所以在这些子流程中,我想检查“我是否从流程中调用”。Spring Webflow - 防止子流被直接访问
有没有什么机制可以实现它?我正在考虑春季安全,但我找不到任何有用的功能来进行这种限制。
谢谢你!
您可以使用输入映射器属性来检查它,如下所示。假设你有一个调用“子流程”的父流程。您需要将包含作为父流的名称的输入值:
<?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";
}
}
...
}
希望这有助于。
我也希望有某种内置的简单机制。不过,我只是做了一个与我分享的略有不同的方式。也就是说,因为我的子流程都知道自己的名字,我只是在流程配置检查第一状态的流ID(名称)是否不是硬编码名称匹配:
<decision-state id="subOnly">
<if test="flowExecutionContext.definition.id == 'thisSubflowFlowName'" then="invalidAccess" else="mySubflowFirstState"/>
</decision-state>
其中“invalidAccess”是end-state
像之前的答案所示。
好的,这有帮助!然而,我期待一个更通用的解决方案,而不涉及额外的代码。但我认为没有这样的解决方案!谢谢! – sebarocker