Quarkus security annotations with example!!

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...

Quarkus provides a number of security annotations that can be used to secure your application. Here are some commonly used security annotations with examples:
The @RolesAllowed annotation is used to specify which security roles are allowed to access a method.
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/secured")
public class SecuredResource {
@GET
@Path("/admin")
@RolesAllowed("admin")
public Response adminOnly() {
// This method can only be accessed by users with the "admin" role
return Response.ok("Hello Admin!").build();
}
@GET
@Path("/user")
@RolesAllowed({"admin", "user"})
public Response userOrAdmin() {
// This method can be accessed by users with either the "admin" or "user" role
return Response.ok("Hello User or Admin!").build();
}
}
In the above example, the adminOnly method can only be accessed by users with the "admin" role, while the userOrAdmin method can be accessed by users with either the "admin" or "user" role.
The @Authenticated annotation is used to specify that a method can only be accessed by authenticated users.
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.auth.inject.SecurityContext;
@Path("/secured")
public class SecuredResource {
@GET
@Path("/authenticated")
@Authenticated
public Response authenticatedOnly(@Context SecurityContext securityContext) {
// This method can only be accessed by authenticated users
String username = securityContext.getUserPrincipal().getName();
return Response.ok("Hello " + username + "!").build();
}
}
In the above example, the authenticatedOnly method can only be accessed by authenticated users. The SecurityContext parameter can be used to retrieve information about the authenticated user, such as their username.
The @PermitAll annotation is used to specify that a method can be accessed by all users, even those who are not authenticated.
import javax.annotation.security.PermitAll;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/public")
public class PublicResource {
@GET
@Path("/hello")
@PermitAll
public Response publicHello() {
// This method can be accessed by all users, even those who are not authenticated
return Response.ok("Hello World!").build();
}
}
In the above example, the publicHello method can be accessed by all users, even those who are not authenticated.
The @DenyAll annotation is used to specify that a method cannot be accessed by any user, even those who are authenticated.
import javax.annotation.security.DenyAll;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/restricted")
public class RestrictedResource {
@GET
@Path("/secret")
@DenyAll
public Response secret() {
// This method cannot be accessed by any user, even those who are authenticated
return Response.status(Response.Status.FORBIDDEN).build();
}
}
In the above example, the secret method cannot be accessed by any user, even those who are authenticated.
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.