How to enable Basic authentication on Swagger UI using spring security

  Reading Time:

Assuming we have already configured Swagger in our project , we shall turn on basic authentication using spring security on spring boot v2

We have to create SecurityConfiguration as shown below

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

       private static final String[] AUTH_LIST = { //
                        "/v2/api-docs", //
                        "/configuration/ui", //
                        "/swagger-resources", //
                        "/configuration/security", //
                        "/swagger-ui.html", //
                        "/webjars/**" //

        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                                .password(passwordEncoder().encode("admin")).roles("USER", "ADMIN");

        protected void configure(HttpSecurity http) throws Exception {

        public PasswordEncoder passwordEncoder() {
                return new BCryptPasswordEncoder();

Here we add all the swagger url pattern behind basic authentication

private static final String[] AUTH_LIST = { //
                        "/swagger-resources/**", //
                        "/swagger-ui.html**", //
                        "/webjars/**", //
                        "/v2/api-docs" //

When you point your chrome browser to http://localhost:8080/swagger-ui.html we will see the browser throw default basic authentication login popup

Basic authentication popup

Once we login successfully we will see Swagger-UI page

Swagger UI


We just saw how to enable basic authentication on Swagger UI using spring security basic authentication

The code used in the article is available on GITHUB

How to enable caching in Spring Boot 2 using Redis

Since version 3.1, the Spring Framework provides support for adding caching to an existing Spring application. The caching abstraction allows consistent use of various caching solutions. We’ll enable simple caching in our project using caching abstraction in Spring and will use Redis to cache data...

App & Geek   Never miss a story from App & Geek, get updates in your inbox.