天天看點

關于Struts2.5的全局方法調用配置說明及相應的SMI的兩種解決方案

##關于Struts2.5的全局方法調用配置說明及相應的SMI的兩種解決方案

Struts2.5與2.3跟之前版本的差別,請參閱幫助文檔 struts-2.5.1-all\struts-2.5.1\docs\docs\action-configuration.html 官方解釋的已經很詳細了,下面貼出自己的一些廢話。。。

1、

由之前的不嚴格的Dynamic Method Invocation 更新到了 - Strict DMI

說白了,就是防止壞蛋攻擊和破解,提升這小子的安全性。

Struts2給她起了個名字,叫做SMI

Strict Method Invocation
In Struts 2.5 the Strict DMI was extended and it's called Strict Method Invocation aka SMI. You can imagine that the DMI is a "border police", where SMI is a "tax police" and keeps eye on internals. With this version, SMI is enabled by default (strict-method-invocation attribute is set to true by default in struts-default package), you have option to disable it per package - there is no global switch to disable SMI for the whole application. To gain advantage of new configuration option please use the latest DTD definition:

           

需要在相關版本的限定檔案中使用,才能進行有效的配置 自己去看幫助文檔粘貼吧~

這是官方給出的配置方案

SMI works in the following way:

<allowed-methods> / @AllowedMethods is defined per action - SMI works without switching it on but just for those actions (plus adding <global-allowed-methods/>)
SMI is enabled but no <allowed-methods> / @AllowedMethods are defined - SMI works but only with <global-allowed-methods/>
SMI is disabled - call to any action method is allowed that matches the default RegEx - ([A-Za-z0-9_$]*)
You can redefine the default RegEx by using a constant as follow <constant name="struts.strictMethodInvocation.methodRegex" value="([a-zA-Z]*)"/>
           

我測試了一下,

<constant name="struts.strictMethodInvocation.methodRegex" value="([a-zA-Z]*)"/>
           

這個配置說是可以替換

<global-allowed-methods/>

的預設值, 但是,親測,沒什麼鳥用。

<global-allowed-methods/>

也沒有什麼預設值。 後期用到再論。

2、SMI解決方案

首先要說的是, 下面這個2.3之前的配置已經沒有效果了,删了就是

<constant name="struts.enable.DynamicMethodInvocation" value="true" />
           

想要實作之前的效果,這裡提兩種解決方案

一個是 在目前的action标簽下,添加

<allowed-methods>動作類的方法名,…,…</allowed-methods>

, 放在

<action>

裡面,至于放在什麼位置,自己看DTD報錯提示吧。

這種設計在後期可以增強配置檔案可讀性,實作了指定方法的高維護性的通路設計,增強了架構的安全性。 适合實際開發使用。

一個是 在頂層的包中再添加一個配置

<global-allowed-methods>regex:([A-Za-z0-9_$]*)</global-allowed-methods>
           

至于說放在什麼位置,還是上面的廢話 —— 自己去看DTD報錯提示吧~~

這樣的話,意思就是說,除了火星文之外的任意的方法名均可正常通路。

不設限制,與之前的使用無異。

如需要轉載 請注明文章出處 **https://my.oschina.net/u/3032872/blog/1648374 ** 謝謝~

轉載于:https://my.oschina.net/u/3032872/blog/1648374