Why Spring Security authentication causes CORS error

Why Spring Security authentication causes CORS error

I have a backend server made in Java with Spring Boot, Security and Web and a client made with Angular.

I have a backend server made in Java with Spring Boot, Security and Web and a client made with Angular.

Currently I am trying to make a simple request under localhost:8080/resource.

The controller for this address is shown bellow:

@RestController
public class IndexController {
    @CrossOrigin
    @RequestMapping("/resource")
    public Map<String, Object> home() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("id", UUID.randomUUID().toString());
        model.put("content", "Hello World");

    return model;
}

}

And the Angular client (the part that performs the request) is this:

import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";

@Component({ selector: "app-root", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent { public title = "Security Client"; public greeting = {};

constructor(private http: HttpClient) {
    http.get("http://localhost:8080/resource").subscribe(data =&gt; this.greeting = data);
}

}

The problem by using just what was shown is that I get a CORS error.

Whether removing Spring Security from my pom.xml or adding this configuration:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests().antMatchers("/resource").permitAll();
}

}

Solves the problem.

What I wanna know is why I am getting an CORS error instead of a 401 Unauthorized when accessing an address that demands user authentication.

java spring

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Spring Beans in Java

Spring IoC container is the core of the Spring Framework.In spring-based applications, objects live inside a spring containerThe container instan.

Java Spring Boot First App

Step by Step to your First Spring App

Spring | Spring JDBC Tutorial | Java Spring Tutorial Spring Training

Spring | Spring JDBC Tutorial | Java Spring Tutorial Spring Training . Spring Framework Certification Training This post Spring JDBC Tutorial video will help you

Top 10 Advanced Java and Spring Boot Courses for Full-Stack Java Developers

These are best online courses to learn Spring Boot, Advanced Java, Docker, React, Microservices, DEvops, and Angular to become full stack Java developer.