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 ...
随机推荐
- MVC+ZTree大数据异步树加载
实例部分: 首先是为ZTree提供的数据规范,定义一个标准的接口,这样对于前台调用是清楚的,简单的,因为它返回的JSON数据将与ZTree默认的数据元素保持一致 /// <summary> ...
- Num 36 : ZOJ 2100 [ 深度优先搜索算法 ] [ 回溯 ]
该题是用回溯法来解决的题: 题目: Seeding Time Limit: 2 Seconds Memory Limit: 65536 KB It is spring time and fa ...
- redis缓存数据库的详解
1,什么是redis? Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以 ...
- 理解static关键字
1.static 变量是类变量,通过类名引用. 2.static 方法不需要针对某个对象进行操作,其运行结果仅仅与输入的参数有关,调用时直接类名引用. 3.static 方法不能对非静态成员进行访问. ...
- Mac Mysql [ERR] 2006 - MySQL server has gone away
Mac mysql 安装后,导入sql数据,出现这个错误: 处理方式,是因为sql文件太大,需要修改mysql的配置.如果没有my.cnf就自己建一个. cd /etc sudo vim my.cnf ...
- HDU1054 Strategic Game —— 最小点覆盖 or 树形DP
题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others) ...
- YTU 2597: 编程题B-选拔飞行员
2597: 编程题B-选拔飞行员 时间限制: 1 Sec 内存限制: 128 MB 提交: 131 解决: 35 题目描述 2100年空军选拔高中生飞行学员基本条件要求如下,年龄范围:16-19周 ...
- linux永久或临时修改dns
1.临时修改网卡DNS地址 sudo vim /etc/resolv.conf 改为如下内容: nameserver 8.8.8.8 #修改成你的主DNS nameserver 8.8.4.4 #修改 ...
- html5--7-33 阶段练习5
html5--7-33 阶段练习5 总结: 1.JS中可以递归函数 2.js中数组对象array的使用 学习要点 综合运用学过的知识完成三个综合小练习,巩固学过的知识. 阶段小练习5-1:使用递归算法 ...
- Java中去除字符串中的所有空格
JAVA中去掉空格 1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); ...