<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pramitha Jayasooriya</title>
    <description>The latest articles on DEV Community by Pramitha Jayasooriya (@pramithamj).</description>
    <link>https://dev.to/pramithamj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1407250%2F5f5b2ecb-ac8d-4600-94cd-dd070501b86a.jpeg</url>
      <title>DEV Community: Pramitha Jayasooriya</title>
      <link>https://dev.to/pramithamj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pramithamj"/>
    <language>en</language>
    <item>
      <title>Run Ballerina in Your Browser!</title>
      <dc:creator>Pramitha Jayasooriya</dc:creator>
      <pubDate>Sat, 15 Nov 2025 07:58:36 +0000</pubDate>
      <link>https://dev.to/pramithamj/run-ballerina-in-your-browser-10fo</link>
      <guid>https://dev.to/pramithamj/run-ballerina-in-your-browser-10fo</guid>
      <description>&lt;h2&gt;
  
  
  ⚡ &lt;strong&gt;Instant Access, Powerful Engine&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Ballerina Online Playground eliminates the setup barrier. With a single click, you get a secure, sandboxed environment ready to write, compile, and execute Ballerina code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxtidalo0hq1ag9kgf3jk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxtidalo0hq1ag9kgf3jk.png" alt="Compiler" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Launch the Playground Here&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://ballerina-online-playground.pages.dev/" rel="noopener noreferrer"&gt;https://ballerina-online-playground.pages.dev/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqosq3qfu7bfe2spnb4xf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqosq3qfu7bfe2spnb4xf.png" alt="https://ballerina-online-playground.pages.dev/" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core workflow:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code Editor (Left Panel):&lt;/strong&gt; Write or paste your Ballerina code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run Code:&lt;/strong&gt; Click &lt;strong&gt;Run Code&lt;/strong&gt; or press &lt;code&gt;Ctrl + Enter&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output Panel (Right Panel):&lt;/strong&gt; View compilation and execution output in real time&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Editor Tools:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reset&lt;/strong&gt; → load sample code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear&lt;/strong&gt; → start fresh&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🤖 &lt;strong&gt;The Ballerina AI Assistant: Your Intelligent Co-Pilot&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxcuwmtgv798uc9ojo74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxcuwmtgv798uc9ojo74.png" alt="Ballerina AI" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What sets this Playground apart is the integrated &lt;strong&gt;AI Assistant&lt;/strong&gt;, powered by &lt;strong&gt;Google Gemini and OpenAI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is more than a chatbot — it’s a &lt;strong&gt;real-time Ballerina expert&lt;/strong&gt;, making learning and debugging fast and intuitive.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Quick Actions:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explain Code:&lt;/strong&gt;
Detailed, line-by-line explanations of your Ballerina logic&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix Errors:&lt;/strong&gt;
Suggestions to resolve compilation/runtime errors&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimize:&lt;/strong&gt;
Professional tips for performance and best practices&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Help:&lt;/strong&gt;
Ask about syntax, modules, error handling, or any Ballerina concept&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can click the quick-action buttons or ask custom questions in chat.&lt;br&gt;
AI-suggested code can be inserted directly into the editor with &lt;strong&gt;Insert Code&lt;/strong&gt;, providing a seamless workflow.&lt;/p&gt;

&lt;p&gt;📌 &lt;em&gt;AI is fully context-aware and trained using ballerina.io documentation.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🧹 &lt;strong&gt;Real-Time Code Quality with Ballerina Lint&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvzeazvqmmyigxs6k712c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvzeazvqmmyigxs6k712c.png" alt="balarina lint" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maintaining high code quality is crucial.&lt;br&gt;
The Playground integrates a &lt;strong&gt;Best Practices Checker&lt;/strong&gt; inspired by the &lt;strong&gt;Ballerina Lint VS Code Extension&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As you type, the linter provides real-time feedback on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code quality&lt;/li&gt;
&lt;li&gt;Ballerina best practices&lt;/li&gt;
&lt;li&gt;Recommended conventions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;VS Code Extension:&lt;br&gt;
👉 &lt;a href="https://marketplace.visualstudio.com/items?itemName=Pramitha.ballerina-lint" rel="noopener noreferrer"&gt;https://marketplace.visualstudio.com/items?itemName=Pramitha.ballerina-lint&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🏗️ &lt;strong&gt;Comprehensive Version Support (26 Ballerina Versions!)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Unlike simple online compilers, the Playground gives you full control over your execution environment.&lt;/p&gt;

