struts2 <allowed-methods > 标签配置
1.在struts2 2.5版本中添加了对方法访问的权限,如果没有被添加到<allow-method> 方法的标签,将会报一下错误
5:05:18.078 [http-apr-8020-exec-8] ERROR org.apache.struts2.dispatcher.Dispatcher - Could not find action or result: /core/DayFirst!login.do
com.opensymphony.xwork2.config.ConfigurationException: Method login for action DayFirst is not allowed!
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:203) ~[struts2-core-2.5.14.1.jar:?]
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:76) ~[struts2-core-2.5.14.1.jar:?]
at org.apache.struts2.rest.RestActionProxyFactory.createActionProxy(RestActionProxyFactory.java:50) ~[struts2-rest-plugin-2.5.14.1.jar:?]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564) [struts2-core-2.5.14.1.jar:?]
at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:79) [struts2-core-2.5.14.1.jar:?]
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:141) [struts2-core-2.5.14.1.jar:?]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.73]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.73]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) [catalina.jar:7.0.73]
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:122) [catalina.jar:7.0.73]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java) [catalina.jar:7.0.73]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) [catalina.jar:7.0.73]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) [catalina.jar:7.0.73]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) [catalina.jar:7.0.73]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958) [catalina.jar:7.0.73]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.73]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452) [catalina.jar:7.0.73]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087) [tomcat-coyote.jar:7.0.73]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) [tomcat-coyote.jar:7.0.73]
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517) [tomcat-coyote.jar:7.0.73]
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506) [tomcat-coyote.jar:7.0.73]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_79]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.73]
at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]
解决方法:在struts2 的配置文件中加上<allowed-methods >regex:*</allowed-methods>即可。配置文件如下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.action.extension" value="do"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="core" namespace="/core" extends="struts-default">
<action name="*.*" class="com.controller.{1}Action" method="{2}">
<result name="success" >/WEB-INF/view/success.jsp</result>
<!--这个配置是修复struts的漏洞-->
<allowed-methods >regex:*</allowed-methods>
</action>
</package>
</struts>
当然问题是解决了,看着也很简单就是正则表达式,但是struts2 的原理是什么为什么要这么做?
1.struts2 为什么要对访问的方法加以控制?查看这篇文章就了解了http://www.freebuf.com/vuls/128668.html
2.分析怎么解决这个问题的。首先在控制台查看异常,最先出现异常的类先打印,所以直接看第一行的异常DefaultActionProxy.java:203 ,找到如下地方

配置文件是在server容器启动时候就开始执行了,为什么会这样,是因为配置文件的初始化放在struts2 的strutsPrepareAndExcuteFilter 的init() 方法中。再看this.config.isAllowedMethod 这个方法是判断传入进来的方法是否在配置文件中有。

this.allowedMethods.isAllowed(method) 这个类的这个方法控制了action 方法的访问权限 查看AllowedMethod 这个类,<Allowed-method> 标签是怎么被解析的
public static AllowedMethods build(boolean strictMethodInvocation, Set<String> methods, String defaultRegex) {
Set<AllowedMethods.AllowedMethod> allowedMethods = new HashSet();
Iterator i$ = methods.iterator();
while(true) {
while(i$.hasNext()) {
String method = (String)i$.next();
boolean isPattern = false;
StringBuilder methodPattern = new StringBuilder();
int len = method.length();
for(int x = 0; x < len; ++x) {
char c = method.charAt(x);
if (x < len - 2 && c == '{' && '}' == method.charAt(x + 2)) {
methodPattern.append(defaultRegex);
isPattern = true;
x += 2;
} else {
methodPattern.append(c);
}
}
if (isPattern && !method.startsWith("regex:") && !strictMethodInvocation) {
allowedMethods.add(new AllowedMethods.PatternAllowedMethod(methodPattern.toString(), method));
} else {
String pattern;
if (method.startsWith("regex:")) {
pattern = method.substring(method.indexOf(":") + 1);
allowedMethods.add(new AllowedMethods.PatternAllowedMethod(pattern, method));
} else if (method.contains("*") && !method.startsWith("regex:") && !strictMethodInvocation) {
pattern = method.replace("*", defaultRegex);
allowedMethods.add(new AllowedMethods.PatternAllowedMethod(pattern, method));
} else if (!isPattern) {
allowedMethods.add(new AllowedMethods.LiteralAllowedMethod(method));
} else {
LOG.trace("Ignoring method name: [{}] when SMI is set to [{}]", method, strictMethodInvocation);
}
}
}
LOG.debug("Defined allowed methods: {}", allowedMethods);
return new AllowedMethods(strictMethodInvocation, allowedMethods, defaultRegex);
}
}
大致的意思就是, 将<allowed-method> 标签的内容放入allowedMethods 这个set 集合中,以便调用action 的方法时候决定这个action的方法是否可以被调用。
这里的放入规则遵循标红的判断,带有regex: 前缀的,带有* 的将被单独处理,就是将所有的方法都通过。


看上面的debug 图,发现struts2 默认带了一些有权限的方法 index input 等。
最后还有 <global-allowed-methods>regex:.*</global-allowed-methods> 标签也可以解决问题。
struts2 <allowed-methods > 标签配置的更多相关文章
- Struts2学习笔记(一):Struts2开发环境的配置
一.Struts2应用所需的jar文件. 开发struts2应用需要依赖的jar文件在解压目录下的lib文件夹里面.开发struts2程序最少需要的jar文件为:struts2-core-2.xx.j ...
- struts2 的struts.xml配置详解
在应用struts框架进行开发时,必不可少的一步就是对struts.xml进行配置,对于该文件了解越多,我们开发起一应用程序定会更加顺手.下面我们看一下struts.xml的内容,每一项都有什么作用. ...
- 解决struts2中UI标签出现的问题: The Struts dispatcher cannot be found
解决struts2中UI标签出现的问题: The Struts dispatcher cannot be found 异常信息: The Struts dispatcher cannot be fou ...
- Struts2学习笔记二 配置详解
Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...
- Struts2之数据标签(二)
Struts2之数据标签(一):http://blog.csdn.net/u012561176/article/details/46848817 1.action标签:使用此标签能够同意在JSP页面中 ...
- Struts2和SpringMVC简单配置以及区别总结
Struts2: struts 2 是一个基于MVC(mode-view-con)设计模式的Web应用框架,是由Struts1和WebWork两个经典框架发展而来的. 工作流程: 1客户端浏览器发出H ...
- Struts2框架学习(三)——配置详解
一.struts.xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...
- struts2 s:if标签以及 #,%{},%{#}的使用方法
<s:if>判断字符串的问题: 1.判断单个字符:<s:if test="#session.user.username=='c'"> 这样是从session ...
- struts2 if正确标签示例
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...
随机推荐
- iOS开发——高级篇——Runloop相关一
一.什么是runLoop 1.说白了,runloop就是运行循环 2.runloop,他是多线程的法宝 通常来讲,一个线程一次只能执行一个任务,执行完之后就退出线程.但是,对于主线程是不能退出的,因此 ...
- noteexpress使用指南
软件功能:在写论文时直接调用参考数据并输出正规的格式. (以下简称NE) A.下载安装 下载地址:Note-express - Bibliography Software 选择相应的学校进行下载,相 ...
- Okapi BM25 (BM stands for Best Matching)
Okapi BM25 - Wikipedia https://en.wikipedia.org/wiki/Okapi_BM25 In information retrieval, Okapi BM25 ...
- 临时表 数据在 内存 转移时间 将160秒的创建临时表时间放入定时任务 不到1秒的求和时间 hadoop 引入Hadoop 分布式计算
SELECT SUM(pv) as pv_t FROM 行 112247817表类型 InnoDB自动递增值 1082428327行格式 Compact索引长度 8.60 GB (9,235,93 ...
- activity.runOnUiThread()内的run()方法没有被执行
activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(context, toast, Toast.LEN ...
- 使用JSTL 对在页面上对 0,0,1 的分割处理 forTokens
使用JSTL 对在页面上对 0,0,1 的分割处理 <tr onmouseover="currentcolor=this.style.backgroundColor;this.styl ...
- Servlet启动运行顺序
1.web项目执行属性 启动web项目后,web容器首先回去找web.xml文件,读取这个文件. 容器会创建一个 ServletContext ( servlet 上下文),整个 web 项目的所有部 ...
- zoj 3865
Superbot Time Limit: 2 Seconds Memory Limit: 65536 KB Superbot is an interesting game which you ...
- STM32F4 LTDC学习
很久没有写东西了,也很久没看文档了吼吼,觉得有点无聊,找来F4看看,主要看F429.督促自己多看多记录. 首先配置同步时序先看参考手册 下面看一个实际例子,一块439的开发板 设置: LTDC_Ini ...
- Ruby Hash类
Hash类 更新:2017/06/15 获取没有的哈希值时返回nil 更新:2018/01/03 增加merge! 更新: 2018/04/05 增加搜索 key 更新: 2018/04/30 增加e ...