SWAGGER UI – Enable Token-Based Authentication In Spring Boot2 min read

Swagger UI is the first choice of every developer and tester when it comes to documenting service APIs. It provides live documentation for the APIs where you can actually send the request and get the things done.

It really makes it easy for other people to understand your APIs as well.

Whenever you are working on a project that depends on stateless authentication (which is most of the applications) then you need a way to pass the authentication token (JWT or Custom) from your Swagger UI to the backend.

That feature doesn’t come automatically with the Swagger and that is because Swagger does not know how you are configuring your APIs security. You will have to manually enable it from the configuration.

This is how you will find the UI after configuring it –

Enable the UI for passing the Auth Token
Enable Authorize Button on the UI
Pass the auth token from swagger ui to the backend
Pass your Auth Token from the SWAGGER

The code for enabling the security in Swagger is really straight-forward. You need a configuration class (@Configuration). Enable the Swagger (@EnableSwagger2).

Now add the security schemes and security context info to the Docket. Here’s the code for the same.

Let me know your views on the same.

Articles that you would be interested in,

Read more on programming here…

  • Article By: Varun Shrivastava

  • Varun Shrivastava is an innovative Full Stack Developer at ThoughtWorks with around 4 years of experience in building enterprise software systems in finance and retail domain. Experienced in design, development, and deployment of scalable software. He is a passionate blogger and loves to write about philosophy, programming, tech and relationships. This is his space, you can get in touch with him here anytime you want.