&lt;p&gt;You can switch between &lt;strong&gt;26 Ballerina versions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From &lt;strong&gt;2201.3.0 (Swan Lake Update 3)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Up to &lt;strong&gt;2201.12.0 (Swan Lake Update 12)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Including the &lt;strong&gt;Latest Development Build&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Use Cases:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Testing compatibility&lt;/li&gt;
&lt;li&gt;Reproducing version-specific issues&lt;/li&gt;
&lt;li&gt;Educational purposes&lt;/li&gt;
&lt;li&gt;Trying out older or newer runtime behaviors&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📝 &lt;strong&gt;Monaco Editor Integration&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Built using the same engine as &lt;strong&gt;VS Code&lt;/strong&gt;, the Monaco Editor provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax highlighting&lt;/li&gt;
&lt;li&gt;Auto-completion&lt;/li&gt;
&lt;li&gt;IntelliSense&lt;/li&gt;
&lt;li&gt;Advanced editing features&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⌨️ &lt;strong&gt;Supported Features &amp;amp; Keyboard Shortcuts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Playground supports all core language features so you can focus on learning Ballerina efficiently.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojxn0yyfw5aok1e5kmgw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojxn0yyfw5aok1e5kmgw.png" alt="Keyboard shortcut" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔒 &lt;strong&gt;Security and Limitations&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Playground is built with strict sandboxing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No internet access&lt;/li&gt;
&lt;li&gt;Isolated environment&lt;/li&gt;
&lt;li&gt;Limited resources&lt;/li&gt;
&lt;li&gt;Read-only file system&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Disabled for security reasons:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;HTTP clients&lt;/li&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;External APIs&lt;/li&gt;
&lt;li&gt;Database connections&lt;/li&gt;
&lt;li&gt;File system access&lt;/li&gt;
&lt;li&gt;Services/listeners&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ensures a &lt;strong&gt;safe and stable&lt;/strong&gt; environment for execution.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 &lt;strong&gt;Ready to Start Coding?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Experience the best way to learn and prototype with Ballerina today.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Start coding with the Ballerina Online Playground&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://ballerina-online-playground.pages.dev/" rel="noopener noreferrer"&gt;https://ballerina-online-playground.pages.dev/&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;~ By Pramitha Jayasooriya&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  📬 &lt;strong&gt;Contact Details&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;LinkedIn:&lt;/strong&gt; &lt;a href="https://www.linkedin.com/in/pramitha-jayasooriya/" rel="noopener noreferrer"&gt;Pramitha Jayasooriya&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/PramithaMJ" rel="noopener noreferrer"&gt;PramithaMJ&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;X (Twitter):&lt;/strong&gt; &lt;a href="https://x.com/PramithaMJ" rel="noopener noreferrer"&gt;PramithaMJ&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Personal Website:&lt;/strong&gt; &lt;a href="https://PramithaMJ.live" rel="noopener noreferrer"&gt;PramithaMJ.live&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Email:&lt;/strong&gt; &lt;a href="mailto:lpramithamj@gmail.com"&gt;lpramithamj@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking forward to connecting with you!&lt;/p&gt;

</description>
      <category>ballerina</category>
      <category>ballerinaonlinecompiler</category>
      <category>ballerinaonlineplayground</category>
      <category>onlinecompiler</category>
    </item>
    <item>
      <title>Why do we need to use Circuit Bracker Pattern inside Microservices?</title>
      <dc:creator>Pramitha Jayasooriya</dc:creator>
      <pubDate>Mon, 08 Apr 2024 03:24:39 +0000</pubDate>
      <link>https://dev.to/pramithamj/why-do-we-need-to-use-circuit-bracker-pattern-inside-microservices-1a21</link>
      <guid>https://dev.to/pramithamj/why-do-we-need-to-use-circuit-bracker-pattern-inside-microservices-1a21</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeopo9agzxxwz0waamkr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeopo9agzxxwz0waamkr.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In today’s world of distributed systems and microservices architecture, ensuring resilience and fault tolerance is crucial. As microservices communicate with each other over networks that can be unreliable, services may fail, leading to cascading failures and degraded performance. The Circuit Breaker pattern offers a &lt;strong&gt;&lt;em&gt;solution to this problem by providing a mechanism to detect and handle failures gracefully, thereby improving the overall reliability of the system.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Circuit Breaker Pattern?
&lt;/h3&gt;

&lt;p&gt;The Circuit Breaker Pattern, inspired by its electrical counterpart, is a design pattern used in software development to handle faults and failures in distributed systems. It is implemented as a state machine that monitors the health of a service or resource. When the number of failures exceeds a predefined threshold, the circuit breaker trips and prevents further calls to the failing service for a specified period. During this time, the circuit breaker redirects calls to a fallback mechanism, such as returning cached data or providing a default response, thus preventing the failure from propagating through the system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use the Circuit Breaker Pattern in Microservices?
&lt;/h3&gt;

&lt;p&gt;In a microservices architecture, where services are independent and communicate over networks, failures are inevitable. A failure in one service can potentially impact other services that depend on it, leading to a domino effect of failures across the system. By implementing the Circuit Breaker pattern, developers can isolate and contain failures, preventing them from spreading and causing widespread outages. Additionally, the Circuit Breaker pattern helps to improve system resilience by providing mechanisms for fault tolerance, graceful degradation, and recovery.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing the Circuit Breaker Pattern
&lt;/h3&gt;

&lt;p&gt;Let’s illustrate the implementation of the Circuit Breaker pattern in a simple microservice scenario using Java and Spring Boot.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dependencies:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependencies&amp;gt;
    &amp;lt;dependency&amp;gt;
        &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
    &amp;lt;/dependency&amp;gt;
    &amp;lt;dependency&amp;gt;
        &amp;lt;groupId&amp;gt;org.springframework.cloud&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;spring-cloud-starter-circuitbreaker-resilience4j&amp;lt;/artifactId&amp;gt;
    &amp;lt;/dependency&amp;gt;
