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 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;

@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";
    
 
    @Bean
    public OpenAPI customOpenAPI() {
        return 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));
    }
   
    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);
    }
}


/*package com.infusers.core.swagger;

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
	@Bean
	public Docket produceApi() {
		return new Docket(DocumentationType.SWAGGER_2)
			    .securityContexts(Arrays.asList(securityContext()))
				.securitySchemes(basicScheme())
				.select()
				.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();
	}

	private List basicScheme() {
		List schemeList = new ArrayList<>();
		//schemeList.add(new BasicAuth("basicAuth"));
		schemeList.add(new ApiKey("JWT", "Authorization", "header"));
		return schemeList;
	}
	
	private SecurityContext securityContext() { 
	    return SecurityContext.builder().securityReferences(defaultAuth()).build(); 
	} 

	private List defaultAuth() { 
	    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); 
	    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; 
	    authorizationScopes[0] = authorizationScope; 
	    return Arrays.asList(new SecurityReference("JWT", authorizationScopes)); 
	}
}*/




© 2015 - 2025 Weber Informatics LLC | Privacy Policy