【问题标题】:chain action in interceptor拦截器中的链式动作
【发布时间】:2014-11-24 16:27:43
【问题描述】:

这是我的代码:

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;

@SuppressWarnings("serial")
public class PostOnlyInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation ai) throws Exception {
        final ActionContext context = ai.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
        if (!request.getMethod().equals("POST")) {
            return Action.ERROR;
        }

        return ai.invoke();

    }
}

出于安全原因,我使用此拦截器来避免“GET”方法请求。但是当我使用链操作方法调用它时:request.getMethod() 返回 GET 请求。

那么如何处理这种情况呢?

【问题讨论】:

    标签: java struts2 get interceptor chaining


    【解决方案1】:

    提防动作链,that is discouraged

    一般来说,不推荐使用动作链。首先探索其他选项,例如 Redirect After Post 技术。

    但是如果您已经在使用它并且无法更改,您可以通过检查结果是否为 chain 类型来绕过拦截器内部的 POST 检查:

    public String intercept(ActionInvocation ai) throws Exception {
        final ActionContext context = ai.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) 
                                      context.get(StrutsStatics.HTTP_REQUEST);
    
        boolean isChainResult = ai.getResult() != null 
              && ActionChainResult.class.isAssignableFrom(ai.getResult().getClass());
    
        if (!request.getMethod().equals("POST") && !isChainResult) {
            return Action.ERROR;
        }
    
        return ai.invoke();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2015-11-22
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多