From eefcf18757bd975905ee479f75d2d123e5bc8ab0 Mon Sep 17 00:00:00 2001 From: Sallo Szrajbman Date: Tue, 27 Oct 2020 02:12:50 +0000 Subject: [PATCH] [BAEL-4173] Set JWT token with Spring Boot & Swagger UI (#10134) * Hexagonal Architecture with Spring Boot example * Set JWT token with Spring Boot & Swagger UI * Revert "Hexagonal Architecture with Spring Boot example" This reverts commit 412b8690 * [BEAL-4173] Code identation fixes README.md fixes * [BEAL-4173] Removing sout * [BEAL-4173] Fixing identation issue * [BEAL-4173] Fixing identation issue * [BEAL-4173] Removing HttpHeaders parameter (not needed) * Update spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/controller/ClientsRestController.java Committing suggestion Co-authored-by: KevinGilmore Co-authored-by: KevinGilmore --- spring-boot-modules/pom.xml | 1 + .../spring-boot-swagger-jwt/README.md | 2 + .../spring-boot-swagger-jwt/pom.xml | 41 ++++++++++++ .../SpringBootSwaggerUIApplication.java | 13 ++++ .../configuration/SwaggerConfiguration.java | 62 +++++++++++++++++++ .../controller/ClientsRestController.java | 25 ++++++++ .../src/main/resources/application.properties | 0 7 files changed, 144 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger-jwt/README.md create mode 100644 spring-boot-modules/spring-boot-swagger-jwt/pom.xml create mode 100644 spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/SpringBootSwaggerUIApplication.java create mode 100644 spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/configuration/SwaggerConfiguration.java create mode 100644 spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/controller/ClientsRestController.java create mode 100644 spring-boot-modules/spring-boot-swagger-jwt/src/main/resources/application.properties diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index fa70a9f058..0d6e434713 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -64,6 +64,7 @@ spring-boot-security spring-boot-springdoc spring-boot-swagger + spring-boot-swagger-jwt spring-boot-testing spring-boot-vue spring-boot-xml diff --git a/spring-boot-modules/spring-boot-swagger-jwt/README.md b/spring-boot-modules/spring-boot-swagger-jwt/README.md new file mode 100644 index 0000000000..97c76dc431 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-jwt/README.md @@ -0,0 +1,2 @@ +## Relevant Articles: + diff --git a/spring-boot-modules/spring-boot-swagger-jwt/pom.xml b/spring-boot-modules/spring-boot-swagger-jwt/pom.xml new file mode 100644 index 0000000000..d71d7342ce --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-jwt/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + ../ + + + spring-boot-swagger-jwt + 0.1.0-SNAPSHOT + spring-boot-swagger-jwt + jar + + Module For Spring Boot Swagger UI + + + org.springframework.boot + spring-boot-starter-web + + + io.springfox + springfox-boot-starter + 3.0.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/SpringBootSwaggerUIApplication.java b/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/SpringBootSwaggerUIApplication.java new file mode 100644 index 0000000000..e9e5d24b54 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/SpringBootSwaggerUIApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerjwt; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootSwaggerUIApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootSwaggerUIApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/configuration/SwaggerConfiguration.java b/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/configuration/SwaggerConfiguration.java new file mode 100644 index 0000000000..240a21cd21 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/configuration/SwaggerConfiguration.java @@ -0,0 +1,62 @@ +package com.baeldung.swaggerjwt.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +@Configuration +public class SwaggerConfiguration { + + public static final String AUTHORIZATION_HEADER = "Authorization"; + + private ApiInfo apiInfo() { + return new ApiInfo("My REST API", + "Some custom description of API.", + "1.0", + "Terms of service", + new Contact("Sallo Szrajbman", "www.baeldung.com", "salloszraj@gmail.com"), + "License of API", + "API license URL", + Collections.emptyList()); + } + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .securityContexts(Arrays.asList(securityContext())) + .securitySchemes(Arrays.asList(apiKey())) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + + private ApiKey apiKey() { + return new ApiKey("JWT", AUTHORIZATION_HEADER, "header"); + } + + private SecurityContext securityContext() { + return SecurityContext.builder() + .securityReferences(defaultAuth()) + .build(); + } + + List defaultAuth() { + AuthorizationScope authorizationScope + = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + return Arrays.asList(new SecurityReference("JWT", authorizationScopes)); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/controller/ClientsRestController.java b/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/controller/ClientsRestController.java new file mode 100644 index 0000000000..f60ba2448a --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-jwt/src/main/java/com/baeldung/swaggerjwt/controller/ClientsRestController.java @@ -0,0 +1,25 @@ +package com.baeldung.swaggerjwt.controller; + +import java.util.Arrays; +import java.util.List; + +import com.baeldung.swaggerjwt.configuration.SwaggerConfiguration; +import io.swagger.annotations.Api; +import org.springframework.http.HttpHeaders; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.ApiOperation; + +@RestController(value = "/clients") +@Api( tags = "Clients") +public class ClientsRestController { + + @ApiOperation(value = "This method is used to get the clients.") + @GetMapping + public List getClients() { + return Arrays.asList("First Client", "Second Client"); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger-jwt/src/main/resources/application.properties b/spring-boot-modules/spring-boot-swagger-jwt/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2