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

io.pythagoras.common.swaggerui.SwaggerConfig Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package io.pythagoras.common.swaggerui;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private String serviceName;

    private String serviceDesc = "desr";

    private String clientId = "gigy";

    private String clientSecret = "secret";

    private String authorizationServiceEndpoint;


    @Autowired
    public void setProperties(SwaggerProperties properties) {
        this.serviceName = properties.getName();
        this.serviceDesc = properties.getDescription();
        this.clientId = properties.getOauth().getClientId();
        this.clientSecret = properties.getOauth().getClientSecret();
        this.authorizationServiceEndpoint = properties.getAuthorizationTokenEndpoint();
    }

    @Bean
    public Docket postsApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
                .apiInfo(apiInfo()).select().paths(postPaths())
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .paths(springBootActuatorJmxPaths())
                .build()
                .securitySchemes(Collections.singletonList(oauth()))
                ;
    }

    private Predicate postPaths() {
        return regex("/.*");
    }

    private Predicate springBootActuatorJmxPaths() {
        return regex("^/(?!env|restart|pause|resume|refresh).*$");
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(serviceName).description(serviceDesc).build();
    }

    @Bean
    List grantTypes() {
        List grantTypes = new ArrayList<>();

        ResourceOwnerPasswordCredentialsGrant grantType = new ResourceOwnerPasswordCredentialsGrant(authorizationServiceEndpoint);
        grantTypes.add(grantType);
        return grantTypes;

    }

    @Bean
    SecurityScheme oauth() {
        return new OAuthBuilder()
                .name("OAuth2")
                .scopes(scopes())
                .grantTypes(grantTypes())
                .build();
    }

    private List scopes() {
        List list = new ArrayList();
        list.add(new AuthorizationScope("read", "Grants read access"));
        list.add(new AuthorizationScope("write", "Grants write access"));
        return list;
    }

    @Bean
    public SecurityConfiguration securityInfo() {
        return new SecurityConfiguration(clientId, clientSecret, "realm", clientId, "apiKey", ApiKeyVehicle.HEADER, "api_key", ",");

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy