Добавил авторизацию JWT для Swagger

This commit is contained in:
Struchkov Mark 2023-02-11 16:56:07 +03:00
parent 5d4970dc3b
commit fe7307ab6c
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
3 changed files with 41 additions and 4 deletions

25
pom.xml
View File

@ -31,6 +31,31 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version> <version>2.0.2</version>
</dependency> </dependency>
<!-- jwt dependencies -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,8 +1,10 @@
package org.sadtech.example.swagger.config; package org.sadtech.example.swagger.config;
import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Contact; import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info; import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
/** /**
* @author upagge 30.12.2020 * @author upagge 30.12.2020
@ -18,6 +20,12 @@ import io.swagger.v3.oas.annotations.info.Info;
) )
) )
) )
@SecurityScheme(
name = "JWT",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
scheme = "bearer"
)
public class OpenApiConfig { public class OpenApiConfig {
} }

View File

@ -2,7 +2,9 @@ package org.sadtech.example.swagger.controller;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import org.sadtech.example.swagger.dto.TypeOperation; import org.sadtech.example.swagger.dto.TypeOperation;
@ -12,7 +14,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Map; import java.util.Map;
@ -33,11 +35,13 @@ public class PointController {
} }
@PostMapping("{key}") @PostMapping("{key}")
@SecurityRequirement(name = "JWT")
@Operation(summary = "Управление баллами", description = "Позволяет удалить или добавить баллы пользователю") @Operation(summary = "Управление баллами", description = "Позволяет удалить или добавить баллы пользователю")
public HttpStatus changePoints( public HttpStatus changePoints(
@PathVariable @NotBlank @Parameter(description = "Идентификатор пользователя") String key, @PathVariable @NotBlank @Parameter(description = "Идентификатор пользователя", example = "key1") String key,
@RequestPart("point") @Min(0) @Parameter(description = "Количество баллов", required = true) Long point, @RequestParam("point") @Min(0) @Parameter(description = "Количество баллов", required = true, example = "10") Long point,
@RequestPart("type") @Parameter(description = "Тип операции", required = true) TypeOperation type @RequestParam("type") @Parameter(description = "Тип операции", required = true) TypeOperation type,
HttpServletRequest request
) { ) {
final UserDto userDto = repository.get(key); final UserDto userDto = repository.get(key);
userDto.setPoints( userDto.setPoints(