ru.mynewtons.starter.swagger.config.SwaggerWebAppConfig Maven / Gradle / Ivy
The 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.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.GrantType;
import springfox.documentation.service.OAuth;
import springfox.documentation.service.ResourceOwnerPasswordCredentialsGrant;
import springfox.documentation.service.SecurityReference;
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.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.oauth.clientSecret}")
private String swaggerOAuthClientSecret;
@Value("${starter.swagger.app.name}")
private String appName;
@Value("${starter.swagger.app.version}")
private String appVersion;
@Value("${starter.swagger.description.api.security}")
private String securityApiDescription;
@Value("${starter.swagger.description.api.all}")
private String allApiDescription;
@Value("${starter.swagger.has.oauth}")
private Boolean hasOauth;
@Value("${starter.swagger.license}")
private String license;
@Value("${starter.swagger.contact.name}")
private String contactName;
@Value("${starter.swagger.contact.url}")
private String contactUrl;
@Value("${starter.swagger.contact.email}")
private String contactEmail;
@Bean
public Docket allApiDocumentation() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("all")
.select().paths(PathSelectors.ant("/api/**")).build()
.apiInfo(buildApiInfo(allApiDescription));
if (hasOauth) {
docket.securitySchemes(Collections.singletonList(securitySchema()))
.securityContexts(Collections.singletonList(securityContext()));
}
return docket;
}
@Bean
public SecurityConfiguration securityInfo() {
return SecurityConfigurationBuilder.builder()
.appName(appName)
.clientId(swaggerOAuthClientId)
.clientSecret(swaggerOAuthClientSecret)
.realm("realm")
.scopeSeparator(" ")
.build();
}
@Bean
public Docket securityApiDocumentation() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("security")
.select().paths(regex("/oauth/.*")).build()
.apiInfo(buildApiInfo(securityApiDescription));
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth())
.forPaths(PathSelectors.ant("/api/**")).build();
}
private OAuth securitySchema() {
final GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(swaggerOAuthUrl);
return new OAuth(securitySchemaOAuth2, Collections.EMPTY_LIST,
Collections.singletonList(grantType));
}
private List defaultAuth() {
AuthorizationScope[] scopes = new AuthorizationScope[0];
return Collections
.singletonList(new SecurityReference(securitySchemaOAuth2, scopes));
}
private ApiInfo buildApiInfo(String description) {
return new ApiInfoBuilder()
.title(appName)
.description(description)
.version(appVersion)
.contact(new Contact(contactName, contactUrl, contactEmail))
.license(license)
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy