Quarkus - Security Guide

Quarkus security allows you to define security authorization requirements for your code using annotations and/or configuration, and providesseveral ways to load security authentication information using Quarkus extensions.

Authentication sources

Quarkus supports several sources to load authentication information from. You need to import at least one of the following extensionsin order for Quarkus to know how to find the authentication information to check for authorizations:

Table 1. Security Extensions
ExtensionDescription
quarkus-elytron-security-properties-fileProvides support for simple properties files that can be used for testing security. This supports both embedding user info in application.properties and standalone properties files.
quarkus-elytron-security-jdbcProvides support for authenticating via JDBC.
quarkus-elytron-security-oauth2Provides support for OAuth2 flows using Elytron. This extension will likely be deprecated soon and replaced by a reactive Vert.x version.
quarkus-smallrye-jwtA Microprofile JWT implementation that provides support for authenticating using Json Web Tokens. This also allows you to inject the token and claims into the application as per the MP JWT spec.
quarkus-oidcProvides support for authenticating via an OpenID Connect provider such as Keycloak.
quarkus-keycloak-authorizationProvides support for a policy enforcer using Keycloak Authorization Services.

Please see the linked documents above for details on how to setup the various extensions.

Authenticating via HTTP

Quarkus has two built in authentication mechanisms for HTTP based FORM and BASIC auth. This mechanism is pluggablehowever so extensions can add additional mechanisms (most notably OpenID Connect for Keycloak based auth).

Basic Authentication

To enable basic authentication set quarkus.http.auth.basic=true. You must also have at least one extension installedthat provides a username/password based IdentityProvider, such as Elytron JDBC.

Form Based Authentication

Quarkus provides form based authentication that works in a similar manner to traditional Servlet form based auth. Unliketraditional form authentication the authenticated user is not stored in a HTTP session, as Quarkus does not provideclustered HTTP session support. Instead the authentication information is stored in an encrypted cookie, which canbe read by all members of the cluster (provided they all share the same encryption key).

The encryption key can be set using the quarkus.http.auth.session.encryption-key property, and it must be at least 16 characterslong. This key is hashed using SHA-256 and the resulting digest is used as a key for AES-256 encryption of the cookievalue. This cookie contains a expiry time as part of the encrypted value, so all nodes in the cluster must have theirclocks synchronised. At one minute intervals a new cookie will be generated with an updated expiry time if the sessionis in use.

The following properties can be used to configure form based auth:

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.http.auth.form.enabledIf form authentication is enabledbooleanfalse
quarkus.http.auth.form.login-pageThe login pagestring/login.html
quarkus.http.auth.form.error-pageThe error pagestring/error.html
quarkus.http.auth.form.landing-pageThe landing page to redirect to if there is no saved page to redirect back tostringrequired
quarkus.http.auth.form.timeoutThe inactivity timeoutDurationPT30M
quarkus.http.auth.form.new-cookie-intervalHow old a cookie can get before it will be replaced with a new cookie with an updated timeout. Not that smaller values will result in slightly more server load (as new encrypted cookies will be generated more often), however larger values affect the inactivity timeout as the timeout is set when a cookie is generated. For example if this is set to 10 minutes, and the inactivity timeout is 30m, if a users last request is when the cookie is 9m old then the actual timeout will happen 21m after the last request, as the timeout is only refreshed when a new cookie is generated.DurationPT1M
quarkus.http.auth.form.cookie-nameThe cookie that is used to store the persistent sessionstringquarkus-credential
About the Duration formatThe format for durations uses the standard java.time.Duration format.You can learn more about it in the Duration#parse() javadoc.You can also provide duration values starting with a number.In this case, if the value consists only of a number, the converter treats the value as seconds.Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.

Authorization in REST endpoints and CDI beans using annotations

Quarkus comes with built-in security to allow for Role-Based Access Control (RBAC)based on the common security annotations @RolesAllowed, @DenyAll, @PermitAll on REST endpoints and CDI beans.An example of an endpoint that makes use of both JAX-RS and Common Security annotations to describe and secure its endpoints is given in SubjectExposingResource Example. Quarkus also providesthe io.quarkus.security.Authenticated annotation that will permit any authenticated user to access the resource(equivalent to @RolesAllowed("*")).

SubjectExposingResource Example

  1. import java.security.Principal;
  2. import javax.annotation.security.DenyAll;
  3. import javax.annotation.security.PermitAll;
  4. import javax.annotation.security.RolesAllowed;
  5. import javax.ws.rs.GET;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.core.Context;
  8. import javax.ws.rs.core.SecurityContext;
  9. @Path("subject")
  10. public class SubjectExposingResource {
  11. @GET
  12. @Path("secured")
  13. @RolesAllowed("Tester") (1)
  14. public String getSubjectSecured(@Context SecurityContext sec) {
  15. Principal user = sec.getUserPrincipal(); (2)
  16. String name = user != null ? user.getName() : "anonymous";
  17. return name;
  18. }
  19. @GET
  20. @Path("unsecured")
  21. @PermitAll(3)
  22. public String getSubjectUnsecured(@Context SecurityContext sec) {
  23. Principal user = sec.getUserPrincipal(); (4)
  24. String name = user != null ? user.getName() : "anonymous";
  25. return name;
  26. }
  27. @GET
  28. @Path("denied")
  29. @DenyAll(5)
  30. public String getSubjectDenied(@Context SecurityContext sec) {
  31. Principal user = sec.getUserPrincipal();
  32. String name = user != null ? user.getName() : "anonymous";
  33. return name;
  34. }
  35. }
1This /subject/secured endpoint requires an authenticated user that has been granted the role "Tester" through the use of the @RolesAllowed("Tester") annotation.
2The endpoint obtains the user principal from the JAX-RS SecurityContext. This will be non-null for a secured endpoint.
3The /subject/unsecured endpoint allows for unauthenticated access by specifying the @PermitAll annotation.
4This call to obtain the user principal will return null if the caller is unauthenticated, non-null if the caller is authenticated.
5The /subject/denied endpoint disallows any access regardless of whether the call is authenticated by specifying the @DenyAll annotation.

Authorization of Web Endpoints using configuration

Quarkus has an integrated plugable web security layer. If security is enabled all HTTP requests will have a permissioncheck performed to make sure they are permitted to continue.

Configuration authorization checks are executed before any annotation-based authorization check is done, so bothchecks have to pass for a request to be allowed.

