A simple and secure Spring Boot URL Shortener that converts long URLs into short codes and redirects users to the original link.
Built using Spring Boot, Spring Security, and PostgreSQL, this project demonstrates backend development concepts like REST APIs, database persistence, authentication, and URL redirection.
Think of it as a mini Bit.ly built with Java.
🔗 Convert long URLs into short URLs
⚡ Instant redirection using short code
👤 User registration with encrypted passwords
🛡 Basic security using Spring Security
🗄 Persistent storage with PostgreSQL
📦 Clean layered architecture
| Technology | Purpose |
|---|---|
| ☕ Java | Programming language |
| 🌱 Spring Boot | Backend framework |
| 🔐 Spring Security | Authentication |
| 🗃 Spring Data JPA | Database interaction |
| 🐘 PostgreSQL | Database |
| 📦 Maven | Dependency management |
The application follows a layered backend architecture.
flowchart LR
Client --> Controller
Controller --> Service
Service --> Repository
Repository --> Database
Handles HTTP requests such as registering users and redirecting URLs.
Example: UrlsShortController
Contains business logic like generating short codes and retrieving URLs.
Example: UrlService
Handles database operations using Spring Data JPA.
Example: UrlRepository
Stores user accounts and shortened URLs.
url-shortener
│
├── controller
│ └── UrlsShortController.java
│
├── service
│ └── UrlService.java
│
├── repository
│ ├── UrlRepository.java
│ └── UserRepository.java
│
├── model
│ ├── UrlInfo.java
│ └── User.java
│
├── security
│ └── SecurityConfig.java
│
└── UrlShortApplication.java
1️⃣ User submits a long URL
2️⃣ Backend generates a unique short code
3️⃣ Short code is stored in database
4️⃣ Visiting the short code redirects to the original URL
Example:
Original URL
https://www.example.com/very/long/url
Short URL
http://localhost:8080/abc123
| Method | Endpoint | Description |
|---|---|---|
| POST | /register | Register a new user |
| Method | Endpoint | Description |
|---|---|---|
| GET | /{shortCode} | Redirect to original URL |
Example:
GET /abc123
Redirects to:
https://original-url.com
POST /register
Content-Type: application/json
{
"username": "cos",
"password": "12345"
}
GET /abc123
If the short code exists, the user is redirected to the original URL.
Short codes are generated using UUID-based random values.
Example implementation:
UUID.randomUUID().toString().substring(0,6)
This produces short codes like:
a8f2c1
d9b321
x12ac9
git clone https://github.com/yourusername/url-shortener.git
cd url-shortener
Update application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/urlshort
spring.datasource.username=youruser
spring.datasource.password=yourpassword
mvn spring-boot:run
Server runs at
http://localhost:8080
Use Postman or browser.
Example test:
http://localhost:8080/abc123
If the short code exists, the browser redirects to the original URL.
🚀 Custom short URLs
📊 URL analytics (click tracking)
🕒 Expiring links
🔐 JWT authentication
📜 Swagger API documentation
✔ Building REST APIs with Spring Boot
✔ Implementing URL redirection logic
✔ Working with relational databases using JPA
✔ Secure password storage using BCrypt
✔ Designing backend services with layered architecture
⭐ If you like this project, consider giving it a star on GitHub.