Commit ca218337 authored by Matija Obreza's avatar Matija Obreza

Enabled Swagger UI

parent 2f4814cb
......@@ -58,7 +58,7 @@
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
<junit.version>4.12</junit.version>
<application.blocks.version>1.2-SNAPSHOT</application.blocks.version>
<application.blocks.version>1.2</application.blocks.version>
<commons.beanutils.version>1.9.2</commons.beanutils.version>
<commons.collections.version>3.2.1</commons.collections.version>
<commons.fileupload.version>1.3.1</commons.fileupload.version>
......@@ -96,7 +96,8 @@
<jackson.version>2.6.4</jackson.version>
<jaxb-api.version>2.2.12</jaxb-api.version>
<liquibase.version>3.5.3</liquibase.version>
<swagger.version>2.7.0</swagger.version>
<!--Container -->
<jetty.version>9.3.6.v20151106</jetty.version>
......@@ -296,6 +297,12 @@
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- App blocks -->
<dependency>
<groupId>org.genesys-pgr</groupId>
......
......@@ -71,6 +71,10 @@ public class SpringServletConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/html/**").addResourceLocations("/html/");
// Swagger
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
......
package org.genesys2.spring.config;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static springfox.documentation.builders.PathSelectors.ant;
import static springfox.documentation.builders.PathSelectors.regex;
import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.AuthorizationCodeGrantBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.ClientCredentialsGrant;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResourceOwnerPasswordCredentialsGrant;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.service.TokenEndpoint;
import springfox.documentation.service.TokenRequestEndpoint;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${base.hostname}")
private String hostName;
@Value("${build.version}")
private String version;
public SwaggerConfig() {
System.err.println("Making SwaggerConfig!");
}
/**
* Every Docket bean is picked up by the swagger-mvc framework - allowing for
* multiple swagger groups i.e. same code base multiple swagger resource
* listings.
*
* @return Docket
*/
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2).host(hostName).protocols(newHashSet("http", "https"))
// Paths
.select().paths(apiPaths()).build()
// General info
.apiInfo(apiInfo())
// Security
.securitySchemes(newArrayList(oauthClientCredentials(), oauthAuthorizationCode(), oauthUserPassword())).securityContexts(newArrayList(securityContext()));
}
private Predicate<String> apiPaths() {
return regex("/api/v0/.*");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Genesys API v0").description("This is the description of the API to the Genesys").termsOfServiceUrl(
"https://www.genesys-pgr.org/content/terms-and-conditions").contact(contact()).license("Apache License Version 2.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.version(version).build();
}
@Bean
Contact contact() {
return new Contact("Genesys Helpdesk", "https://www.genesys-pgr.org", "helpdesk@genesys-pgr.org");
}
// @Bean
// SecurityScheme basic() {
// return new BasicAuth("login");
// }
@Bean
SecurityScheme oauthAuthorizationCode() {
return new OAuthBuilder().name("Other clients").grantTypes(newArrayList(new AuthorizationCodeGrantBuilder()
// token request
.tokenRequestEndpoint(new TokenRequestEndpoint("//" + hostName + "/oauth/authorize", "client_id", "client_secret"))
// token endpoint
.tokenEndpoint(new TokenEndpoint("//" + hostName + "/oauth/token", "foo"))
// do
.build())).scopes(newArrayList(scopes())).build();
}
@Bean
SecurityScheme oauthClientCredentials() {
return new OAuthBuilder().name("OAuth Client Credentials").grantTypes(newArrayList(new ClientCredentialsGrant("//" + hostName + "/oauth/token"))).scopes(newArrayList(scopes()))
.build();
}
@Bean
SecurityScheme oauthUserPassword() {
return new OAuthBuilder().name("Resource Owner").grantTypes(newArrayList(new ResourceOwnerPasswordCredentialsGrant("//" + hostName + "/oauth/token"))).scopes(newArrayList(scopes()))
.build();
}
@Bean
SecurityContext securityContext() {
final SecurityReference securityReference = SecurityReference.builder().reference("oauth2").scopes(scopes()).build();
return SecurityContext.builder().securityReferences(newArrayList(securityReference)).forPaths(ant("/api/v0/.*")).build();
}
AuthorizationScope[] scopes() {
return new AuthorizationScope[] { new AuthorizationScope("write", "modify data in your account"), new AuthorizationScope("read", "read your data") };
}
}
......@@ -29,6 +29,7 @@ log4j.rootLogger=error, stdout
log4j.category.org.genesys2=warn
log4j.category.org.genesys=warn
log4j.category.liquibase=debug
#log4j.category.springfox=trace
#log4j.category.org.genesys2.server.servlet.controller=debug
#log4j.category.org.hibernate.cfg.Configuration=debug
#log4j.category.org.hibernate.search=debug
......
......@@ -23,6 +23,9 @@
<mime-type>text/html</mime-type>
<mapping path="/html/*" exclude="true" />
<mapping path="/swagger-ui.html*" exclude="true" />
<mapping path="/swagger-resources*" exclude="true" />
<mapping path="/webjars/*" exclude="true" />
<mapping path="/index.html" exclude="true" />
<mapping path="*/ajax/*" exclude="true" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment