Skip to main content

CSRF enabled Ajax requests using Spring Security

Many of you who have worked on Spring Security might be aware of the fact that Spring Security protects applications from Cross Site Request Forgery using _csrf tokens in the request sent to the web server. You can find a detailed understanding in the Spring documentation page. The objective of this post is to explain how to send _csrf tokens in the Ajax requests when we protect our application URL and application access using spring security.

How to get CSRF tokens
While we submit a form using an application that is protected with Spring Security, the form gets a default hidden parameter in the form body when using <form:form> element. The param contains the _csrf tokens to authenticate the requests in the server. In case we use other ways to create forms, we have to manually include a hidden parameter that contains name as ${_csrf.parameterName} and  value as ${_csrf.token}. For example,
 <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/

Ajax Requests
While we need  to send ajax requests in an application that is protected with Spring Security, you will most likely find 403 error in the response from the server. This is due to the fact that the server expects a _csrf token that is missing in the XHR. How do we send csrf tokens in the ajax request so the server is happy to regard our request legitimate and help us with the response we need? Well, there are two ways we can do.
  1. Generate csrf token header using spring security and set it in the ajax header.
  2. Generate csrf token using spring security and send them in the ajax request either via GET or POST.

Generate CSRF Tokens
Spring security provides specific taglib that we can use to generate the CSRF tokens for sending them in the XHR requests.
 <%@taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
The tag provides so many features that cater to security requirements. One of the usages of the taglib is to generate the metatags in the header.
 <sec:csrfMetaTags />
This will generate meta tags that are shown as below.
<meta name="_csrf_parameter" content="_csrf" />
<meta name="_csrf_header" content="X-CSRF-TOKEN" />
<meta name="_csrf" content="897dc174-8aac-44be-bf63-7395bcac87f2" />
The above well fits into if we use taglibs and JSPs. But for those who prefer to use other templates like Thymeleaf, the csrf tokens can be obtained using the approach explained in the `How to get CSRF tokens`.

CSRF token or Header?
Depending on the use case, we can set the CSRF token header or just CSRF token. For both GET or POST method, we can exploit the header approach to set the CSRF token header. But in case we need the CSRF token in the request param or request body, we can use just CSRF token. The below approach is based on setting it in the header so it fits for any requests. Assuming we have spring security metatags,
var csrfHeader = $("meta[name='_csrf_header']").attr("content");
var csrfToken  = $("meta[name='_csrf']").attr("content");
we create variables like these from meta tag content. And we set them in the header like below.
$('.editable-action').editable({
                 type : 'select',
                 source : MAKINUSCV.baseURL + '/admin/user/list/action.mk',
                 url : MAKINUSCV.baseURL + '/admin/user/list/do/action.mk',
                 ajaxOptions: {
                               beforeSend: function (xhr)
                                 {
                                   xhr.setRequestHeader(csrfHeader, csrfToken);
                                 }
                        },
                 success : function(response, newValue) {
                        if (response.status == 'error')
                     return response.msg;
                 }
});

In case we need to set in the request param or request body, we will be creating a data object in which we will set CSRF token. In that case, we will want to use the _csrf_parameter variable, not _csrf_header.
var csrfParam   = $("meta[name='_csrf_parameter']").attr("content");
var data        = {};
data[csrfParam] = csrfToken;
And pass the data object to ajax request. If its GET request, just append the csrfParam and csrfToken in the url request.

