Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is CSRF protection and when should it be disabled in Spring Boot? CSRF सुरक्षा क्या है और स्प्रिंग बूट में इसे कब अक्षम करना चाहिए?

Answer

CSRF (Cross-Site Request Forgery) protection prevents a malicious site from tricking an authenticated user's browser into submitting an unwanted state-changing request using the user's existing session cookie. Spring Security defends against this by requiring a unique, unpredictable CSRF token to be included in state-changing requests (POST, PUT, DELETE), which it validates against the token stored in the user's session.

CSRF protection matters for browser-based, cookie-authenticated session apps but is typically disabled for stateless REST APIs authenticated with a bearer token (JWT) or API key, since those aren't vulnerable to the cookie-based attack CSRF protection defends against, and enforcing it would break token-based clients that don't send the CSRF header.

http.csrf(csrf -> csrf.disable()); // typical for stateless JWT-secured REST APIs

CSRF (Cross-Site Request Forgery) सुरक्षा किसी दुर्भावनापूर्ण साइट को ऑथेंटिकेटेड यूज़र के ब्राउज़र को यूज़र की मौजूदा सेशन कुकी का उपयोग करके अवांछित स्टेट-बदलने वाली रिक्वेस्ट सबमिट करने से रोकती है। स्प्रिंग सिक्योरिटी इसके खिलाफ स्टेट-बदलने वाली रिक्वेस्ट्स में एक अद्वितीय CSRF टोकन शामिल करने की आवश्यकता रखकर बचाव करता है।

CSRF सुरक्षा ब्राउज़र-आधारित, कुकी-ऑथेंटिकेटेड सेशन ऐप्स के लिए मायने रखती है लेकिन आमतौर पर बेयरर टोकन (JWT) से ऑथेंटिकेट होने वाली स्टेटलेस REST API के लिए अक्षम की जाती है, क्योंकि वे कुकी-आधारित हमले के प्रति संवेदनशील नहीं होतीं।

http.csrf(csrf -> csrf.disable()); // स्टेटलेस JWT-सुरक्षित REST API के लिए सामान्य

Was this answer clear?