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

io.github.selectorrr.starters.swagger.config.SwaggerConfigurer Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package io.github.selectorrr.starters.swagger.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.AuthorizationCodeGrantBuilder;
import springfox.documentation.builders.OAuthBuilder;
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.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;

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

@Configuration
@EnableSwagger2
@RequiredArgsConstructor
public class SwaggerConfigurer {

    private static final String OAUTH_NAME = "spring_oauth";
    private static final String ALLOWED_PATHS = "/api/.*";
    private final SwaggerSpringBootProperties properties;

    @Bean
    public Docket taskApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(true)
                .select()
                .paths(regex(ALLOWED_PATHS))
                .build()
                .securitySchemes(Arrays.asList(securityScheme()))
                .securityContexts(Arrays.asList(securityContext()));
    }


    @Bean
    public SecurityConfiguration security() {
        return SecurityConfigurationBuilder.builder()
                .realm(properties.getRealm())
                .clientId(properties.getResource())
                .scopeSeparator(" ")
                .build();
    }


    private SecurityScheme securityScheme() {
        String openIdUrl = properties.getAuthServerUrl() + "/realms/" + properties.getRealm() + "/protocol/openid-connect";
        GrantType grantType =
                new AuthorizationCodeGrantBuilder()
                        .tokenEndpoint(new TokenEndpoint(openIdUrl + "/token", properties.getResource()))
                        .tokenRequestEndpoint(
                                new TokenRequestEndpoint(openIdUrl + "/auth", properties.getResource(), null))
                        .build();

        return new OAuthBuilder()
                .name(OAUTH_NAME)
                .grantTypes(Collections.singletonList(grantType))
                .scopes(Arrays.asList(scopes()))
                .build();
    }


    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{
                new AuthorizationScope("openid", null)
        };
    }


    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(new SecurityReference(OAUTH_NAME, scopes())))
                .forPaths(PathSelectors.regex(ALLOWED_PATHS))
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy