This project is a sample application that integrates React with Spring Boot and Keycloak for authentication. It consists of a backend service built with Spring Boot and a frontend application developed using React.
To get started, clone this repository:
git clone https://github.com/Rajkumardev/react-springboot-keycloak-app.git
cd react-springboot-keycloak-app
react-springboot-keycloak-app
├── backend
│ ├── src
│ │ ├── main
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── keycloakapp
│ │ │ │ ├── controller
│ │ │ │ │ └── HelloController.java
│ │ │ │ ├── config
│ │ │ │ │ └── SecurityConfig.java
│ │ │ │ ├── security
│ │ │ │ │ └── CustomSecurityConfig.java
│ │ │ │ └── KeycloakAppApplication.java
│ │ │ └── resources
│ │ │ ├── application.properties
│ │ │
│ ├── target
│ ├── pom.xml
│ └── README.md
│ ├── target
│ ├── pom.xml
│ └── README.md
├── frontend
│ ├── src
│ │ ├── App.tsx
│ │ ├── index.tsx
│ │ ├── components
│ │ │ ├── Dashboard.tsx
│ │ │ └── Login.tsx
│ │ └── routes
│ │ └── AppRouter.tsx
│ ├── public
│ │ └── index.html
│ ├── package.json
│ ├── tsconfig.json
│ └── README.md
└── README.md
-
Run Keycloak locally or on Docker:
docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:24.0.2 start
MFA Setup Watch this video for a step-by-step guide: Keycloak MFA Setup (YouTube)
-
To access the Keycloak server locally, open
http://localhost:8080
in your browser. Login Screen:
- Navigate to the
backend
directory. - Ensure you have Java and Maven installed.
- Run the following command to start the Spring Boot application:
mvn spring-boot:run
- The backend will be available at
http://localhost:8080
.
- Update the following properties in
application.properties
with your Keycloak server details, realm, and client information:
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/realms/your-releam
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://localhost:8080/realms/your-releam/protocol/openid-connect/certs
- Make sure to replace the placeholders with your actual Keycloak configuration values.
- Navigate to the
frontend
directory. - Ensure you have Node.js and npm installed.
- Install the dependencies by running:
npm install
- Start the React application with:
npm start
- The frontend will be available at
http://localhost:3000
.
To configure Keycloak in your React frontend, update your KeycloakService.ts
(or equivalent) with your Keycloak server details. For example:
typescript
const keycloak = new Keycloak({
url: 'http://localhost:8080',
realm: 'your-releam',
clientId: 'your-client',
});
Replace the url
, realm
, and clientId
values with those matching your Keycloak setup. This ensures your React app can authenticate users via Keycloak.
Front End Login Screen:
Home Page:
Dashboard Page:
Accessing a private API endpoint without a token:
Accessing a public API endpoint without a token:
This project serves as a basic template for integrating React with Spring Boot and Keycloak. You can extend it further by adding more features and functionalities as per your requirements.