Ani*_*nil 5 java csrf spring-security
在最近对我们的 Java Web 应用程序进行安全扫描时,我们发现了 CSRF 漏洞。我知道对于使用 Spring Security 之类的安全框架的较新应用程序,我们可以轻松地为每个表单添加一个隐藏的输入并进行其他所需的配置,这将解决问题。
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
Run Code Online (Sandbox Code Playgroud)
但是我们的应用程序仍然使用 acegi-security (1.0.2) 并且有 100 多个用 JSP 编写的表单。在所有这些表单上添加输入类型隐藏的 csrf 令牌似乎非常乏味。有没有更聪明的方法来保护我的应用程序而无需所有这些艰苦的工作。
感谢您的反馈和答复。我遵循以下解决方案。我创建了两个过滤器。设置CsrfTokenFilter。doFilter 方法执行以下操作。
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpRes = (HttpServletResponse) response;
String randomLong = ""+random.nextLong();
Cookie cookie = new Cookie("csrfToken", randomLong);
httpRes.addCookie(cookie);
next.doFilter(request, response);
Run Code Online (Sandbox Code Playgroud)
验证CsrfTokenFilter。doFilter 方法执行以下操作
String csrfToken = httpReq.getParameter("csrfToken");
String tokenFromCookie = getCsrfTokenFromCookie(httpReq);
if (WmUtil.isEmpty(csrfToken) || !csrfToken.equals(tokenFromCookie)) {
httpRes.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
else {
next.doFilter(request, response);
}
Run Code Online (Sandbox Code Playgroud)
为我的 web.xml 中的几乎所有网址添加了这两个过滤器。最后在我的jsp页面中通过jquery以各种形式注入下面的代码。
<input type="hidden" name="csrfToken" value="readFromCookieThroughJavascript"/>
Run Code Online (Sandbox Code Playgroud)
这解决了我的问题,下次扫描找不到任何 csrf 问题。对于那些想要服务器端过滤器的完整源代码和客户端 JavaScript 代码的人,我创建了一个 git 项目。 https://github.com/anilpank/oldWebAppCsrfProtection
| 归档时间: |
|
| 查看次数: |
1794 次 |
| 最近记录: |