Skip to content

Instantly share code, notes, and snippets.

@felipewind
Last active March 20, 2024 20:33
Show Gist options
  • Save felipewind/8d199b2ac9b35abc540cac0ac0505fd9 to your computer and use it in GitHub Desktop.
Save felipewind/8d199b2ac9b35abc540cac0ac0505fd9 to your computer and use it in GitHub Desktop.
Passing JWT token using the swagger-ui
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeIn;
import org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeType;
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement;
import org.eclipse.microprofile.openapi.annotations.security.SecurityScheme;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
@Path("users/{user}/products")
@RequestScoped
@Produces("application/json")
@Consumes("application/json")
@Tag(name = "Products")
@SecurityScheme(securitySchemeName = "Authentication",
description = "JWT token",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "JWT",
in = SecuritySchemeIn.HEADER)
public class ProductResource {
@Inject
JsonWebToken jwt;
@GET
@RolesAllowed({ "User", "Admin" })
@SecurityRequirement(name = "Authentication")
public List<Product> getByUserName(@PathParam("user") String user, @Context SecurityContext ctx) {
return Product.listByUserName(user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment