SlideShare a Scribd company logo
Step 1: Set Up Docker Environment
Create a docker-compose.yml file to define the services for Next.js, Django, PostgreSQL, and
Keycloak.
# docker-compose.yml
version: '3.8'
services:
postgres:
image: postgres:13
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: mydatabase
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- app-network
django:
build: ./django
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./django:/app
ports:
- "8000:8000"
depends_on:
- postgres
networks:
- app-network
nextjs:
build: ./nextjs
command: npm run dev
volumes:
- ./nextjs:/app
ports:
- "3000:3000"
depends_on:
- django
networks:
- app-network
keycloak:
image: jboss/keycloak:16.1.1
environment:
DB_VENDOR: postgres
DB_ADDR: postgres
DB_DATABASE: keycloak
DB_USER: keycloak
DB_PASSWORD: keycloak
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
ports:
- "8080:8080"
depends_on:
- postgres
networks:
- app-network
volumes:
postgres_data:
networks:
app-network:
driver: bridge
Step 2: Configure Keycloak
1. Access Keycloak at http://localhost:8080.
2. Log in with the admin credentials (admin/admin).
3. Create a new realm (e.g., myrealm).
4. Set up a client for your Next.js application:
o Go to Clients > Create.
o Set Client ID to nextjs-app.
o Set Client Protocol to openid-connect.
o Set Root URL to http://localhost:3000.
5. Configure the client:
o Set Valid Redirect URIs to http://localhost:3000/*.
o Set Web Origins to http://localhost:3000.
6. Add a mapper for user roles:
o Go to Mappers > Create.
o Set Name to roles.
o Set Mapper Type to User Role.
o Set Token Claim Name to roles.
Step 3: Integrate Keycloak with Active Directory
1. In Keycloak, go to User Federation.
2. Add a new LDAP provider:
o Set Vendor to Active Directory.
o Set Connection URL to your AD server (e.g., ldap://ad.example.com:389).
o Set Users DN to the base DN for users (e.g., cn=Users,dc=example,dc=com).
o Set Bind Type to simple.
o Set Bind DN to the admin user DN (e.g., cn=admin,cn=Users,dc=example,dc=com).
o Set Bind Credentials to the admin password.
3. Sync users from AD to Keycloak.
Step 4: Integrate Keycloak with Next.js
1. Install the required packages in your Next.js app:
# npm install @react-keycloak/keycloak keycloak-js
2. Create a Keycloak configuration file (keycloak.js):
import Keycloak from 'keycloak-js';
const keycloak = new Keycloak({
url: 'http://localhost:8080/auth',
realm: 'myrealm',
clientId: 'nextjs-app',
});
export default keycloak;
3. Wrap your Next.js app with the Keycloak provider:
import { ReactKeycloakProvider } from '@react-keycloak/keycloak';
import keycloak from './keycloak';
function MyApp({ Component, pageProps }) {
return (
<ReactKeycloakProvider authClient={keycloak}>
<Component {...pageProps} />
</ReactKeycloakProvider>
);
}
export default MyApp;
Step 5: Integrate Keycloak with Django
1. Install the required packages:
# pip install django-keycloak
2. Add django_keycloak to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...
'django_keycloak',
]
3. Configure Keycloak in settings.py:
KEYCLOAK_CONFIG = {
'SERVER_URL': 'http://localhost:8080/auth',
'REALM': 'myrealm',
'CLIENT_ID': 'django-app',
'CLIENT_SECRET_KEY': 'your-client-secret',
}
4. Use Keycloak authentication in your views:
from django_keycloak.auth import KeycloakAuthentication
class MyView(APIView):
authentication_classes = [KeycloakAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request):
return Response("Authenticated!")
Step 6: Run the Docker Containers
1. Build and start the containers:
# docker-compose up –build
2. Access the applications:
o Next.js: http://localhost:3000
o Django: http://localhost:8000
o Keycloak: http://localhost:8080
Step 7: Test the Integration
1. Log in to the Next.js app using AD credentials via Keycloak.
2. Verify that the Django app authenticates users via Keycloak.

More Related Content

PDF
Storytelling For The Web: Integrate Storytelling in your Design Process
PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
PDF
powervault-md3600f_setup guide_en-us.pdf
DOCX
Setting Up Harbor Docker Registry in ubuntu
DOCX
Kafk a with zoo keeper setup documentation
DOCX
terraform_document installation on linux
DOCX
Docker containers migration setup in linux
PPTX
Storytelling For The Web: Integrate Storytelling in your Design Process
2024 Trend Updates: What Really Works In SEO & Content Marketing
powervault-md3600f_setup guide_en-us.pdf
Setting Up Harbor Docker Registry in ubuntu
Kafk a with zoo keeper setup documentation
terraform_document installation on linux
Docker containers migration setup in linux

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Spectroscopy.pptx food analysis technology
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Encapsulation_ Review paper, used for researhc scholars
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
“AI and Expert System Decision Support & Business Intelligence Systems”
sap open course for s4hana steps from ECC to s4
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectroscopy.pptx food analysis technology
Ad
Ad

To integrate Active Directory with keyclock

  • 1. Step 1: Set Up Docker Environment Create a docker-compose.yml file to define the services for Next.js, Django, PostgreSQL, and Keycloak. # docker-compose.yml version: '3.8' services: postgres: image: postgres:13 environment: POSTGRES_USER: myuser POSTGRES_PASSWORD: mypassword POSTGRES_DB: mydatabase volumes: - postgres_data:/var/lib/postgresql/data networks: - app-network django: build: ./django command: python manage.py runserver 0.0.0.0:8000 volumes: - ./django:/app ports: - "8000:8000" depends_on: - postgres networks: - app-network nextjs: build: ./nextjs command: npm run dev volumes: - ./nextjs:/app ports: - "3000:3000" depends_on: - django networks: - app-network keycloak: image: jboss/keycloak:16.1.1 environment: DB_VENDOR: postgres DB_ADDR: postgres DB_DATABASE: keycloak DB_USER: keycloak DB_PASSWORD: keycloak
  • 2. KEYCLOAK_USER: admin KEYCLOAK_PASSWORD: admin ports: - "8080:8080" depends_on: - postgres networks: - app-network volumes: postgres_data: networks: app-network: driver: bridge Step 2: Configure Keycloak 1. Access Keycloak at http://localhost:8080. 2. Log in with the admin credentials (admin/admin). 3. Create a new realm (e.g., myrealm). 4. Set up a client for your Next.js application: o Go to Clients > Create. o Set Client ID to nextjs-app. o Set Client Protocol to openid-connect. o Set Root URL to http://localhost:3000. 5. Configure the client: o Set Valid Redirect URIs to http://localhost:3000/*. o Set Web Origins to http://localhost:3000. 6. Add a mapper for user roles: o Go to Mappers > Create. o Set Name to roles. o Set Mapper Type to User Role. o Set Token Claim Name to roles. Step 3: Integrate Keycloak with Active Directory 1. In Keycloak, go to User Federation. 2. Add a new LDAP provider: o Set Vendor to Active Directory.
  • 3. o Set Connection URL to your AD server (e.g., ldap://ad.example.com:389). o Set Users DN to the base DN for users (e.g., cn=Users,dc=example,dc=com). o Set Bind Type to simple. o Set Bind DN to the admin user DN (e.g., cn=admin,cn=Users,dc=example,dc=com). o Set Bind Credentials to the admin password. 3. Sync users from AD to Keycloak. Step 4: Integrate Keycloak with Next.js 1. Install the required packages in your Next.js app: # npm install @react-keycloak/keycloak keycloak-js 2. Create a Keycloak configuration file (keycloak.js): import Keycloak from 'keycloak-js'; const keycloak = new Keycloak({ url: 'http://localhost:8080/auth', realm: 'myrealm', clientId: 'nextjs-app', }); export default keycloak; 3. Wrap your Next.js app with the Keycloak provider: import { ReactKeycloakProvider } from '@react-keycloak/keycloak'; import keycloak from './keycloak'; function MyApp({ Component, pageProps }) { return ( <ReactKeycloakProvider authClient={keycloak}> <Component {...pageProps} /> </ReactKeycloakProvider> ); } export default MyApp; Step 5: Integrate Keycloak with Django 1. Install the required packages: # pip install django-keycloak 2. Add django_keycloak to your INSTALLED_APPS in settings.py: INSTALLED_APPS = [ ... 'django_keycloak', ]
  • 4. 3. Configure Keycloak in settings.py: KEYCLOAK_CONFIG = { 'SERVER_URL': 'http://localhost:8080/auth', 'REALM': 'myrealm', 'CLIENT_ID': 'django-app', 'CLIENT_SECRET_KEY': 'your-client-secret', } 4. Use Keycloak authentication in your views: from django_keycloak.auth import KeycloakAuthentication class MyView(APIView): authentication_classes = [KeycloakAuthentication] permission_classes = [IsAuthenticated] def get(self, request): return Response("Authenticated!") Step 6: Run the Docker Containers 1. Build and start the containers: # docker-compose up –build 2. Access the applications: o Next.js: http://localhost:3000 o Django: http://localhost:8000 o Keycloak: http://localhost:8080 Step 7: Test the Integration 1. Log in to the Next.js app using AD credentials via Keycloak. 2. Verify that the Django app authenticates users via Keycloak.