Struts2 Session 使用和安全

When your Action class needs to access the HTTP session object implement the SessionAware interface and override the setSession method.

ps: 实现 SessionAware 的 setSession 方法得到 session,但是这里的session是Map型的,struts2框架会做Map 和HttpSession之间的转换。

public void setSession(Map<String, Object) session) {

   userSession = session ;

}
 

Be sure to also implement the ParameterNameAware interface and override the acceptableParameterName method to mitigate a potential security vulnerability .

PS: 为了确保使用session的安全性,我们最好再实现ParameterNameAware接口的acceptableParameterName方法,具体如下 。 当参数的名字里包含session,request该方法会返回false,告诉struts2 framework 忽略这些参数。

public boolean acceptableParameterName(String parameterName) {
		
		boolean allowedParameterName = true ;
		
		if ( parameterName.contains("session")  || parameterName.contains("request") ) {
		
			allowedParameterName = false ;
			
		} 
		
		return allowedParameterName;
	}
 

 

If you have multiple actions that implement SessionAware then consider modifying the params interceptor's excludeParams value as part of your Struts 2 package setup。

PS: 如果你包里许多的action里实现SessionAware 可以如下做统一做安全保证配置,就可以不用再去实现ParameterNameAware 接口的acceptableParameterName 方法了。

 

<package name="basicstruts2" extends="struts-default">

 		<interceptors>
	 		<interceptor-stack name="appDefault">
	        	 <interceptor-ref name="defaultStack">
	      			<param name="params.excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
	   			</interceptor-ref>
	 	    </interceptor-stack>
		</interceptors>
		
		<default-interceptor-ref name="appDefault" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值