<?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>Forem: Tharun</title>
    <description>The latest articles on Forem by Tharun (@tharun_07).</description>
    <link>https://forem.com/tharun_07</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%2F1424568%2F2a5ff1a1-3899-4783-af10-251bc1b92c89.jpg</url>
      <title>Forem: Tharun</title>
      <link>https://forem.com/tharun_07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tharun_07"/>
    <language>en</language>
    <item>
      <title>Spring Security’s Silent Rules That Break Your App</title>
      <dc:creator>Tharun</dc:creator>
      <pubDate>Tue, 22 Apr 2025 20:55:12 +0000</pubDate>
      <link>https://forem.com/tharun_07/spring-securitys-silent-rules-that-break-your-app-55j1</link>
      <guid>https://forem.com/tharun_07/spring-securitys-silent-rules-that-break-your-app-55j1</guid>
      <description>&lt;p&gt;If you’ve ever added Spring Security to your project and immediately thought:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;"Why is nothing working?!"&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;…you’re not alone.&lt;/p&gt;

&lt;p&gt;Spring Security is powerful, but its default behavior can be confusing. After debugging countless issues (and reading one too many Stack Overflow threads), I’ve compiled some of the basic mistakes I made — the kind many beginners are likely to make as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Silent Lockdown: Why All Your Endpoints Return 401
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: You add spring-boot-starter-security, and suddenly every request gets blocked.&lt;br&gt;
&lt;strong&gt;Why?&lt;/strong&gt; Spring Security defaults to securing all endpoints.&lt;br&gt;
&lt;strong&gt;Fix&lt;/strong&gt;: (&lt;em&gt;Gradually tighten security starting with permitAll()&lt;/em&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean  
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {  
    http.authorizeHttpRequests(auth -&amp;gt; auth.anyRequest().permitAll());  
    return http.build();  
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSRF &amp;amp; Stateless APIs: The Hidden 403 Forbidden
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Your POST requests fail even with valid tokens.&lt;br&gt;
&lt;strong&gt;Why?&lt;/strong&gt; CSRF protection is enabled by default (good for traditional apps, bad for APIs).&lt;br&gt;
&lt;strong&gt;Fix&lt;/strong&gt;:&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(csrf -&amp;gt; csrf.disable());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;When to keep CSRF? Only for session-based apps.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  CORS Errors: Why @CrossOrigin Isn’t Enough
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Your frontend gets blocked despite using @CrossOrigin.&lt;br&gt;
&lt;strong&gt;Why?&lt;/strong&gt; Spring Security overrides CORS settings.&lt;br&gt;
&lt;strong&gt;Fix&lt;/strong&gt;: Configure it in SecurityFilterChain&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http.cors(cors -&amp;gt; cors.configurationSource(request -&amp;gt; {  
    CorsConfiguration config = new CorsConfiguration();  
    config.setAllowedOrigins(List.of("http://localhost:3000"));  
    config.setAllowedMethods(List.of("*"));  
    return config;  
}));  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Spring Security’s defaults are secure—but that also means they’re restrictive. The key is understanding why things break before changing them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s your biggest Spring Security struggle?&lt;/strong&gt; Did I miss any common pitfalls? Let’s discuss in the comments!&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>learning</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
