replace the securityContext (#14202)

This commit is contained in:
ACHRAF TAITAI 2023-06-10 18:47:47 +02:00 committed by GitHub
parent fec7001cbd
commit 18baf2eed6
1 changed files with 31 additions and 37 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.swaggerjwt.configuration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*; import springfox.documentation.service.*;
@ -16,47 +17,40 @@ import java.util.List;
@Configuration @Configuration
public class SwaggerConfiguration { public class SwaggerConfiguration {
public static final String AUTHORIZATION_HEADER = "Authorization"; public static final String AUTHORIZATION_HEADER = "Authorization";
private ApiInfo apiInfo() { private ApiInfo apiInfo() {
return new ApiInfo("My REST API", 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());
"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 @Bean
public Docket api() { public Docket api() {
return new Docket(DocumentationType.SWAGGER_2) return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.apiInfo(apiInfo()) .securityContexts(Arrays.asList(securityContext()))
.securityContexts(Arrays.asList(securityContext())) .securitySchemes(Arrays.asList(apiKey()))
.securitySchemes(Arrays.asList(apiKey())) .select()
.select() .apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any())
.paths(PathSelectors.any()) .build();
.build(); }
}
private ApiKey apiKey() { private ApiKey apiKey() {
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header"); return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
} }
private SecurityContext securityContext() { private SecurityContext securityContext() {
return SecurityContext.builder() return SecurityContext.builder()
.securityReferences(defaultAuth()) .securityReferences(List.of(defaultAuth()))
.build(); .operationSelector(o -> o.requestMappingPattern()
} .matches("/.*"))
.build();
}
List<SecurityReference> defaultAuth() { private SecurityReference defaultAuth() {
AuthorizationScope authorizationScope return SecurityReference.builder()
= new AuthorizationScope("global", "accessEverything"); .scopes(new AuthorizationScope[0])
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; .reference("JWT")
authorizationScopes[0] = authorizationScope; .build();
return Arrays.asList(new SecurityReference("JWT", authorizationScopes)); }
}
} }