Prevent getText() evaluating EL expressions

血红的双手。 提交于 2020-01-14 01:36:23

问题


In Struts2 backend, I have an action class instance variable, eg: keyName. A dynamic key returned to view(JSP).

This keyName variable is set using a request parameter using POST method. Depending on the request parameter value, the keyName will vary.

In JSP, I am using <s:property value="getText(keyName)" /> to show the label corresponding to the key given by keyName variable.

When I send an EL expression for example ${90-40} to keyName this expression is being evaluated and resulting in showing 50 on the UI.

How can we avoid or prevent such EL injection with getText()?

Is there any other alternative way instead of <s:property value="getText(keyName)" />?


回答1:


You could create your own text provider and register it in struts.xml:

<constant name="struts.xworkTextProvider" value="com.struts.text.MyTextProvier"/>

Now create a class MyTextProvier that extends TextProviderSupport and override getText() methods. All methods take a parameter key as String and you can replace unwanted characters from it. Then call super.getText(). For example

public String getText(String key) {
  return super.getText(key.replaceAll("[\\$\\{\\}]", ""));
}



回答2:


Use <s:text name="keyName" />

http://struts.apache.org/docs/text.html



来源:https://stackoverflow.com/questions/32008505/prevent-gettext-evaluating-el-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!