Skip to content

Commit

Permalink
Implement Swagger authentication support using JWT (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
nklimovych authored Jun 8, 2024
1 parent 8463040 commit 7866771
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/main/java/mate/academy/bookstore/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static org.springframework.security.config.Customizer.withDefaults;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import mate.academy.bookstore.security.JwtAuthenticationFilter;
import org.springframework.context.annotation.Bean;
Expand All @@ -22,6 +26,10 @@
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
private static final String BEARER_AUTH = "bearerAuth";
private static final String BEARER = "bearer";
private static final String JWT = "JWT";

private final UserDetailsService userDetailsService;
private final JwtAuthenticationFilter jwtAuthFilter;

Expand Down Expand Up @@ -63,4 +71,17 @@ public AuthenticationManager authenticationManager(
) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

@Bean
public OpenAPI customizeOpenApi() {
return new OpenAPI()
.addSecurityItem(new SecurityRequirement()
.addList(BEARER_AUTH))
.components(new Components()
.addSecuritySchemes(BEARER_AUTH, new SecurityScheme()
.name(BEARER_AUTH)
.type(SecurityScheme.Type.HTTP)
.scheme(BEARER)
.bearerFormat(JWT)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Order Management", description = "Endpoints for managing the order")
@RequiredArgsConstructor
@RestController
@Tag(name = "Order Management", description = "Endpoints for managing the order")
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Shopping Cart Management", description = "Endpoints for managing the shopping cart")
@RequiredArgsConstructor
@RestController
@Tag(name = "Shopping Cart Management", description = "Endpoints for managing the shopping cart")
@RequestMapping("/cart")
@RequiredArgsConstructor
public class ShoppingCartController {
private final ShoppingCartService shoppingCartService;

Expand Down

0 comments on commit 7866771

Please sign in to comment.