This project demonstrates how to implement OAuth2 authentication in a Spring Boot application using custom login pages with JTE (Java Template Engine) and Tailwind CSS. It includes both traditional form login and OAuth2 login with Google and GitHub.
Clone the repository
git clone <repository-url>
cd spring-boot-oauth-demo
Configure OAuth credentials (see OAuth Setup sections below)
Set environment variables
export GOOGLE_CLIENT_ID=your_google_client_id
export GOOGLE_CLIENT_SECRET=your_google_client_secret
export GITHUB_CLIENT_ID=your_github_client_id
export GITHUB_CLIENT_SECRET=your_github_client_secret
Run the application
mvn spring-boot:run
Visit http://localhost:8080
The application comes with a default user for testing:
admin
admin123
Go to Google Cloud Console
Create a new project or select an existing one
Configure the OAuth consent screen:
Create OAuth2 credentials:
Add these URLs:
Authorized JavaScript origins:
http://localhost:8080
Authorized redirect URIs:
http://localhost:8080/login/oauth2/code/google
Click "New OAuth App"
Fill in the application details:
Application name: Your App Name
Homepage URL: http://localhost:8080
Authorization callback URL: http://localhost:8080/login/oauth2/code/github
Register the application
Note your client ID and generate a client secret
Create or update application.yml
:
spring:
security:
oauth2:
client:
registration:
google:
client-id: ${GOOGLE_CLIENT_ID}
client-secret: ${GOOGLE_CLIENT_SECRET}
scope:
- email
- profile
github:
client-id: ${GITHUB_CLIENT_ID}
client-secret: ${GITHUB_CLIENT_SECRET}
scope:
- user:email
- read:user
src/
main/
java/
com.example/
SecurityConfig.java # Spring Security configuration
LoginController.java # Login handling
DashboardController.java # Dashboard pages
resources/
application.yml # Application configuration
jte/
layout/
default.jte # Base template
pages/
login.jte # Login page
dashboard.jte # Dashboard page
home.jte # Home page
<dependencies>
<!-- Spring Boot Starters -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<!-- JTE Template Engine -->
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte-spring-boot-starter</artifactId>
<version>3.1.9</version>
</dependency>
</dependencies>
Redirect URI Mismatch
http://localhost:8080/login/oauth2/code/google
http://localhost:8080/login/oauth2/code/github
Authentication Errors
Login Page Not Loading
Authentication Not Working
Production Deployment
OAuth Provider Setup