In this quick tutorial, we'll show how to set up basic authentication in Spring Boot project using Spring Security. Spring Security offers a lot of security methods and this one is the simplest to configure -when you add Spring Security to your classpath, Basic authentication is enabled by default. The only thing we should do is configure the username and password for our default user.


First, we should add Spring security Maven dependency to our pom.xml file:


Also, we'll create a simple endpoint to test our authentication. For the purpose of simplicity, it will only return a string.

package com.javahowtos.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class DemoController {

    public String getSomeResponse() {
        return "Hello!";

Then, we'll configure the username and password in application.yml file:

      name: myUsername
      password: somepassword

And that's it. Now, let's try this out using Postman:

How to set up basic authentication in Spring Boot

If you're using some other tool, be sure that it sends Authorization header follows "Basic Base64EncodedString" rule where Base64EncodedString represents Base64 encoded username:password string.


In this tutorial, we created a simple project and configured a basic authentication for a Spring Boot project. The complete code could be found on GitHub.