Comments

  1. Very good ideas and this was very useful for my professional oriented. Thanks for you provides a great post and I expect to you more information. Your written style is very impressed to me and I'm waiting for your updates...
    Spoken English Classes in Chennai
    Best Spoken English Classes in Chennai
    Html5 Training in Chennai
    IELTS Coaching in Chennai
    Pega Training in Chennai
    Japanese Classes in Chennai
    TOEFL Coaching in Chennai
    Spoken English Classes in Porur
    Spoken English Classes in Anna Nagar

    ReplyDelete
  2. Nice post with lots of information keep up the good work

    Python Course | Digital Marketing Course | Java Course

    ReplyDelete
  3. If you have bad credit and you can't qualify for a 0% APR credit card, don't give up and swear off dentists until you win the lottery. It's a tough situation, but you still have some dental financing options to consider.

    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com

    ReplyDelete
  4. Your post is really good thanks for sharing these kind of post but if anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in mumbai, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or Visit https://experiencecertificates.com/experience-certificate-provider-in-mumbai.html

    ReplyDelete
  5. I generally check this kind of article and I found your article which is related to my interest. Genuinely it is good and instructive information. Thankful to you for sharing an article like this. acmc token price

    ReplyDelete
  6. Can you make a video about it? I think such video will look great on youtube. From here https://soclikes.com/ you can get likes for your video

    ReplyDelete
  7. Have to work? need of money but have no experience certificate. Get in touch with us we provide experience certificate in Gurgaon 100% genuine certificate in Gurgaon. It will help it your courier. So don’t be late. Get your experience letter now. For experience letter in Delhi contact at 9599119376 or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-Gurgaon.html

    ReplyDelete
  8. You’re so interesting! I don’t believe I’ve truly read something like this before. So great to find someone with genuine thoughts on this issue. Really.. many thanks for starting this up. This website is something that’s needed on the internet, someone with some originality!

    CBSE Schools In Ahmedabad
    CBSE Schools In Surat
    CBSE Schools In Rajkot
    CBSE Schools In Visakhapatnam
    CBSE Schools In Kangra
    CBSE Schools In Shimla
    CBSE Schools In Jammu
    CBSE Schools In Solan
    CBSE Schools In Mangalore
    CBSE Schools In Mysore

    ReplyDelete
  9. I read the above article and I got some knowledge from your article. It's actually great and useful data for us. Thanks for share it. Information Security Training In Canada

    ReplyDelete
  10. It is what I was searching for is really informative. It is a significant and useful post for us. Thankful to you for sharing video like this. SEO service provider in India

    ReplyDelete
  11. I'm really inspired together with your writing skills and also with the layout for your weblog. Thanks for sharing this blog here. Privileged Access Management Solutions Software

    ReplyDelete
  12. สูตรลอตเตอรี่ที่คำนวณแบบสำเร็จรูปนั้นสามารถใช้ได้กับทุกสลากกินแบ่งออนไลน์ เป็นสูตรที่คำนวณจากสถิติที่ถูกต้องโดยฝังสถิติสลากกินแบ่งมากยิ่งกว่า 10 ปีลงเอา เเทงหวย ไว้ภายในระบบโปรแกรมจากนักเขียนโปรแกรมคำนวณสลากกินแบ่งที่มากประสบการณ์มาทำไว้ให้ และก็ในกระดานลอตเตอรี่เราประเทศไทยนี้ ยังมีดูดวงโหราศาสตร์เกี่ยวกับการเล่นลอตเตอรี่ เเทงหวยออนไลน์ เพื่อลดการเสี่ยงการเล่นว่าจะซื้อวันนี้ดีไหม โชควันนี้คืออะไร จะไปซื้อลอตเตอรี่ทางด้านไหนทางไหนดี แม้กระทั้งวิธีการทำนายฝันของความฝันแต่ละคนฝันว่าอะไร ฝันมองเห็นอะไรก็มาเป็นดูดวงชะตาพยากรณ์ฝันสลากกินแบ่งได้แต่ละวันเช่น เว็บหวย เดียวกันเป็นทายฝันที่แม่นที่สุด รวมทั้งจำนวนถูกต้องแม่นยำ เพื่อช่วยลดการเสี่ยงของคนเล่นสลากกินแบ่งได้อย่างดีเยี่ยม

    ReplyDelete
  13. สลากกินแบ่งออนไลน์
    เรียกว่าในตอนนี้กำลังเป็นที่นิยมอย่างยิ่ง และออกรางวัลแทบทุกวี่วันอย่างยิ่งจริงๆสำหรับ ลอตเตอรี่ออนไลน์ ในตอนนี้เข้าใจกันอยู่ว่าการจำหน่ายสลากกินแบ่งผ่านอินเทอร์เน็ตเป็นเรื่องที่ไม่ยาก โดยแนวทางการซื้อนั้นสามารถทำเป็น ด้วยการเติมเงินเข้าระบบ แต่ว่าคุณควรมีบัญชีผู้ใช้แล้วก็รหัสผ่านสำหรับการเข้าใช้งานก่อน ก็เลยจะสามารถซื้อสลากกินแบ่งออนไลน์บนเว็บนั้นๆได้สิ่งจำเป็นที่สุดมันก็คือส่วนวิธีขาย ลอตเตอรี่ฮานอย จะแบ่งได้เป็น 2 แบบหมายถึงเป็นเจ้าของเว็บหรือที่เรียกกันว่าเจ้ามือขายสลากกินแบ่งออนไลน์ แทงหวยฮานอย แล้วก็เป็นผู้แทนขายลอตเตอรี่ออนไลน์นั่นเอง

    สลากกินแบ่งฮานอย
    สำหรับจุดเด่นของการเล่น หวยออนไลน์ โดยยิ่งไปกว่านั้นในออนไลน์ สิ่งจำเป็นมันก็คือว่าพวกเรานั้นสามารถจำหน่ายง่ายผ่านอินเทอร์เน็ต ไม่ยุ่งยากสบายเร็วไว แล้วก็สิ่งจำเป็นที่สุดซึ่งก็คือว่าสำเร็จทดแทนสูง เนื่องด้วยเป็นการซื้อแบบไม่ผ่านพ่อค้าคนกลาง แล้วก็ลดการเอารัดเอาเปรียบผู้ซื้อ ถ้าหากว่าถูกรางวัล จะได้รับเงินอย่างเร็ว ไม่เกิน 6 นาฬิกาเย็น ข้างในวันที่ลอตเตอรี่ออก สลากกินแบ่งออนไลน์ขยายเวลาการค้าขายในวันที่สลากกินแบ่งออกถึง 15.30 น. ซื้อได้ในราคาที่ระบุ ราคาแพงที่แน่ๆ ลอตเตอรี่ลาว สามารถตรึงราคาได้ หวยออนไลน์ สลากกินแบ่งออนไลน์สามารถซื้อได้ทุกจำนวน ไม่มีเลขกลั้นหรือระบบเลขเต็ม ลดปัญหาเรื่องการซื้อหวยลง

    ReplyDelete
  14. Thanks for sharing informative post. Are looking for best Tamil typing tool online, make use of our Tamil typing software to make translation faster. Thirumana Porutham in Tamil | Samacheer Kalvi Books PDF

    ReplyDelete
  15. Lion123 เว็บไซต์คาสิโนออนไลน์ ที่มาแรงที่สุดตอนนี้ ข้างในเว็บไซต์นั้นมีทุกการพนัน ไม่ว่าจะเป็น กีฬา คาสิโน สล็อต บาคาร่า รูเล็ต เสือมังกร ยิงปลา คาสิโนออนไลน์ แล้วก็อื่นๆอีกมาหมาย เว็บไซต์ lion123 นั้นมีความน่าไว้วางใจ เป็นเว็บไซต์ที่นักพนันนั้นเอ๋ยถึงเยอะที่สุด รับประกันจากเสียงของสมาชิกในเว็บไซต์พวกเรา ตอบปัญหามาที่สุด ระบบออโต้ ใช้งานง่าย เร็ว ไม่ต้องคอยอะไรบ้างในนานๆ มีทุกสิ่งครบพนันเว็บไซต์เดียวจบทุกสิ่ง lion123 การเข้าถึงง่ายดาย การันตีวัสดุอุปกรณ์ทุกต้นแบบ ใช้งานสบาย เร็ว ไม่มีอันตราย แน่ๆ 100%

    ReplyDelete
  16. สำหรับเว็บไซต์ สล็อตpg ของพวกเรานั้น พวกเราชูมาให้ท่านแล้วตรงนี้ที่เดียว SLOT XO เล่นบันเทิงใจ เล่นง่าย ได้เงินจริง สำหรับเว็บไซต์ของพวกเรานั้นมีเกมน่าเลือกล่นมาก สามารถที่จะสร้างผลกำไรจากการเล่นได้ สร้างผลกำไรได้อย่างดีเยี่ยม รวมทั้งสามารถที่จะเข้ามาพนันกับทางเว็บไซต์ slot xo พวกเราได้ตลอด 1 วัน

    ท่านเองสามารถที่จะเข้ามาพนันกับทางเว็บไซต์ Slot pg พวกเราได้ เว็บไซต์ของพวกเรานั้นเป็นเว็บไซต์ที่เยี่ยมที่สุด ใช้งานง่ายสุดๆที่สุด งามสูงที่สุดแล้วเวลานี้ พวกเรามากับความแปลกใหม่ คัดสรรเกม slotpg ที่มีความมากมาย เล่นบันเทิงใจได้ตลอด 1 วัน มีเกมสล็อตดังนานัปการค่าย ไม่ต้องเดินทางให้เสียเวล่ำเวลา อยู่ที่แหน่งใดก็พนันได้ เล่นผ่านมือถือได้ เล่นสนุกสนาน รวมทั้งได้เงินจริง สามารถสร้างผลกำไรได้มาก

    ReplyDelete
  17. Many website owners erroneously make the assumption that optimization was included in development. It rarely is. Or perhaps it's more appropriate to say that sufficient optimization was likely not included bmrb wisc edu

    ReplyDelete
  18. ไม่ว่าจะเติมเงินเท่าไหร่ PG ก็มีโอกาสได้รับเงินรางวัลมากมาย พร้อมมีให้เลือกเพิ่มเติมกับเครดิตฟรี มีให้รับอย่างหลากหลาย ไม่ว่าจะเป็นโปรโมชั่นสมัครสมาชิก แนะนำเพื่อน ฝากทั้งวัน และรวมไปถึงคืนยอดเสีย ได้ตลอด 24 ชั่วโมง

    ReplyDelete
  19. เดิมพันน้อย ก็สามารถทำกำไรได้อย่างมากมาย กับเครดิตฟรี ที่รับได้อย่างมากมาย ไม่ว่าจะเป็นโปรโมชั่นสมัครสมาชิก โปรโมชั่นแนะนำเพื่อน โปรโมชั่นฝากทั้งวัน และโปรโมชั่นคืนยอดเสีย รับเครดิตได้ตลอดทั้งเดือน พีจี

    ReplyDelete
  20. Creating a number of cybersecurity precautions will protect you and your family, as well as your coworkers, and your employer. Cybersecurity Risk Manager Career Overview

    ReplyDelete
  21. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Looking for reliable and effective home tuition for 11th class? Ziyyara’s online home tuition for class 11 offers personalized guidance and comprehensive support to help you excel in your studies.
    For more info Contact us: +91-9654271931, +971-505593798 or visit Home tuition for 11th class

    ReplyDelete
  22. pg slot เว็บตรงอันดับ 1 เมื่อพูดถึงความบันเทิงออนไลน์ ไม่สามารถที่จะไม่พูดถึงสล็อตออนไลน์ได้ PG SLOT เกมสล็อตได้กลายเป็นหนึ่งในเกมคาสิโนที่นิยมและเป็นที่นิยมมากที่สุดในยุคปัจจุบันนี้

    ReplyDelete

