Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    Fixing 401 Error with CORS Preflights (with Spring boot)

    Back-End Development
    spring boot cors java security
    3
    3
    519
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Udith Indrakantha
      Udith Indrakantha Web Development last edited by

      alt text
      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.

      Error in brief

      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.

      alt text

      Reason
      My pre-flight request is not responded with status OK(200).

      Solution

      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, click me )

      By using Web Security in Spring Boot,

      You have to create a web security configuration class. I hope everybody creates that class for backend authentication purposes.

      If you have already created that class, inside configure( HttpSecurity httpSecurity ) method ,add this line httpSecurity.cors();
      alt text

      It’s that simple !!!
      Originally published at https://webtechmora.blogspot.com.

      1 Reply Last reply Reply Quote 2
      • root
        root Linux Help last edited by

        great bro

        1 Reply Last reply Reply Quote 1
        • Nubelle
          Nubelle Web Development last edited by

          good work bro

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post

          3
          Online

          3.6k
          Users

          1.3k
          Topics

          5.3k
          Posts

          • Privacy
          • Terms & Conditions
          • Donate

          © Copyrights and All right reserved Lanka Developers Community

          Powered by Axis Technologies (PVT) Ltd

          Made with in Sri Lanka

          | |