Here is an example of spring boot basic authentication using spring security. Security most important feature while working application especially for the web application. The application does not allow to access all information for all user based on user ROLEs allowed to information to complete those requirement spring security is a very useful module of spring. While developing REST API using spring boot basic authentication will be too much important. Here is the complete example of spring boot basic authentication.

At following places, basic authentication plays an important role.

  • Develop Rest API
  • Internal communication will spring boot application
  • Require to pass authentication information in header at every require
  • Stateless application development


To work with spring security following spring boot dependency is requires. Here is complete pom also available.

    <groupId>org.springframework.boot</groupId>  <!--starter require for spring boot spring security-->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
    <description>spring boot basic authentication</description>
    <!-- Inherit defaults from Spring Boot -->
            <groupId>org.springframework.boot</groupId>  <!--starter require for spring boot spring security-->
    <!-- Package as an executable jar -->


Here users are created in memory. It can be overridden by UserDetailsService if required getting user details from the database. This configuration will automatically enable spring security. By default, spring security will enable for URLs.

package com.javadeveloperzone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
 * Created by JavaDeveloperZone on 04-08-2017.
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
    @Autowired      // here is configuration related to spring boot basic authentication
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            .withUser("zone2").password("mypassword").roles("USER");// those are user name and password
    protected void configure(HttpSecurity http) throws Exception {
        .httpBasic() // it indicate basic authentication is requires
        .antMatchers( "/index").permitAll() // /index will be accessible directly no need of any authentication
        .anyRequest().authenticated(); // it's indicate all request will be secure


Its startup class to start spring boot application.

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
 * Created by JavaDeveloperZone on 19-07-2017.
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {, args);            // it wil start application


This controller can only be accessible if a user is authenticated. If user tries to access page first spring security ask for basic authentication and current user details it will be allowed to access information from it.

package com.javadeveloperzone.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 * Created by Java Developer Zone on 19-07-2017.
public class SpringBootExampleController {
    public String SpringBootHello() {
        return "You are authorise to access this page, You passed spring boot basic authentication successfully..";
    public String index() 
       return "This is public page"; 


Now run your application and try to access location:8080. It will ask for entering username and password.
spring boot basic authentication - demo

spring boot basic authentication – demo

If authentication failed than it will return 404 status code. Its indicate Unauthorized access.
spring boot basic invalid authentication

spring boot basic invalid authentication

If authentication success than it will allow accessing information
spring boot basic authentication success

spring boot basic authentication success



Spring Boot Security Documentation


Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *