Enforcing Web Security
During this scenario, we will use the Devprime stack’s security adapter to enable security settings so that when there is an access request, user authentication is required on a centralized Keycloak basis.
Cheklist and preperation of the initial environment:
- Open an account on Devprime Platform and purchase a Developer/Enterprise) license.
- Install an updated version of .NET (Linux, macOS, and Windows)
- Install and/or update Visual Studio Code and/or Visual Studio 2023 Community / Professional / Enterprise.
- Install and/or update docker (For Windows, use WSL2).
- Initialize the MongoDB and RabbitMQ containers in docker and add the queue ‘orderevents’ in RabbitMQ.
- Install and activate the latest version of the Devprime CLI.
- Create a folder for your projects and set read and write permissions.
- See the article “Creating the first microservice” to explore getting started with the Devprime platform.
- Create a Host account on Auth0.com.
Creating a microservice to use in the example
The first step is to create a new microservice that we can use as a template to publish to the host. The name of this microservice will be set to “ms-order”, as demonstrated in the command below.
- Building the first microservice
dp new ms-order --stream rabbitmq --state mongodb
- Adding an example business rule “Order”
dp marketplace order
- Initializing and accelerating microservice deployments
dp init
After the creation of the new microservice, enter the “ms-order” project folder and you will be able to view all the implementations by Visual Studio Code, as demonstrated in the article related to creation of the first microservice.
After completion, you can run the microservice. Then finish.
.\run.ps1 ou ./run.sh (Linux, macOS)
Applying Keycloak Settings in the Microservice
The security settings on the Devprime platform are applied in the Security Adapter which in the local environment is in the file “src/App/appsettings.json” and at that point we will apply
the same data obtained in the portal to Auth0.
Use the Domain, ClientId, ClientSecret values obtained in your configuration in the portal
of Auth0. In this scenario, we’ll use EnableOIDC.
By Visual Studio Code:
code src/App/appsettings.json
|
|
Creating a web interface to use in the example
Adding web template for use in the demo. This will create a few web pages for us to use in the demonstration.
dp add web login
After running it, we will have a new endpoint called “/private” that already has the “Authorize” attribute necessary to indicate that that url requires authentication. The excerpt from the file below “src/Adapters/Web/Pages/Account.cs” demonstrates this scenario
with the URLs /private, /login, and /logout.
To view from Visual Studio Code:
code src/Adapters/Web/Pages/Account.cs
|
|
Run the application and open https://localhost:5001 to view the new links added.
Next Steps:
You’ve secured a web application using Keycloak and a microservice using the Devprime platform. Congratulations🚀🚀🚀
Last modified April 11, 2024 (cc33f7e6)