web csrf java_在Java Web应用程序中阻止CSRF
web csrf java
package com.ricardozuasti.csrf;import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.RandomStringUtils;public class LoadSalt implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {// Assume its HTTPHttpServletRequest httpReq = (HttpServletRequest) request;// Check the user session for the salt cache, if none is present we create oneCache<String, Boolean> csrfPreventionSaltCache = (Cache<String, Boolean>)httpReq.getSession().getAttribute("csrfPreventionSaltCache");if (csrfPreventionSaltCache == null){csrfPreventionSaltCache = CacheBuilder.newBuilder().maximumSize(5000).expireAfterWrite(20, TimeUnit.MINUTES).build();httpReq.getSession().setAttribute("csrfPreventionSaltCache", csrfPreventionSaltCache);}// Generate the salt and store it in the users cacheString salt = RandomStringUtils.random(20, 0, 0, true, true, null, new SecureRandom());csrfPreventionSaltCache.put(salt, Boolean.TRUE);// Add the salt to the current request so it can be used// by the page rendered in this requesthttpReq.setAttribute("csrfPreventionSalt", salt);chain.doFilter(request, response);}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void destroy() {}
}
...<filter><filter-name>loadSalt</filter-name><filter-class>com.ricardozuasti.csrf.LoadSalt</filter-class></filter>...<filter-mapping><filter-name>loadSalt</filter-name><url-pattern>*</url-pattern></filter-mapping>...
就像我说的,要在执行安全交易之前验证盐,我们可以编写另一个过滤器:
package com.ricardozuasti.csrf;import com.google.common.cache.Cache;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;public class ValidateSalt implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {// Assume its HTTPHttpServletRequest httpReq = (HttpServletRequest) request;// Get the salt sent with the requestString salt = (String) httpReq.getParameter("csrfPreventionSalt");// Validate that the salt is in the cacheCache<String, Boolean> csrfPreventionSaltCache = (Cache<String, Boolean>)httpReq.getSession().getAttribute("csrfPreventionSaltCache");if (csrfPreventionSaltCache != null &&salt != null &&csrfPreventionSaltCache.getIfPresent(salt) != null){// If the salt is in the cache, we move onchain.doFilter(request, response);} else {// Otherwise we throw an exception aborting the request flowthrow new ServletException("Potential CSRF detected!! Inform a scary sysadmin ASAP.");}}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void destroy() {}
}
...<filter><filter-name>validateSalt</filter-name><filter-class>com.ricardozuasti.csrf.ValidateSalt</filter-class></filter>...<filter-mapping><filter-name>validateSalt</filter-name><url-pattern>/transferMoneyServlet</url-pattern></filter-mapping>...
...
<form action="/transferMoneyServlet" method="get"><input type="hidden" name="csrfPreventionSalt" value="<c:out value='${csrfPreventionSalt}'/>"/>...
</form>
...
当然,您可以编写一个自定义标签,一个不错的Javascript代码,或在每个所需的链接/表单中添加新参数的方法。
参考: Ricardo Zuasti博客博客上的JCG合作伙伴 Ricardo Zuasti 阻止了Java Web应用程序中的CSRF 。
翻译自: https://www.javacodegeeks.com/2012/06/preventing-csrf-in-java-web-apps.html
web csrf java
web csrf java_在Java Web应用程序中阻止CSRF相关推荐
- 在Java Web应用程序中阻止CSRF
跨站点请求伪造攻击(CSRF)在Web应用程序中非常常见,如果允许,可能会造成重大危害. 如果您从未听说过CSRF,建议您查看有关它的OWASP页面 . 幸运的是,阻止CSRF攻击非常简单,我将向您展 ...
- Oracle的Web控制台端口,Oracle Java Web Console(概述)
Oracle Java Web Console(概述) Oracle Java Web Console 为用户访问基于 Web 的系统管理应用程序提供了一个公用位置.可以使用所支持的几种 Web 浏览 ...
- ASP.NET Core与Dapper和VS 2017使用JWT身份验证WEB API并在Angular2客户端应用程序中使用它
目录 介绍 背景 步骤1 创建ASP.NET Core Web API项目 Fitness.JWT.API项目说明 使用代码 startup.cs JwtIssuerOptions.cs JwtCon ...
- Spring MVC,Thymeleaf,Spring Security应用程序中的CSRF保护
跨站点请求伪造(CSRF)是一种攻击,它迫使最终用户在当前已通过身份验证的Web应用程序上执行不需要的操作. 如果您使用Spring Security 3.2及更高版本,在Spring MVC / T ...
- 使用tinylog 1.1改进您在Java EE应用程序中的登录
tinylog是Java的轻量级日志记录框架. 与Apache Log4j和Logback相反,tinylog由仅80KB的单个JAR文件组成,没有任何依赖性,并且具有静态logger类. 这意味着您 ...
- java application pdf_從Java應用程序中即時打開PDF文件
Is there any way to have a code that opens a PDF file in Java application in a platform independant ...
- java web源代码_检测Java Web应用程序而无需修改其源代码
java web源代码 与其他系统进行交互时,大多数Java Web应用程序都使用标准Java接口. 诸如Web页面或REST服务器之类的基于HTTP的服务是使用接口javax.servlet.Ser ...
- java web 自定义标签_如何在JavaWeb程序中使用自定义标签
展开全部 tld定义格式32313133353236313431303231363533e78988e69d8331333361323566 [java] view plain copy print? ...
- 体育馆预约系统java_基于JAVA WEB的高校体育场地预约管理系统(计算机毕业设计)...
项目类型:JAVA WEB项目 项目名称:高校体育场地预约管理系统 用户类型:三种角色(老师+学生+管理员) 难度:7 项目技术:spring+struuts+jsp+jquery+hibernate ...
最新文章
- 格式工厂软件处理视频
- win服务器系统程序原因
- 【教程】Linux 系统下对目录扩容的方法
- mongodb 3.4 安装_Python数据分析及可视化实例之CentOS7.2+MongoDB V3.4 安装
- 看一下iFM最新文章
- [Javascript] Avoid Creating floats if they are not needed
- gitlab中的CI
- 配置汇编环境:使用vs2010+MASM
- UTC时间与当地时间的转换关系?
- 台式电脑屏幕怎么调亮度_电脑屏幕亮度到底怎么调才最好?
- BZOJ 2466 [中山市选2009]树(高斯消元)
- Spring定时器corn表达式详解
- Java源文件编译出错:类文件包含错误的类 请删除该文件或确保文件位于正确的类路径子目录中
- docker compose自定义IP报错ERROR: Pool overlaps with other one on this address space
- 使用两个路由器级连实现共用一个宽带
- 申请一个微信小程序有哪些需要注意的事项
- 小数的二进制与十进制转换
- 深度丨详解 Amazon Go 三大核心技术
- JAVA获得股票数据大全
- python data PDAdata pivot table ,group by, contains, starts with