[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 <kpg102@gmail.com>

Co-authored-by: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
Sallo Szrajbman 2020-10-27 02:12:50 +00:00 committed by GitHub
parent 4a36ad4b79
commit 731f7f8001
7 changed files with 144 additions and 0 deletions

View File

@ -64,6 +64,7 @@
<module>spring-boot-security</module> <module>spring-boot-security</module>
<module>spring-boot-springdoc</module> <module>spring-boot-springdoc</module>
<module>spring-boot-swagger</module> <module>spring-boot-swagger</module>
<module>spring-boot-swagger-jwt</module>
<module>spring-boot-testing</module> <module>spring-boot-testing</module>
<module>spring-boot-vue</module> <module>spring-boot-vue</module>
<module>spring-boot-xml</module> <module>spring-boot-xml</module>

View File

@ -0,0 +1,2 @@
## Relevant Articles:

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>spring-boot-swagger-jwt</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>spring-boot-swagger-jwt</name>
<packaging>jar</packaging>
<description>Module For Spring Boot Swagger UI</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope
= new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
}
}

View File

@ -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<String> getClients() {
return Arrays.asList("First Client", "Second Client");
}
}