Skip to content

Instantly share code, notes, and snippets.

@george-hawkins
Created June 15, 2018 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save george-hawkins/e1d5786f409ac9f73fd88f3e8ecf50ae to your computer and use it in GitHub Desktop.
Save george-hawkins/e1d5786f409ac9f73fd88f3e8ecf50ae to your computer and use it in GitHub Desktop.
package bookmarks
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.bind.annotation.RequestMethod
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.builders.ResponseMessageBuilder
import springfox.documentation.schema.ModelRef
import springfox.documentation.service.AuthorizationScope
import springfox.documentation.service.Contact
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
@Configuration
@EnableSwagger2
class SwaggerConfig {
@Value("\${app.client.id}")
private val clientId: String? = null
@Value("\${app.client.secret}")
private val clientSecret: String? = null
@Value("\${host.full.dns.auth.link}")
private val authLink: String? = null
@Bean
fun api2(): Docket {
fun responseMessage(code: Int, message: String) =
ResponseMessageBuilder().code(code).message(message).responseModel(ModelRef("Result")).build()
val messages = listOf(
responseMessage(500, "500 message"),
responseMessage(401, "Unauthorized"),
responseMessage(406, "Not Acceptable"))
val docket = Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
docket
.securitySchemes(listOf(securitySchema()))
.securityContexts(listOf(securityContext()))
.pathMapping("/")
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.globalResponseMessage(RequestMethod.GET, messages)
.globalResponseMessage(RequestMethod.POST, messages)
return docket
}
private val authorizationScopes = arrayOf(
AuthorizationScope("read", "read all"),
AuthorizationScope("trust", "trust all"),
AuthorizationScope("write", "write all"))
private fun securitySchema(): OAuth {
val grantTypes = listOf(ResourceOwnerPasswordCredentialsGrant(authLink!! + "/oauth/token"))
return OAuth("oauth2schema", authorizationScopes.asList(), grantTypes)
}
private fun securityContext(): SecurityContext {
val defaultAuth = listOf(SecurityReference("oauth2schema", authorizationScopes))
return SecurityContext.builder()
.securityReferences(defaultAuth)
.forPaths(PathSelectors.ant("/user/**"))
.build()
}
@Bean
fun securityInfo(): SecurityConfiguration = SecurityConfigurationBuilder.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.build()
private fun apiInfo() = ApiInfoBuilder()
.title("My API title")
.termsOfServiceUrl("https://www.example.com/api")
.contact(Contact("Hasson", "http://www.example.com", "hasson@example.com"))
.license("Open Source")
.licenseUrl("https://www.example.com")
.version("1.0.0")
.build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment