Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

spring swagger ui login oauth2

@Configuration
@EnableSwagger2
public class SpringFoxConfig implements WebMvcConfigurer {

	private String accessTokenUri = "http://localhost:8080/oauth/token";

	@Bean
	public Docket api() {

		return new Docket(DocumentationType.SWAGGER_2).select()
				.apis(RequestHandlerSelectors.basePackage("com.gft.desafioapi")).paths(PathSelectors.any()).build()
				.apiInfo(apiInfo())
				.tags(new Tag("Fornecedores", "Gerencia os fornecedores"), new Tag("Clientes", "Gerencia os clientes"),
						new Tag("Produtos", "Gerencia os produtos"), new Tag("Vendas", "Gerencia as vendas"))
				.securityContexts(Collections.singletonList(securityContext()))
				.securitySchemes(Arrays.asList(securitySchema()));
	}

	private OAuth securitySchema() {

		List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
		authorizationScopeList.add(new AuthorizationScope("read", "read all"));

		List<GrantType> grantTypes = new ArrayList<>();
		GrantType passwordCredentialsGrant = new ResourceOwnerPasswordCredentialsGrant(accessTokenUri);

		grantTypes.add(passwordCredentialsGrant);

		return new OAuth("oauth2", authorizationScopeList, grantTypes);

	}

	private SecurityContext securityContext() {
		return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.ant("/api/**"))
				.build();
	}

	private List<SecurityReference> defaultAuth() {

		final AuthorizationScope[] authorizationScopes = { new AuthorizationScope("read", "read all") };

		return Collections.singletonList(new SecurityReference("oauth2", authorizationScopes));
	}

	@Bean
	public SecurityConfiguration securityInfo() {
		return new SecurityConfiguration("admin", "admin", "", "", "Bearer access token", ApiKeyVehicle.HEADER,
				HttpHeaders.AUTHORIZATION, "");
	}

	public ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("GFT - Desafio API")
				.description("API construida para o desafio do programa START GFT").version("1")
				.contact(new Contact("GFT", "https://www.gft.com", "lucas.pedroso@gft.com")).build();
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: enhanced for loop java 
Java :: Java Hasmap Remove Elements 
Java :: Java char Keyword 
Java :: java constructor example 
Java :: recursion java fibonacci 
Java :: Exception handling using try...catch 
Java :: contain java 
Java :: swap two nodes of a linked list 
Java :: java 8 lambda comparator 
Java :: authentication in spring boot 
Java :: instantiation in java 
Java :: get the average of an array in java 
Java :: pass data to layout from navigation android studio 
Java :: java crypto ke random 
Java :: convert code from kotlin to java 
Java :: reading 2d array in java 
Java :: Java program to swap two numbers using bitwise xor operator 
Java :: what is a callable in java 
Java :: Java extends and implements clause 
Java :: MOST COMPLEX NAME OF CONSUMER 
Java :: reactive kafka 
Java :: java Least prime factor of numbers till n 
Java :: grava 
Java :: copy and deletion of div by pressing button in javasript 
Java :: adding prefix zeros 
Java :: int[] left = Arrays.copyOfRange(arr, l, m + 1); 
Java :: ternario java 
Java :: java preprocessor example 
Java :: spigot self cancelling task 
Java :: java file and stream 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =