Search Tutorials

Spring Boot + Swagger 3 (OpenAPI 3) + Security Example(Basic Authentication) | JavaInUse

Spring Boot + Swagger 3 (OpenAPI 3) + Security Example(Basic Authentication)


In previous tutorial we had implemented Spring Boot + Swagger 3 (OpenAPI 3) Hello World Example. Also previously we had implemented Understand Spring Security Architecture and implement Spring Boot Security Example. In this tutorial we will be implementing Spring Boot Basic Security for the spring boot swagger example. So when using Swagger to access the endpoints, swagger also allows us to configure the spring security user name and password.

Spring Boot Swagger- Table of Contents

Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot + Swagger + Profile - Implementing Spring Boot Profile for a Swagger application Spring Boot + Swagger 3 (OpenAPI 3) Hello World Example Spring Boot + Swagger 3 (OpenAPI 3) + Security Example


This tutorial is explained in the below Youtube Video.

Lets Begin

We will be modifying the Spring Boot + Swagger 3 (OpenAPI 3) Hello World Example project we had implemented in the previous tutorial.
Download the project and import the maven project in eclipse. Start the project. If we go to http://localhost:8080/swagger-ui/index.html#/ we see the following
Spring Boot Swagger example
The final maven project we will be developing is as follows -
Spring Boot Swagger Open-api3 security maven
Modify the pom.xml to add the spring security dependency -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath /> <!-- lookup parent from repository -->


		<!-- Swagger UI -->





Next we will be adding the spring security configuration.
  • We annotate the SecurityConfiguration class with @EnableWebSecurity to apply the class to the global WebSecurity
  • Also we extend WebSecurityConfigurerAdapter, which provides us a configuration methods,to define rules to specify what URIs to protect or pass through.
  • Extending WebSecurityConfiguration allows to customize spring security by overriding methods.
package com.javainuse.configuration;

import org.springframework.context.annotation.Bean;

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity http) throws Exception {
                .antMatchers("/swagger-ui/**", "/javainuse-openapi/**").permitAll()

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
Start the project. Using Swagger UI if we not try to test the REST endpoints it asks for the username and password. If we enter these, Swagger is able to successfully interact with the endpoints.
Spring Boot Swagger Open-api3 basic security

Let us now configure Swagger for Spring Security. In the SwaggerSpringDemoApplication class specify SecurityScheme. OpenAPI uses the term security scheme for authentication and authorization schemes. OpenAPI 3.0 lets you describe APIs protected using the following security schemes:
  • HTTP authentication schemes (they use the Authorization header):
    • Basic
    • Bearer
    • other HTTP schemes as defined by RFC 7235 and HTTP Authentication Scheme Registry
  • API keys in headers, query string or cookies
    • Cookie authentication
  • OAuth 2
  • OpenID Connect Discovery
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;

@OpenAPIDefinition(info = @Info(title = "Employees API", version = "2.0", description = "Employees Information"))
@SecurityScheme(name = "javainuseapi", scheme = "basic", type = SecuritySchemeType.HTTP, in = SecuritySchemeIn.HEADER)
public class SwaggerSpringDemoApplication {

	public static void main(String[] args) {, args);


Finally in the controller class use SecurityRequirement to define security requirements for the single operation (when applied at method level) or for all operations of a class (when applied at class level).
package com.javainuse.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;


@SecurityRequirement(name = "javainuseapi")
public class EmployeeController {

	private List<Employee> employees = createList();

	@RequestMapping(value = "/employees", method = RequestMethod.GET, produces = "application/json")
	public List<Employee> firstPage() {
		return employees;

	@DeleteMapping(path = { "/{id}" })
	public Employee delete(@PathVariable("id") int id) {
		Employee deletedEmp = null;
		for (Employee emp : employees) {
			if (emp.getEmpId().equals(id)) {
				deletedEmp = emp;
		return deletedEmp;

	public Employee create(@RequestBody Employee user) {
		return user;

	private static List<Employee> createList() {
		List<Employee> tempEmployees = new ArrayList<>();
		Employee emp1 = new Employee();

		Employee emp2 = new Employee();
		return tempEmployees;


Start the project. We can now see the lock symbol.
Spring Boot Swagger UI Security
If we click on it, it asks for the credentials. On entering it, we can successfully access the REST enfpoints using swagger.
Spring Boot Swagger UI Security

Spring Boot Swagger UI Security

Spring Boot Swagger UI Security

Download Source Code

Download it - Spring Boot + Swagger 3 (OpenAPI 3) + Security Example