Post a Comment

Popular posts from this blog

A wonderful technique to reduce website development cost

Websites - Good way to get online presence Websites are very vital to get online presence of any business nowadays. Websites are categorized into two different types. First one is Static Website and second one is Dynamic Website, normally known as web applications. Static websites are most widely used for any business since they help to bring up the online presence more easily and quickly. Depending on the content and features, static websites cost around $300-$700 . It includes web design and development. Apart from that, the business has to spend for hosting space and domain name for the website. Cloud based development is now more prevalent. Building a website and running it will be very easy and cheap using these cloud infrastructure. But the difficulty facing the development of static websites still looms high as it does not matter who provides the infrastructure. The development cost is still same. Technology - LAMP Static websites are developed using HTML and PHP mostly

In-Place editing with X-Editable using Datatable plugin

Introduction In-place editing is a trending feature that can be seen in many latest web applications, a popular example would be trello.com where the editing data happens on the web page without any explicit forms or popups. Another such example I could point out is, thoughtplan.com. The in-place editing is very nice in such a way that editing data seems so natural and user friendly. To enable in-place editing, there are many free JQuery plugins available on the internet. We are discussing a very popular plugin called x-editable . Most of the time we use html tables to display data where in-place editing is enabled. Hence we need another plugin to elegantly display tables with enormous data. We use a famous JQuery plugin called datatable . Both of these plugins are used widely and free to use. Assumption      : Bootstrap 3, JQuery used Projects Integration In order to enable datatable features on any ordinary table found on web page, we should initialise datatable plugin for