All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infusers.core.springdoc.SpringdocConfig Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.springdoc;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;

@Configuration
public class SpringdocConfig {
 
    private static final String SCHEME_NAME_BASIC = "basicAuth";
    private static final String SCHEME_BASIC = "basic";
    
    private static final String SCHEME_NAME_JWT = "Bearer Authentication";
    private static final String SCHEME_JWT = "bearer";
        
    @Value("${swagger.api.url:}")
    private String swaggerApiUrl; // Default to an empty string if not defined    
 
    @Bean
    public OpenAPI customOpenAPI() {    	
        OpenAPI openAPI = new OpenAPI()
	        //.info(getInfo(properties))
	        /*.components(new Components()
	                .addSecuritySchemes(SCHEME_NAME_BASIC, createBasicSecurityScheme()))
	        .addSecurityItem(new SecurityRequirement().addList(SCHEME_NAME_BASIC))*/
	        .components(new Components()
	                .addSecuritySchemes(SCHEME_NAME_JWT, createJWTSecurityScheme()))
	        .addSecurityItem(new SecurityRequirement().addList(SCHEME_NAME_JWT));
	        
	        // Add the server configuration if the swaggerApiUrl is not empty
        	if (swaggerApiUrl != null && !swaggerApiUrl.trim().isEmpty()) {
	            List servers = new ArrayList<>();
	            servers.add(new Server().url(swaggerApiUrl).description("API Server"));
	            openAPI.setServers(servers);
	        }        
        
        return openAPI;
    }
   
    private SecurityScheme createBasicSecurityScheme() {
        return new SecurityScheme()
                .name(SCHEME_NAME_BASIC)
                .type(SecurityScheme.Type.HTTP)
                .scheme(SCHEME_BASIC);
    }
    
    private SecurityScheme createJWTSecurityScheme() {
        return new SecurityScheme()
                .name(SCHEME_NAME_JWT)
                .type(SecurityScheme.Type.HTTP)
                .scheme(SCHEME_JWT);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy