Chapter 2. Enable Basic authentication
Enable Basic authentication to require a username and password before users can access your Quarkus application. For more information, see Basic authentication.
2.1. Prerequisites Copy linkLink copied to clipboard!
You have installed at least one extension that provides an
IdentityProviderto verify username and password credentials and map them to aSecurityIdentity. For example:
2.2. Procedure Copy linkLink copied to clipboard!
In the
application.propertiesfile, set thequarkus.http.auth.basicproperty totrue.quarkus.http.auth.basic=true- Configure an identity provider to verify the username and password against your user store. For a production application, configure one of the identity provider extensions listed in the Prerequisites, for example, by storing users in a database with the Jakarta Persistence extension. For more information, see the Identity providers guide.
For testing only: In a non-production environment, you can use the embedded realm provided by the
quarkus-elytron-security-properties-fileextension instead of a production identity provider.To enable authentication for the embedded realm, set the
quarkus.security.users.embedded.enabledproperty totrue.quarkus.security.users.embedded.enabled=trueYou can also configure the required user credentials: user name, password, and roles. For example:
quarkus.http.auth.basic=true quarkus.security.users.embedded.enabled=true quarkus.security.users.embedded.plain-text=true quarkus.security.users.embedded.users.alice=alice quarkus.security.users.embedded.users.bob=bob quarkus.security.users.embedded.roles.alice=admin quarkus.security.users.embedded.roles.bob=userwhere:
-
The user,
alice, hasaliceas their password andadminas their role. -
The user,
bob, hasbobas their password anduseras their role.
-
The user,
2.3. Verification Copy linkLink copied to clipboard!
- Start your application.
Send a request to a secured endpoint without credentials and confirm that the application returns a
401 Unauthorizedresponse:$ curl -i -X GET http://localhost:8080/<your_secured_endpoint> HTTP/1.1 401 Unauthorized WWW-Authenticate: BasicSend the same request with a valid username and password and confirm that the application returns a
200 OKresponse:$ curl -i -X GET -u <username>:<password> http://localhost:8080/<your_secured_endpoint> HTTP/1.1 200 OK
2.4. Next steps Copy linkLink copied to clipboard!
For a more detailed walk-through that shows you how to configure Basic authentication together with Jakarta Persistence for storing user credentials in a database, see the Getting started with Security by using Basic authentication and Jakarta Persistence guide.