DEV Community

Ayush Singh
Ayush Singh

Posted on

Add Basic Authentication (Console - based)

If you just want to see how the authentication looks like in Spring Boot, this blog is for you.

Basic Authentication

  1. Create any controller which you want to secure.

    @GetMapping("/")
    public String helloWorld() {
    return "Hello World";
    }
    
  2. Add the spring-boot-starter-security dependency in pom.xml file. It will automatically add the security on all the endpoints.

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
  3. Now, start the app and you will see a random generated string on the console.

  4. Go to any API client (like Postman) and test the endpoint.

  5. Add Basic Auth security for your endpoint.

  6. Copy the string and use it as a password (Default username is user).

  7. Now, test the endpoint, it will give you the desired result.

Thank you!

Top comments (0)