Creating a CSRF protection with Spring 3.x--reference
reference from:http://info.michael-simons.eu/2012/01/11/creating-a-csrf-protection-with-spring-3-1/
Creating a CSRF protection with Spring 3.1
Note: This tutorial is for Spring Security 3.1, an updated version that uses the build-in CSRF protection of Spring Security 3.2 can be found here
CSRF Attacks still seems to be a problem, a pity that there is no standard solution in the Spring 3.1 framework. Although not probably, i wanted to protect my projects by malicious crafted links.
I didn’t want to use an extra library but something which is already available in the Spring framework. Here is what i come up with:
I choose the token protection mechanism for my implementation.
The core of my solution is the CSRFToken Service:
public interface CSRFTokenService { |
import java.security.SecureRandom; |
“getTokenFromSession” is called right after a user logs in, so that the token gets stored into his session.
As you can see in the implementation of “acceptsTokenIn”, the token is only needed and verified when the principal is not null, meaning when a user is authenticated.
The interface contains some constants: The name of the token in forms and requests and the name of the attribute under which the token is stored in the session. The token itself is just a base64 of some random bytes.
I only want the token to be checked in writing methods: METHODS_TO_CHECK, meaning only in put, delete and posts requests. My applications don’t change state based on get requests.
So where to check for the token? I use a pretty simple Spring “HandlerInterceptor”:
package de.dailyfratze.controller; |
This interceptor stops the chain if the request method should be checked and the token is not acceptable by sending a HTTP forbidden error. The additional response header is used by Ajax calls to present a dialog that the session is invalidated.
How to get the token into forms? I wanted to be able to change the token name in only one place so i came up with the following custom tag:
import java.io.IOException; |
with the corresponding mapping:
<?xml version="1.0" encoding="UTF-8" ?> |
I can use this tag in forms like so:
<form method="post" action="foobar"> |
Or for generating url parameters for example for ajax calls like so:
<c:url value="/foobar"> |
So if a token is invalid, the user is either redirect to an error page if it is a normal post, ajax calls through jQuery can be handled like so:
function isInvalidCSRFToken = function(xhr) { |
The code snippets are all taken from a running project. If you want to use them, use them. The package names are missing and must be added. Also the JavaScript code isn’t complete.
Feel free to comment, if you have suggestions, remarks or anything else. Also, if you can use this, i’d be happy to hear from you.
---------------------------------------------------------------------------------------------------------------------------------------------------------
CSRF PROTECTION WITH SPRING SECURITY REVISITED
reference from:http://info.michael-simons.eu/2014/01/29/csrf-protection-with-spring-security-revisited/
At the end of last year, Spring Security 3.2 was released and brought a lot of new features, among them a built-in “Cross Site Request Forgery” protection”.
Nearly two years earlier i wrote my CSRF protection implementation with Spring Security 3.1, have a look here.
I really like the built-in implementation and most of it is very similar to my solution. The main difference is that the protection is at security filter level and not at application level like mine was. Also they use a token class for encapsulating the tokens name and value.
My solution can be very easily adapted to Spring Security 3.2.
First of all, configure it with the onliner
<csrf /> |
or use the new annotation based configuration method.
Then throw away everything from my solution except the CSRFTokenTag. Edit the later one to contain the following code:
import java.io.IOException; |
The guys at Spring have a nice suggestions for including the token for AJAX/Jsons request. The new filter also validates request headers. The recommend adding the header name and token value to the pages meta information like so
<meta name="_csrf" content="${_csrf.token}"/> |
and then manually add the header to each JavaScript request made with jQuery.
An alternative for jQuery users would be the following pre filter:
$.ajaxPrefilter(function(options, originalOptions, jqXHR) { |
I’m happy to be able to get rid of some code of mine, though the solution worked quite well for 2 years now.
Creating a CSRF protection with Spring 3.x--reference的更多相关文章
- How to resolve CSRF protection error while adding service through Ambari api
Short Description: This article will describe on how to disable CSRF protection in Ambari. Article A ...
- HTML form without CSRF protection,HTML表单没有CSRF保护
HTML form without CSRF protection =HTML表单没有CSRF保护 CSRF是伪造客户端请求的一种攻击,CSRF的英文全称是Cross Site Request For ...
- springMVC常见错误-解决org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spring
笔者参考文档: https://blog.csdn.net/sinat_24928447/article/details/47807105 可能错误原因即解决方法: 1.配置文件错误 a)这是配置文件 ...
- spring mybatis circular reference
摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unreso ...
- Spring Boot Admin Reference Guide
1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...
- Spring错误——Spring 注解——factory-bean reference points back to the same bean definition
背景:学习Spring,在使用注解@Bean的name属性配置<bean>实例时,不能注册实例成功 报错 WARNING: Exception encountered during con ...
- 【Spring实战】----开篇(包含系列目录链接)
[Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...
- Cross Site Request Forgery (CSRF)--spring security -转
http://docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html/csrf.html 13. Cross ...
- Spring Security Oauth2 : Possible CSRF detected
Spring Security Oauth2 : Possible CSRF detected 使用Spring Security 作为 Oauth2 授权服务器时,在授权服务器登录授权后,重定向到客 ...
随机推荐
- 基于mapreduce的大规模连通图寻找算法
基于mapreduce的大规模连通图寻找算法 当我们想要知道哪些账号是一个人的时候往往可以通过业务得到两个账号之间有联系,但是这种联系如何传播呢? 问题 已知每个账号之间的联系 如: A B B C ...
- 轻松学习Linux之入门篇
1.Linux概述: 2.Linux优点 3.linux历史待上传 4.linux部分发行版 5.linux政府扶持 本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- cordova,phonegap 重力感应
3.0版本后,cordova通过插件模式实现设备API,使用CLI的plugin命令可以添加或者移除插件: $ cordova plugin add org.apache.cordova.device ...
- 第一百九十六天 how can I 坚持
老妈邮的咸菜到了,美味啊,买不到,哈哈. 以后要勤给鱼换水啊,10天不换,水都臭了,拒绝懒惰. 明天要回济南了,刘松结婚,估计又没法发博客了. 两条鱼,一条罗娜,一条我,哈哈. 睡觉.
- JVM系列四:生产环境参数实例及分析【生产环境实例增加中】
java application项目(非web项目) 改进前: -Xms128m-Xmx128m-XX:NewSize=64m-XX:PermSize=64m-XX:+UseConcMarkSweep ...
- 数据结构(C语言版)---第三章栈和队列 3.4.2 队列的链式表示和实现(循环队列)
这个是循环队列的实现,至于串及数组这两章,等有空再看,下面将学习树. 源码如下: #include <stdio.h> #include <stdlib.h> #define ...
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...
- 应用TcpListener实现的socket服务器端
前言 项目中要实现一个简单的socket服务器端,采用了TcpListener这个类.除了基本的功能之外,有几处需要注意的点. 要能同时接收多个客户端的连接,当然,不需要几千个那么多. 要能探测到客户 ...
- POJ 2774 Long Long Message (后缀数组模板)
借用罗大神的模板,开始搞后缀数组 #include <cstdio> #include <iostream> #include <cstring> #include ...
- C#泛型在unity3D中的运用...
泛型是什么? 这是摘自百度百科中对泛型的介绍: 泛型是c#2.0的一个新增加的特性,它为使用c#语言编写面向对象程序增加了极大的效力和灵活性.不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类 ...