This is a Spring Boot web application that demonstrates OpenID Connect authentication using Keycloak. The application has two endpoints:
- A public endpoint accessible to everyone
- A protected endpoint accessible only to authenticated users
- Spring Boot 3.1.5
- Spring Security with OpenID Connect
- Thymeleaf templates for the frontend
- Bootstrap 5 for styling
- Home Page: Publicly accessible. Greets authenticated users by name.
- User Page: Protected page accessible only to authenticated users. Displays user information.
- Login Page: Provides a link to authenticate with Keycloak.
- Java 17 or higher
- Gradle
- Keycloak server (for OpenID Connect authentication)
- Clone the repository
- Configure Keycloak (see below)
- Update
application.properties
with your Keycloak settings - Run the application:
./gradlew bootRun
- Access the application at http://localhost:8080
- Install and start Keycloak
- Create a new realm (e.g.,
webapp2-realm
) - Create a new client:
- Client ID:
webapp2-client
- Client Protocol:
openid-connect
- Access Type:
confidential
- Valid Redirect URIs:
http://localhost:8080/*
- Client ID:
- After creating the client, go to the "Credentials" tab to get the client secret
- Create a user in the realm
- Update the
application.properties
file with your Keycloak settings:spring.security.oauth2.client.registration.keycloak.client-id=webapp2-client spring.security.oauth2.client.registration.keycloak.client-secret=your-client-secret spring.security.oauth2.client.provider.keycloak.issuer-uri=http://localhost:8180/auth/realms/webapp2-realm
Main.java
: Spring Boot application entry pointHomeController.java
: Controller for the public endpointUserController.java
: Controller for the protected endpointSecurityConfig.java
: Security configuration for OpenID Connecttemplates/home.html
: Home page templatetemplates/user/home.html
: User page templatetemplates/login.html
: Login page templateapplication.properties
: Application configuration
- The application uses Spring Security's OAuth2 client support for OpenID Connect authentication
- Thymeleaf templates use Bootstrap 5 for styling
- The application includes debug logging for Spring Security to help with troubleshooting