Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup XSRF protection in angular js?

Tags:

angularjs

csrf

I am developing an application using angularJs, resources and jersey rest api's. And I want to implement xsrf protection in my project. Please suggest a better example. I got one example here, but it uses ColdFusion. http://www.bennadel.com/blog/2568-Preventing-Cross-Site-Request-Forgery-CSRF-XSRF-With-AngularJS-And-ColdFusion.htm

like image 800
user1863368 Avatar asked Mar 17 '14 19:03

user1863368


1 Answers

Different from given example, you need to do 2 things:

  1. When the main page gets loaded once the user logs in, you need to set a session cookie with name XSRF-COOKIE. Then AngularJS will do the rest by appending a header to every request as stated in documentation (1)
  2. You need to validate every call to your rest API in back-end (for example with an interceptor) by comparing the token in cookie and the one in header. The logic is described on the page you referenced

(1) To take advantage of this, your server needs to set a token in a JavaScript readable session cookie called XSRF-TOKEN on the first HTTP GET request. CSRF Protection section in Documentation

like image 167
mesutozer Avatar answered Nov 09 '22 06:11

mesutozer