<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph" -->

---
title: Spring Boot: Build URL Shortener Application - Part 8:...
description: A step-by-step continuation of a Spring Boot URL shortener build walks through adding private short URLs and custom expiration for authenticated users. It...
canonical: https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Spring Boot: Build URL Shortener Application - Part 8: Private Short URLs for Authenticated Users | daily.dev
og:description: A step-by-step continuation of a Spring Boot URL shortener build walks through adding private short URLs and custom expiration for authenticated users. It...
og:url: https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph
og:image: https://api.daily.dev/og/posts/LLbAxvJph.png
og:image:alt: Spring Boot: Build URL Shortener Application - Part 8: Private Short URLs for Authenticated Users
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Spring Boot: Build URL Shortener Application - Part 8: Private Short URLs for Authenticated Users

**[SivaLabs \(YouTube\)](https://daily.dev/sources/sivalabs-youtube)** · 15 min read · 0 upvotes · 0 comments

## Summary

A step-by-step continuation of a Spring Boot URL shortener build walks through adding private short URLs and custom expiration for authenticated users. It covers updating the form and command objects to conditionally accept privacy and expiration fields, associating created short URLs with a logged-in user's ID, and fixing an access-control bug where private URLs could be accessed without login. The redirect logic is updated to check ownership before allowing access to private URLs, and the video previews upcoming work on pagination for the list of public URLs.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=fHs-ueL3wm0>

## Questions this post answers

### How do I restrict access to a private short URL so only the owner can use it in Spring Boot?

Check the current logged-in user's ID against the short URL's created-by field before redirecting. When a short URL is marked private, compare its owner ID to the requesting user's ID using a null-safe equals check; if they don't match (or no user is logged in), return an empty optional instead of redirecting, which surfaces as a not-found error.

_daily.dev surfaces implementation walkthroughs like this for developers building access-controlled features in Spring Boot._

### How can I let logged-in users set a custom expiration date for a short URL while defaulting guests to a fixed expiry?

Add an optional expirationInDays field (min 1, max 365) to the form and command object, shown only when the user is authenticated. In the service layer, check whether the command's user ID is non-null; if so, apply the custom expiration value when provided, otherwise fall back to the default 30-day expiration used for guest users.

_developers designing tiered features by auth status can track patterns like this via daily.dev._

## Similar posts on daily.dev

- [System Design Drill Day 14: URL Shortener and Unique ID Generation](https://daily.dev/posts/system-design-drill-day-14-url-shortener-and-unique-id-generation-fwoorwvvg) · C\# Corner · 14 upvotes · 1 comments
- [Secure Your APIs: OAuth2 and JWT for Beginners](https://daily.dev/posts/secure-your-apis-oauth2-and-jwt-for-beginners-hh8guukwi) · JetBrains · 5 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#authentication](https://daily.dev/tags/authentication), [#spring-boot](https://daily.dev/tags/spring-boot), [#authorization](https://daily.dev/tags/authorization), [#spring-security](https://daily.dev/tags/spring-security)

[View this post on daily.dev](https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Spring Boot: Build URL Shortener Application - Part 8: Private Short URLs for Authenticated Users","url":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph"},"datePublished":"2026-09-27T07:26:39.656Z","dateModified":"2026-09-27T07:32:19.980Z","description":"A step-by-step continuation of a Spring Boot URL shortener build walks through adding private short URLs and custom expiration for authenticated users. It...","image":"https://i.ytimg.com/vi/fHs-ueL3wm0/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/fHs-ueL3wm0/sddefault.jpg","isAccessibleForFree":true,"articleSection":"SivaLabs (YouTube)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SivaLabs (YouTube)","logo":"https://media.daily.dev/image/upload/s--8ATVo4an--/f_auto,q_auto/v1790493987/logos/sivalabs-youtube?_a=BAMAMicg0","url":"https://daily.dev/sources/sivalabs-youtube"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,authentication,spring-boot,authorization,spring-security","timeRequired":"PT15M","video":{"@type":"VideoObject","name":"Spring Boot: Build URL Shortener Application - Part 8: Private Short URLs for Authenticated Users","description":"A step-by-step continuation of a Spring Boot URL shortener build walks through adding private short URLs and custom expiration for authenticated users. It...","thumbnailUrl":"https://i.ytimg.com/vi/fHs-ueL3wm0/sddefault.jpg","uploadDate":"2026-09-27T07:26:39.656Z","duration":"PT15M","url":"https://api.daily.dev/r/LLbAxvJph","embedUrl":"https://www.youtube.com/embed/fHs-ueL3wm0"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SivaLabs (YouTube)","item":"https://daily.dev/sources/sivalabs-youtube"},{"@type":"ListItem","position":3,"name":"Spring Boot: Build URL Shortener Application - Part 8: Private Short URLs for Authenticated Users"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/spring-boot-build-url-shortener-application---part-8-private-short-urls-for-authenticated-users-llbaxvjph#faq","mainEntity":[{"@type":"Question","name":"How do I restrict access to a private short URL so only the owner can use it in Spring Boot?","acceptedAnswer":{"@type":"Answer","text":"Check the current logged-in user's ID against the short URL's created-by field before redirecting. When a short URL is marked private, compare its owner ID to the requesting user's ID using a null-safe equals check; if they don't match (or no user is logged in), return an empty optional instead of redirecting, which surfaces as a not-found error. daily.dev surfaces implementation walkthroughs like this for developers building access-controlled features in Spring Boot."}},{"@type":"Question","name":"How can I let logged-in users set a custom expiration date for a short URL while defaulting guests to a fixed expiry?","acceptedAnswer":{"@type":"Answer","text":"Add an optional expirationInDays field (min 1, max 365) to the form and command object, shown only when the user is authenticated. In the service layer, check whether the command's user ID is non-null; if so, apply the custom expiration value when provided, otherwise fall back to the default 30-day expiration used for guest users. developers designing tiered features by auth status can track patterns like this via daily.dev."}}]}
```

