下面是我的代码:
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请求。
那么如何处理这种情况呢?
发布于 2014-11-25 01:27:40
注意动作链,that is discouraged
作为规则,不推荐使用操作链接。首先探索其他选项,例如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();
}https://stackoverflow.com/questions/27109474
复制相似问题