&amp;lt;/dependencies&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Circuit Breaker Configuration:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.stereotype.Service;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class ServiceA {
    @CircuitBreaker(name = "serviceA")
    public String callServiceB() {
        // Call to Service B
        // Return response or throw exception if failed
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Fallback Method:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.stereotype.Service;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class ServiceA {
    @CircuitBreaker(name = "serviceA", fallbackMethod = "fallback")
    public String callServiceB() {
        // Call to Service B
        // Return response or throw exception if failed
    }
    public String fallback(Exception e) {
        return "Fallback response";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4 . Circuit Breaker Configuration (application.properties):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resilience4j.circuitbreaker.instances.serviceA.register-health-indicator=true
resilience4j.circuitbreaker.instances.serviceA.failure-rate-threshold=50
resilience4j.circuitbreaker.instances.serviceA.wait-duration-in-open-state=5000
resilience4j.circuitbreaker.instances.serviceA.sliding-window-size=5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;The Circuit Breaker pattern is a valuable tool for ensuring resilience in microservices architectures. By implementing this pattern, developers can mitigate the impact of failures, prevent cascading failures, and improve the overall reliability of their systems. Through proper configuration and integration with frameworks like Spring Boot and Resilience4j, developers can build robust and fault-tolerant microservices that can withstand the challenges of distributed computing.&lt;/p&gt;

&lt;p&gt;~ By Pramitha Jayasooriya&lt;/p&gt;

&lt;p&gt;Contact Details&lt;br&gt;
For further information or to discuss potential opportunities, please feel free to connect with me on my professional and social platforms:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/pramitha-jayasooriya/"&gt;Pramitha-Jayasooriya&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/PramithaMJ"&gt;PramithaMJ&lt;/a&gt;&lt;br&gt;
Personal Website: &lt;a href="https://pramithamj.me/"&gt;PramithaMJ.me&lt;/a&gt;&lt;br&gt;
Email : &lt;a href="mailto:lpramithamj@gmail.com"&gt;lpramithamj@gmail.com&lt;/a&gt;&lt;br&gt;
Looking forward to connecting with you!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>[Solved] Cross-Site Request Forgery (CSRF) Attacks with Spring Security.</title>
      <dc:creator>Pramitha Jayasooriya</dc:creator>
      <pubDate>Fri, 05 Apr 2024 02:19:05 +0000</pubDate>
      <link>https://dev.to/pramithamj/solved-cross-site-request-forgery-csrf-attacks-with-spring-security-32po</link>
      <guid>https://dev.to/pramithamj/solved-cross-site-request-forgery-csrf-attacks-with-spring-security-32po</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69qlamihnujdn711vnd1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69qlamihnujdn711vnd1.png" alt="CSRF Attact" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A typical Cross-Site Request Forgery (CSRF or XSRF) attack aims to perform an operation in a web application on behalf of a user without their explicit consent. In general, it doesn’t directly steal the user’s identity, but it exploits the user to carry out an action without their will.&lt;/p&gt;

&lt;p&gt;Consider you are using the website nextflix.com and the attacker’s website evil.com.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1:&lt;/em&gt; The Netflix user login to Nextflix.com and the backend server of Nextflix will provide a cookie that will be stored in the browser against the domain name Nextflix.com&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 2:&lt;/em&gt; The same Nexflix user opens an evil.com website in another tab of the browser&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 3 :&lt;/em&gt; The user was tempted and clicked on the malicious link which makes a request to Nextflix.com. And since the login cookie is already present in the same browser and the request change email is being made to the same domain Nextflix.com, The backend server of Nextflix.com can’t differentiate from where the request came. So here the evil.com forged the request as if it is coming from a Nextflix.com UI page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="https://nextflix.com/changeEmail"
method= "POST" id = "form"&amp;gt;
&amp;lt;input type= "hidden" name="email" value="user@evil.com"&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;script&amp;gt;
doucment.getElementById('form').submit()
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Solution to CSRF attack
&lt;/h2&gt;

&lt;p&gt;To defeat an SCRF attack, the application needs a way to determine if the HTTP request is legitimately generated via the application’s user interface. the best way to achieve this is through a CSRF token. A CSRF token is a secure random token that is used to prevent CSRF attacks. The token needs to be unique per user session and should be of large random value to make it difficult to guess.&lt;/p&gt;

&lt;p&gt;Let’s see how the CSRF attacks by taking the previous Netflix example again.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1:&lt;/em&gt; The Netflix user logs in to Nextflix.com and the backend server of Nextflix will provide a cookie that will be stored in the browser against the domain name Nextflix. com along with a randomly generated unique CSRF token for this particular user session. CSRF token is inserted within hidden parameters of HTML forms to avoid exposure to session cookies.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 2:&lt;/em&gt; The same Nextflix user opens the evils.com website in another tab of the browser.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 3:&lt;/em&gt; User tempted and clicked on the malicious link which make a request to Nextflix.com. Since the login cookie is already present in the same browser and the request to change email is being made to the same domain Netflix.com. This time the nextfllix.com backend server except for the CSRF token along with the cookie. the CSRF token must be the same as the initial value generated during the operation.&lt;/p&gt;

&lt;p&gt;The CSRF token will be used by the application server to verify the legitimacy of the end-user request if it is coming from the same App UI or not. the application server rejects the request if the CSRF token fails to match the test.&lt;/p&gt;

&lt;h2&gt;
  
  
  TIPS
&lt;/h2&gt;

&lt;p&gt;By default, Spring Security enables CSRF fixes for all the HTTP methods that result in data changes like POST, DELETE, etc. But not for GET.&lt;/p&gt;

&lt;p&gt;Using Spring Security configurations we can disable the CSRF protection for complete applications or only a few paths based on our requirements like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http.csrf().disable()
http.csrf().ignoring RequestMatchers("/saveMsg")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thymeleaf has great integration &amp;amp; support with Spring Security to generate a CSRF token. We just need to add the below code in the login HTML form code and Thymeleaf will automatically append the CSRF token for the remaining pages/forms inside the web application,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Enhancements and Best Practices
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. CSRF Token Storage:&lt;/strong&gt;&lt;br&gt;
Ensure that the CSRF token is securely stored in the user’s session. Spring Security automatically handles this for you, but it’s crucial to emphasize the importance of secure session management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Token Expiry and Regeneration:&lt;/strong&gt;&lt;br&gt;
Implement mechanisms to expire and regenerate CSRF tokens to prevent token reuse and enhance security. Spring Security offers built-in features for token expiration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Custom CSRF Header:&lt;/strong&gt;&lt;br&gt;
Consider using a custom header for CSRF tokens, especially if your application involves multiple technologies or if you want to add an extra layer of security. Spring Security allows customization of the header name.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http.csrf().headerName("X-CSRF-TOKEN");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Strict Content Security Policy (CSP):&lt;/strong&gt;&lt;br&gt;
Enforce strict Content Security Policy headers on your web pages to mitigate the risk of XSS attacks. This will further secure your application by limiting the sources from which resources can be loaded.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http.headers().contentSecurityPolicy("default-src 'self'");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. CSRF Token in AJAX Requests:&lt;/strong&gt;&lt;br&gt;
If your application makes AJAX requests, ensure that the CSRF token is included and validated in those requests. Modify your Thymeleaf code accordingly to handle AJAX scenarios.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$.ajax({
    url: '/changeEmail',
    type: 'POST',
    data: {
        email: 'user@evil.com',
        _csrf: /* CSRF Token value */
    },
    success: function(response) {
        // Handle success
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Educate Users:&lt;/strong&gt;&lt;br&gt;
Educate your users about the importance of not clicking on suspicious links and being cautious while interacting with websites. User awareness is a crucial component in preventing CSRF attacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Logging and Monitoring:&lt;/strong&gt;&lt;br&gt;
Implement comprehensive logging and monitoring to keep track of suspicious activities and potential CSRF attacks. This will help in identifying and mitigating threats in real time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Incorporating CSRF protection is a fundamental step in securing your Spring Boot applications. By following best practices and leveraging the capabilities of Spring Security, you can significantly reduce the risk of CSRF attacks. Remember that security is an ongoing process, and staying informed about the latest security developments is essential to maintaining a robust defense against evolving threats.&lt;/p&gt;

&lt;p&gt;By implementing these additional measures and best practices, you can fortify your application’s security posture and provide a safer online experience for your users. Stay vigilant, keep your dependencies up-to-date, and regularly review and enhance your security practices to adapt to emerging threats.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;~By Pramitha Jayasooriya&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Contact Details&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
For further information or to discuss potential opportunities, please feel free to connect with me on my professional and social platforms:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/pramitha-jayasooriya/"&gt;Pramitha Jayasooriya&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/PramithaMJ"&gt;PramithaMJ&lt;/a&gt;&lt;br&gt;
Personal Website: &lt;a href="https://pramithamj.me"&gt;Pramithamj.me&lt;/a&gt;&lt;br&gt;
Looking forward to connecting with you!&lt;/p&gt;

</description>
      <category>springsecurity</category>
      <category>csrf</category>
      <category>springboot</category>
      <category>cybersecurity</category>
    </item>
  </channel>
</rss>
