简体   繁体   English

Struts 1中的CSRF令牌实现

[英]CSRF Token implementation in Struts 1

We are using synchronizer token to prevent CSRF vulnerability as below 我们正在使用同步器令牌来防止CSRF漏洞,如下所示

1> saveToken(request)
2> <input type="hidden"
   name="<%=org.apache.struts.taglib.html.Constants.TOKEN_KEY%>"
   value="<bean:write name="<%=Globals.TRANSACTION_TOKEN_KEY%>"/>">
3> isTokenValid(request) 

The fix is not working due to token not being refreshed. 由于未刷新令牌,因此修复程序无法正常工作。 what can be causing this. 是什么原因造成的。

Also what difference, below code will make 还有什么不同,下面的代码将使

isTokenValid(request,reset) isTokenValid(请求,重置)

During the action that displays your edit page, you call the saveToken method. 在显示编辑页面的操作期间,您调用saveToken方法。

saveToken(request)

This generated a new token and saves it on the session (the html:form tag detects this value and stores it as a hidden value on your html form). 这将生成一个新令牌并将其保存在会话中( html:form标记检测到此值并将其作为隐藏值存储在html表单中)。 You don't really need to create an input hidden element in your JSP, cause the saveToken method together with html:form will create it. 您实际上并不需要在JSP中创建输入隐藏元素,因为saveToken方法与html:form一起将创建它。

During the action that saves your data, you call the isTokenValid method. 在保存数据的操作期间,您调用isTokenValid方法。 This method checks that the value submitted matches the token saved on the session. 此方法检查提交的值是否与会话中保存的令牌匹配。

At this point and if the token is valid, you have two options: 此时,如果令牌有效,则有两种选择:

  1. You can call resetToken , which clears the token on the session. 您可以调用resetToken ,以清除会话上的令牌。 So, if the user submits the page again, the token on the session should be cleared and the second call to isTokenValid will fail. 因此,如果用户再次提交页面,则应清除会话上的令牌,并且对isTokenValid的第二次调用将失败。

  2. Pass true in as the second parameter to isTokenValid . 将true作为第二个参数传递给isTokenValid This will reset the token after checking it. 检查后将重置令牌。

     isTokenValid(request,true) 

You can find more info in Struts API of: 您可以在以下Struts API中找到更多信息:

  1. isTokenValid(javax.servlet.http.HttpServletRequest request) isTokenValid(javax.servlet.http.HttpServletRequest请求)
  2. saveToken(javax.servlet.http.HttpServletRequest request) saveToken(javax.servlet.http.HttpServletRequest请求)

Hope this help you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM