首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拦截器中的链操作

拦截器中的链操作
EN

Stack Overflow用户
提问于 2014-11-25 00:27:43
回答 1查看 314关注 0票数 1

下面是我的代码:

代码语言:javascript
复制
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请求。

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

EN

回答 1

Stack Overflow用户

发布于 2014-11-25 01:27:40

注意动作链,that is discouraged

作为规则,不推荐使用操作链接。首先探索其他选项,例如Post后重定向技术。

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

代码语言:javascript
复制
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();

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27109474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档