简体   繁体   English

为某些操作添加自定义拦截器

[英]Adding a custom interceptor for some actions

I have created an interceptor which I want to map to some particular methods in some action and I don't want to disturb the existing code:我创建了一个拦截器,我想在某些操作中将其映射到某些特定方法,并且我不想打扰现有代码:

struts.xml : struts.xml

<package name="francis" namespace="/francis" extends="struts-default,json-default">
    <interceptors>             
        <interceptor name="authentication" class="main.java.com.commdel.commons.struts2.interceptors.AuthenticationInterceptor"/>            
        <interceptor name="authorization" class="main.java.com.commdel.commons.struts2.interceptors.SecurityInterceptor"/>

The custom interceptor csrf :自定义拦截器csrf

        <interceptor name="csrf" class="main.java.com.commdel.commons.struts2.interceptors.CsrfTokenInterceptor"/>           
        
        <interceptor-stack name="commonStack">
            <interceptor-ref name="authentication"/>
            <interceptor-ref name="authorization"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
     </interceptors> 
     <default-interceptor-ref name="commonStack"/>
    <global-results>
          <result name="error" type="velocity">/results/velocity/common/globalerror.vm</result>  
          <result name="AUTHORIZATIONERROR" type="velocity">/results/velocity/common/sessionError.vm</result>                        
      </global-results>

Mapping csrf to some action:csrf映射到某些操作:

    <action name="addUpdateClaimHeadMetronic" class="main.java.com.commdel.francis.struts2.actions.ClaimHeadAction" method="addUpdateClaimHead">
        <interceptor-ref name="csrf"/>
    </action>
</package> 

You seem to think that the default Interceptor Stack applies to all actions, and that the eventual interceptor you're specifying for to the single actions are added to the default stack.您似乎认为默认拦截器堆栈适用于所有操作,并且您为单个操作指定的最终拦截器会添加到默认堆栈中。 They're not.他们不是。

The default stack is the stack used for all the actions that does not specify any stack (or single interceptor, like in your case).默认堆栈是用于所有未指定任何堆栈(或单个拦截器,如您的情况)的操作的堆栈。

If you want to add an interceptor, then specify also the stack it should add to,如果你想添加一个拦截器,那么还要指定它应该添加到的堆栈,

<action name="addUpdateClaimHeadMetronic" 
       class="main.java.com.commdel.francis.struts2.actions.ClaimHeadAction" 
      method="addUpdateClaimHead">

    <interceptor-ref name="commonStack"/>
    <interceptor-ref name="csrf"/>
</action>

or define a new stack and use it:或定义一个新堆栈并使用它:

<interceptor-stack name="commonStack">
    <interceptor-ref name="authentication"/>
    <interceptor-ref name="authorization"/>
    <interceptor-ref name="defaultStack"/>
</interceptor-stack>

<interceptor-stack name="csrfStack">
    <interceptor-ref name="authentication"/>
    <interceptor-ref name="authorization"/>
    <interceptor-ref name="defaultStack"/>
    <interceptor-ref name="csrf"/>
</interceptor-stack>

....

<default-interceptor-ref name="commonStack"/>

....

<action name="addUpdateClaimHeadMetronic" 
       class="main.java.com.commdel.francis.struts2.actions.ClaimHeadAction" 
      method="addUpdateClaimHead">

    <interceptor-ref name="csrfStack"/>
</action>

Note that the first approach violates DRY if you have a lot of actions sharing the secondary interceptor stack configuration, and it can be used only if your Interceptor is good to be placed as first or as last.请注意,如果您有许多共享辅助拦截器堆栈配置的操作,则第一种方法违反 DRY,并且仅当您的拦截器适合作为第一个或最后一个放置时才可以使用它。 If you need it in the middle (eg. after param , but before workflow ) you have to define a stack by exploding the defaultStack, copying it from struts-default.xml .如果您在中间需要它(例如,在param之后,但在workflow之前),您必须通过分解 defaultStack 来定义堆栈,从struts-default.xml复制它。

If you add a custom interceptor to the action configuration like如果您将自定义拦截器添加到操作配置中,例如

<action name="addUpdateClaimHeadMetronic" class="main.java.com.commdel.francis.struts2.actions.ClaimHeadAction" method="addUpdateClaimHead">
    <interceptor-ref name="csrf"/>
</action>

then only csrf interceptor will be configured to this action mapping.那么只有csrf拦截器会被配置到这个动作映射。 Other interceptor's included in the commonStack will be omitted even if you use即使您使用, commonStack包含的其他拦截器也将被省略

<default-interceptor-ref name="commonStack"/>

Struts allows to override the action configuration which will not use the default-interceptor-ref , it you add a custom interceptor to the action mapping. Struts 允许覆盖不会使用default-interceptor-ref的动作配置,如果您将自定义拦截器添加到动作映射。 By overriding the action configuration you can use a custom set of interceptors.通过覆盖操作配置,您可以使用一组自定义的拦截器。

You might be missing some important interceptors from the defaultStack that drive the Struts2 framework if you forget to include all references of interceptors in the custom action mapping.如果您忘记在自定义操作映射中包含拦截器的所有引用,那么您可能会错过驱动 Struts2 框架的defaultStack中的一些重要拦截器。

Note: interceptors in Struts2 add additional functionality to the action before or after the action is executed.注意: Struts2 中的拦截器在动作执行之前或之后为动作添加了额外的功能。 But missing some important interceptors might break the action execution.但是缺少一些重要的拦截器可能会破坏动作执行。


If you want to add a custom interceptor reference to the existed action, then you should add, along with your custom interceptor, a reference to the default stack in your action mapping before or after depends on your code.如果您想添加对现有操作的自定义拦截器引用,那么您应该与自定义拦截器一起添加对操作映射之前或之后的默认堆栈的引用,具体取决于您的代码。

<action name="addUpdateClaimHeadMetronic" class="main.java.com.commdel.francis.struts2.actions.ClaimHeadAction" method="addUpdateClaimHead">
    <interceptor-ref name="csrf"/>
    <interceptor-ref name="commonStack"/>
</action> 

Note: interceptors are invoked by the order they are used in the xml configuration.注意:拦截器是按照它们在 xml 配置中使用的顺序调用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM