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

ru.mynewtons.starter.swagger.config.SwaggerWebAppConfig Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package ru.mynewtons.starter.swagger.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
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.Collections;
import java.util.List;

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

@Configuration
@EnableSwagger2
@PropertySource("classpath:swagger.properties")
public class SwaggerWebAppConfig extends WebMvcConfigurerAdapter {

    public static final String securitySchemaOAuth2 = "oauth2";
    public static final String authorizationScopeGlobal = "global";
    public static final String authorizationScopeGlobalDesc = "accessEverything";

    @Value("${starter.swagger.oauth.url}")
    private String swaggerOAuthUrl;

    @Value("${starter.swagger.oauth.clientId}")
    private String swaggerOAuthClientId;

    @Value("${starter.swagger.app.name}")
    private String appName;

    @Value("${starter.swagger.has.oauth}")
    private Boolean hasOauth;

    @Bean
    public Docket allApiDocumentation() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("all")
                .select().paths(PathSelectors.ant("/api/**")).build()
                .apiInfo(buildApiInfo("All API"));
        if (hasOauth) {
            docket.securitySchemes(Collections.singletonList(securitySchema()))
                    .securityContexts(Collections.singletonList(securityContext()));
        }
        return docket;

    }


    @Bean
    public SecurityConfiguration securityInfo() {
        return new SecurityConfiguration(swaggerOAuthClientId, "112233", "realm", "swagger",
                "", ApiKeyVehicle.QUERY_PARAM, "", " ");
    }


    @Bean
    public Docket securityApiDocumentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("security")
                .select().paths(regex("/oauth/.*")).build()
                .apiInfo(buildApiInfo("Security API"));
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(defaultAuth())
                .forPaths(PathSelectors.ant("/api/**")).build();
    }

    private OAuth securitySchema() {
        final AuthorizationScope authorizationScope =
                new AuthorizationScope(authorizationScopeGlobal, authorizationScopeGlobalDesc);
        final LoginEndpoint loginEndpoint = new LoginEndpoint(swaggerOAuthUrl);
        final GrantType grantType = new ImplicitGrant(loginEndpoint, "access_token");
        return new OAuth(securitySchemaOAuth2, Collections.singletonList(authorizationScope),
                Collections.singletonList(grantType));
    }

    private List defaultAuth() {
        final AuthorizationScope authorizationScope =
                new AuthorizationScope(authorizationScopeGlobal, authorizationScopeGlobalDesc);
        final AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Collections
                .singletonList(new SecurityReference(securitySchemaOAuth2, authorizationScopes));
    }

    private ApiInfo buildApiInfo(String description) {
        return new ApiInfoBuilder()
                .title(appName)
                .description(description)
                .version("0.0.1")
                .contact(new Contact("http://mynewtons.ru/", "http://mynewtons.ru", ""))
                .license("All rights reserved Copyright © 2018")
                .build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy