Post

Implement swagger 3

βœ… swagger v3

πŸ”΄ HTTPS CORS error

However, there kept happening a CORS error
1

πŸ”΅ What I tried

In browser, I was using https.
However, the generated server url in swagger seemed to be http
So I decided to change http to https.

image

🟒 Solution

In SwaggerConfig, I added server and server url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
@OpenAPIDefinition(
        info = @io.swagger.v3.oas.annotations.info.Info(
                title = "DrugstoreShop project",
                description = "μ›ν•˜λŠ” μƒν’ˆμ„ μž₯λ°”κ΅¬λ‹ˆμ— λ‹΄μ•„ μ‚΄ 수 μžˆλŠ” κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€",
                version = "1.0.0"
        ),
        servers = {
                @Server(url = "https://drugstoreproject.shop", description = "Generated server url") //🟒 add server url
        }
)

public class SwaggerConfig {
  //swagger config
}

Now, the page looks like this.
And the CORS error was gone.
image

πŸ”΄ Required Token Problem

The cors error was fixed, and APIs that do not require tokens were run successfully.
However, we stil need to fix the problem of APIs that need token.
If APIs are run without token, I got a 500 error.

Screenshot 2024-07-10 at 16 58 55

πŸ”΅ Identify cause of error

THe problem is because there is no token, when the API requires token.

🟒 Add token in swagger

βœ”οΈ add build.gradle

1
 implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'

βœ”οΈ add to SwaggerConfig
πŸ’‘ Reference https://velog.io/@ryulkim/Spring-boot-JWT%EC%99%80-Swagger-JWT-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

1
2
3
4
5
private SecurityScheme createAPIKeyScheme() {
    return new SecurityScheme().type(SecurityScheme.Type.HTTP)
        .bearerFormat("JWT")
        .scheme("bearer");
}

πŸ”΄ HTTP Error

However, it was not run.
There seemed to be HTTP error.
The blog reference uses http, whereas I am using https

πŸ”΅ Swagger Security Scheme type

I realized there are various Swagger Security Scheme types.
πŸ’‘ Reference https://swagger.io/docs/specification/authentication/basic-authentication/
πŸ’‘ Reference https://swagger.io/docs/specification/authentication/api-keys/

I leaned that I have to use APIKEY.
If I am using HTTPS/SSL, I have to use type APIKEY.

🟒 change type to APIKEY

1
2
3
4
5
6
7
    private SecurityScheme createAPIKeyScheme() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER)
                .bearerFormat("JWT")
                .scheme("bearer");
    }

Finally, I made Authorize button in swagger.
I could login and get the token.
And also I could put token in the Authorize button.

1 2

πŸ”΄ Header name difference error

However, for some reason, it would not recognize the token.

πŸ”΅ Identify cause of error

I looked closely at postman, developer tools and swagger,
I realized the name of developer tools and swagger is different!

  • Developer tools token
    name of token is Token
    1

  • Swagger token
    name of token is Authorization
    2

🟒 Change the token header name of swagger

I decided to change the swagger token header name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class SwaggerConfig {
    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI().addSecurityItem(new SecurityRequirement().
                        addList("Bearer token"))
                .components(new Components().addSecuritySchemes
                        ("Bearer token", createAPIKeyScheme())) //🟒 change
                .info(new Info().title("DrugstoreShop project")
                        .description("μ›ν•˜λŠ” μƒν’ˆμ„ μž₯λ°”κ΅¬λ‹ˆμ— λ‹΄μ•„ μ‚΄ 수 μžˆλŠ” κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.")
                        .version("1.0.0"));
    }

    private SecurityScheme createAPIKeyScheme() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER)
                .name("token") //🟒 change
                .bearerFormat("JWT")
                .scheme("bearer");
    }

}

Screenshot 2024-07-10 at 22 17 10

Finally, all APIs recognize the token and works perfectly.
My swagger has been finished!

https://drugstoreproject.shop/swagger-ui/index.html#/

This post is licensed under CC BY 4.0 by the author.