-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use the Bearer Authentication for apis? #90
Comments
Definition
use |
I use comment |
Hi, I am very new to this, and for sure I use the wrong syntax!? I'm trying to get the Header value prefixed with @xxiaocheng At way back =), did you find a solution? In // @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization And then at the routes I added But in the gui I have to add the string The generated files for ex
At Swagger see BearerAuth it seems that you should specify
|
If you want to use with swagger v2.0, then after trying few solutions, it worked for me. |
Server.go At controller: |
To summarize the findings of previous posters:
When using Swagger UI in a browser, you must specify |
Hi, for OpenAPI 2.0 just enter the following comments and generate the docs by swag init.
On Swagger UI in a browser, you must specify the bearer in the value field of the authorization pop up: |
How can I have "Bearer" as the default value in authorization, so I only need to add the JWT token, like when I use Postman and select "Auth Type: Bearer Token" and only need to add the token in the field? |
To pre-fill the token field with the "Bearer" prefix in Swagger, you can customize the Swagger template generated by Swaggo. Unfortunately, Swaggo does not provide a direct configuration to automatically add the "Bearer" prefix to the authentication field, but there is an alternative solution:
Here is an example of how to do this: Step 1: Inject JavaScript into SwaggerCreate a JS file (e.g., document.addEventListener('DOMContentLoaded', function() {
const ui = window.ui;
ui.preauthorizeApiKey('Bearer', 'Bearer ');
// Configure the token field to automatically fill with "Bearer " at the start
const authTokenInput = document.querySelector('input[placeholder="api_key"]');
if (authTokenInput) {
authTokenInput.addEventListener('input', function() {
if (!authTokenInput.value.startsWith('Bearer ')) {
authTokenInput.value = 'Bearer ' + authTokenInput.value;
}
});
}
}); Step 2: Include the JS in Swagger InitializationIn your code where you set up Swagger with gin-swagger, add a reference to the script: import (
"github.com/gin-gonic/gin"
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)
func main() {
r := gin.Default()
// Serve Swagger UI with the custom script
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.CustomJS("/swagger-setup.js")))
r.Run()
} Step 3: Serve the JavaScript FileMake sure the r.Static("/swagger-setup.js", "./swagger-setup.js") This way, when you open the Swagger UI, the token field will be automatically pre-filled with "Bearer ", and you will only need to add the token manually. |
swaggo/swag#1854 the swaggo will support it~ beartoken |
No description provided.
The text was updated successfully, but these errors were encountered: