SWAGGER UI – Enable Token-Based Authentication In Spring Boot2 min read
- In Programming
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 –
The code for enabling the security in Swagger is really straight-forward. You need a configuration class (
@Configuration). Enable the Swagger (
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,
- How To Mock SecurityContextHolder in Spring Security using Mockito?
- Load Spring Beans Based On Different Profiles Via XML
- How to Fix A Blunder and Get Squashed Commits Back (Git Undo)
- Time Complexity Comparison Sheet Of Elementary Sorting Algorithms
- Gitlab CI – SSH Into Virtual Private Server (VPS) Using Private Key From Gitlab CI Pipeline Job