<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Fixing 401 Error with CORS Preflights (with Spring boot)]]></title><description><![CDATA[<p dir="auto"><img src="https://miro.medium.com/max/800/0*4mvk7Rz4nSozj6gq.jpg" alt="alt text" class=" img-responsive img-markdown" /><br />
When the web application runs on a different domain and the server runs on another domain, we have to deal with cross origin requests. But, if we don’t know the mechanism of cross origin resource sharing (CORS), we will end up with errors like 401.</p>
<p dir="auto"><strong>Error in brief</strong></p>
<p dir="auto">My Angular web application runs on port 4200, and my spring boot application runs on server on port 8080. I could not enable CORS in my backend, so, I got an error when sending a request from my application like this.</p>
<p dir="auto"><img src="https://miro.medium.com/max/2000/0*XL7e45nflM1Xc3dX.PNG" alt="alt text" class=" img-responsive img-markdown" /></p>
<p dir="auto"><strong>Reason</strong><br />
My <a href="https://webtechmora.blogspot.com/2019/09/pre-flight-request.html" target="_blank" rel="noopener noreferrer nofollow ugc">pre-flight request</a> is not responded with status OK(200).</p>
<p dir="auto"><strong>Solution</strong></p>
<p dir="auto">I enabled CORS in my backend. To do this, there are several methods. Here in my post, I have shown the easiest way to do that. But, if you are interest in knowing other ways, <a href="https://www.baeldung.com/spring-cors" target="_blank" rel="noopener noreferrer nofollow ugc">click me</a> )</p>
<p dir="auto"><strong>By using Web Security in Spring Boot,</strong></p>
<p dir="auto">You have to create a web security configuration class. I hope everybody creates that class for backend authentication purposes.</p>
<p dir="auto">If you have already created that class, inside configure( HttpSecurity httpSecurity ) method ,add this line <strong>httpSecurity.cors();</strong><br />
<img src="https://miro.medium.com/max/800/0*sukHgZ6i-_KPsbEh.png" alt="alt text" class=" img-responsive img-markdown" /></p>
<p dir="auto">It’s that simple !!!<br />
Originally published at <a href="https://webtechmora.blogspot.com/2019/09/fixing-401-error-with-cors-preflights.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://webtechmora.blogspot.com</a>.</p>
]]></description><link>https://lankadevelopers.lk/topic/420/fixing-401-error-with-cors-preflights-with-spring-boot</link><generator>RSS for Node</generator><lastBuildDate>Thu, 12 Mar 2026 17:09:37 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/420.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 23 Oct 2019 18:32:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fixing 401 Error with CORS Preflights (with Spring boot) on Thu, 24 Oct 2019 08:36:58 GMT]]></title><description><![CDATA[<p dir="auto">good work bro</p>
]]></description><link>https://lankadevelopers.lk/post/2422</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2422</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Thu, 24 Oct 2019 08:36:58 GMT</pubDate></item><item><title><![CDATA[Reply to Fixing 401 Error with CORS Preflights (with Spring boot) on Wed, 23 Oct 2019 19:35:17 GMT]]></title><description><![CDATA[<p dir="auto">great bro</p>
]]></description><link>https://lankadevelopers.lk/post/2416</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2416</guid><dc:creator><![CDATA[root]]></dc:creator><pubDate>Wed, 23 Oct 2019 19:35:17 GMT</pubDate></item></channel></rss>