通过struts2实现Http只允许POST请求
来源:互联网 发布:报java培训班后悔了 编辑:程序博客网 时间:2024/06/05 23:41
通过struts2实现Http只允许POST请求,有需要的朋友可以参考下。
前两天工作中需要做安全限制工作,今天把代码整理一下。
整体的一个思路就是使用Struts2过滤器拦截请求,反射得到对应请求反正只允许POST请求。
先看一下主要拦截器代码:
import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.renrendai.common.RequestTypeAnnotation;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts2.ServletActionContext;import org.apache.struts2.StrutsStatics;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;/** * 过滤器为request类型请求过滤,当方法中有RequestTypeAnnotation注解时,request必须要注解中对应类型一致。 */public class RequestTypeInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -4204585527913002611L; protected final Log _log = LogFactory.getLog(RequestTypeInterceptor.class); public String intercept(ActionInvocation invocation) throws Exception { Action action = (Action) invocation.getAction(); try { Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {}); Annotation[] annotations = method.getAnnotations(); String methodName = ServletActionContext.getRequest().getMethod(); for (Annotation annotation : annotations) { if (annotation instanceof RequestTypeAnnotation) { RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation; if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) { // 当前台用户请求类型不是方法注解中对应类型时提示此信息 return "input"; } } } } catch (Exception e) { _log.error(e); return "error"; } return invocation.invoke(); }}
主要是通过invocation获得调用方法、对应注解及http请求方式。
拦截器配置
<interceptor name="requestType" class="com.xxx.interceptor.RequestTypeInterceptor" />
配置到对应的action上
<action name="xxxTransfer" class="xxxTransfer"> <interceptor-ref name="requestType"/> <result name="success" type="redirectAction"> <param name="actionName">xxx</param> <param name="xxx">${xxxx}</param> </result> <result name="error"> /exceptions/network-busy/500.html </result> <result name="input"> /exceptions/network-busy/404.html </result></action>
在对应action的方法上添加注解
@RequestTypeAnnotation(RequestType.POST) public String execute() throws Exception{return SUCCESS; }
注解设计
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface RequestTypeAnnotation { RequestType value();}
enum设计
public enum RequestType { /** * post方式的http请求 */ POST, /** * get方式的http请求 */ GET}
0 0
- 通过struts2实现Http只允许POST请求
- 通过struts2实现Http只允许POST请求
- Struts2 过滤器实现只允许POST请求
- nginx只允许get/post请求
- 用Httpclient通过post方式来实现http请求
- 只允许ajax请求
- C++实现Http Post请求
- C++实现Http Post请求
- C++实现Http Post请求
- java实现http post请求
- java 实现http POST请求
- django 允许post请求
- iOS 允许HTTP请求
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- SpringMVC 限定某个业务控制方法,只允许GET或POST请求方式访问
- 【SpringMVC】限定某个业务控制方法,只允许GET或POST请求方式访问(十二)
- C#实现http协议GET、POST请求
- C#实现http协议GET、POST请求
- 如何才能让你的webview不往浏览器跳转
- jQuery学习之jQuery Ajax用法详解
- js正则表达式/replace替换变量方法
- c# wince datatable保存成csv
- android代码添加:ProgressBar
- 通过struts2实现Http只允许POST请求
- android 里面一些关于时间的组件
- SenchaCmd创建项目以及编译项目
- javascript history.go(-1) 返回刷新
- 【Linux】【Some】Linux 进程基础
- 最大流 dinic算法
- 简单设置,SVN提交日志字符数限制
- 检测网络连接是否正常的方法
- GDB调试-ubuntu