The default implementation allows you to define permissions using config in application.properties. An exampleconfig is shown below:

  1. quarkus.http.auth.policy.role-policy1.roles-allowed=user,admin (1)
  2. quarkus.http.auth.permission.roles1.paths=/roles-secured/*,/other/*,/api/* (2)
  3. quarkus.http.auth.permission.roles1.policy=role-policy1
  4. quarkus.http.auth.permission.permit1.paths=/public/* (3)
  5. quarkus.http.auth.permission.permit1.policy=permit
  6. quarkus.http.auth.permission.permit1.methods=GET
  7. quarkus.http.auth.permission.deny1.paths=/forbidden (4)
  8. quarkus.http.auth.permission.deny1.policy=deny
1This defines a role based policy that allows users with the user and admin roles. This is referenced by later rules
2This is a permission set that references the previously defined policy. roles1 is an arbitrary name, you can call the permission sets whatever you want.
3This permission references the default permit built in policy to allow GET methods to /public. This is actually a no-op in this example, as this request would have been allowed anyway.
4This permission references the built in deny build in policy /forbidden. This is an exact path match as it does not end with *.

Permissions are defined in config using permission sets. These are arbitrarily named permission grouping. Each permissionset must specify a policy that is used to control access. There are three built in policies: deny, permit and authenticated,which permit all, deny all and only allow authenticated users respectively.

It is also possible to define role based policies, as shown in the example. These policies will only allow users with thespecified roles to access the resources.

Matching on paths, methods

Permission sets can also specify paths and methods as a comma separated list. If a path ends with '*' then it is consideredto be a wildcard match and will match all sub paths, otherwise it is an exact match and will only match that specific path:

  1. quarkus.http.auth.permission.permit1.paths=/public/*,/css/*,/js/*,/robots.txt
  2. quarkus.http.auth.permission.permit1.policy=permit
  3. quarkus.http.auth.permission.permit1.methods=GET,HEAD

Matching path but not method

If a request would match one or more permission sets based on the path, but does not match any due to method requirementsthen the request is rejected.

Given the above permission set, GET /public/foo would match both the path and method and thus be allowed,whereas POST /public/foo would match the path but not the method and would thus be rejected.

Matching multiple paths: longest wins

Matching is always done on a longest path basis, less specific permission sets are not considered if a more specific onehas been matched:

  1. quarkus.http.auth.permission.permit1.paths=/public/*
  2. quarkus.http.auth.permission.permit1.policy=permit
  3. quarkus.http.auth.permission.permit1.methods=GET,HEAD
  4. quarkus.http.auth.permission.deny1.paths=/public/forbidden-folder/*
  5. quarkus.http.auth.permission.deny1.policy=deny
Given the above permission set, GET /public/forbidden-folder/foo would match both permission sets' paths,but because it matches the deny1 permission set’s path on a longer match, deny1 will be chosen and the request willbe rejected.

Matching multiple paths: most specific method wins

If a path is registered with multiple permission sets then any permission sets that specify a HTTP method will takeprecedence and permissions sets without a method will not be considered (assuming of course the method matches). In thisinstance, the permission sets without methods will only come into effect if the request method does not match any of thesets with method permissions.

  1. quarkus.http.auth.permission.permit1.paths=/public/*
  2. quarkus.http.auth.permission.permit1.policy=permit
  3. quarkus.http.auth.permission.permit1.methods=GET,HEAD
  4. quarkus.http.auth.permission.deny1.paths=/public/*
  5. quarkus.http.auth.permission.deny1.policy=deny
Given the above permission set, GET /public/foo would match both permission sets' paths,but because it matches the permit1 permission set’s explicit method, permit1 will be chosen and the request willbe accepted. PUT /public/foo on the other hand, will not match the method permissions of permit1 and sodeny1 will be activated and reject the request.

Matching multiple paths and methods: both win

If multiple permission sets specify the same path and method (or multiple have no method) then both permissions have toallow access for the request to proceed. Note that for this to happen both have to either have specified the method, orhave no method, method specific matches take precedence as stated above:

  1. quarkus.http.auth.policy.user-policy1.roles-allowed=user
  2. quarkus.http.auth.policy.admin-policy1.roles-allowed=admin
  3. quarkus.http.auth.permission.roles1.paths=/api/*,/restricted/*
  4. quarkus.http.auth.permission.roles1.policy=user-policy1
  5. quarkus.http.auth.permission.roles2.paths=/api/*,/admin/*
  6. quarkus.http.auth.permission.roles2.policy=admin-policy1
Given the above permission set, GET /api/foo would match both permission sets' paths,so would require both the user and admin roles.

Authenticated representation

For every authenticated resource, you can inject a SecurityIdentity instance to get the authenticated identity information.

In some other contexts you may have other parallel representations of the same information (or parts of it) such as SecurityContextfor JAX-RS or JsonWebToken for JWT.

Configuration

There are two configuration settings that alter the RBAC behavior:

  • quarkus.security.jaxrs.deny-unannotated-endpoints=true|false - if set to true, the access will be denied for all JAX-RS endpoints by default.That is if the security annotations do not define the access control. Defaults to false

  • quarkus.security.deny-unannotated-members=true|false - if set to true, the access will be denied to all CDI methodsand JAX-RS endpoints that do not have security annotations but are defined in classes that contain methods withsecurity annotations. Defaults to false.

Registering Security Providers

When running in native mode the default behavior for Graal native image generation is to only include the main "SUN" providerunless you have enabled SSL, in which case all security providers are registered. If you are not using SSL, then you can selectivelyregister security providers by name using the quarkus.security.users.security-providers property. The following example illustratesconfiguration to register the "SunRsaSign" and "SunJCE" security providers:

Example Security Providers Configuration

  1. quarkus.security.security-providers=SunRsaSign,SunJCE
  2. ...