Skip to content

Commit 5749a79

Browse files
committed
Add documentation for HTMX-aware AuthenticationEntryPoint
- CHANGELOG.md: Add [Unreleased] entry for the new feature - README.md: Add HTMX Support section under Security Features with override instructions; update features list and table of contents - CLAUDE.md: Add HtmxAwareAuthenticationEntryPoint to Security section and AuthenticationEntryPoint override to Extension Points
1 parent 15e5af8 commit 5749a79

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## [Unreleased]
2+
### Features
3+
- HTMX-aware AuthenticationEntryPoint for session expiry handling (#294)
4+
- When HTMX requests (identified by `HX-Request: true` header) hit an expired session, the framework now returns a 401 JSON response with an `HX-Redirect` header instead of the default 302 redirect that causes HTMX to swap login page HTML into fragment targets.
5+
- New classes:
6+
- `HtmxAwareAuthenticationEntryPoint` — detects HTMX requests and returns 401 + JSON + `HX-Redirect`; delegates to wrapped entry point for standard browser requests
7+
- `HtmxAwareAuthenticationEntryPointConfiguration` — registers the entry point via `@ConditionalOnMissingBean(AuthenticationEntryPoint.class)`
8+
- `WebSecurityConfig` now always configures `exceptionHandling()` with the injected entry point (previously only configured when OAuth2 was enabled)
9+
- Consumer override: define any `AuthenticationEntryPoint` bean to replace the default
10+
- 100% backward-compatible: non-HTMX browser requests get the same 302 redirect as before
11+
112
## [4.3.1] - 2026-03-22
213
### Features
314
- No new user-facing features in this release.

CLAUDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ com.digitalsanctuary.spring.user
111111
- `DSUserDetails` - Custom UserDetails implementation wrapping User entity
112112
- `DSOAuth2UserService` / `DSOidcUserService` - OAuth2/OIDC user services
113113
- `LoginAttemptService` - Brute force protection with account lockout
114+
- `HtmxAwareAuthenticationEntryPoint` - Returns 401 JSON for HTMX requests instead of 302 redirect on session expiry
114115

115116
**Extension Points:**
116117
- `BaseUserProfile` - Extend for custom user data (see PROFILE.md)
117118
- `UserProfileService<T>` - Interface for profile management
118119
- `BaseSessionProfile<T>` - Session-scoped profile access
119120
- `UserPreDeleteEvent` - Listen for user deletion to clean up related data
121+
- `AuthenticationEntryPoint` - Override via `@ConditionalOnMissingBean` to customize session expiry behavior
120122

121123
### Auto-Configuration
122124

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Check out the [Spring User Framework Demo Application](https://github.com/devond
3939
- [Role-Based Access Control](#role-based-access-control)
4040
- [Account Lockout](#account-lockout)
4141
- [Audit Logging](#audit-logging)
42+
- [HTMX Support](#htmx-support)
4243
- [User Management](#user-management)
4344
- [Registration](#registration)
4445
- [Profile Management](#profile-management)
@@ -83,6 +84,7 @@ Check out the [Spring User Framework Demo Application](https://github.com/devond
8384
- Audit event framework for recording and logging security events, customizable to store audit events in a database or publish them via a REST API.
8485
- Role and Privilege setup service to define roles, associated privileges, and role inheritance hierarchy using `application.yml`.
8586
- Configurable Account Lockout after too many failed login attempts
87+
- HTMX-aware session expiry handling — returns 401 JSON instead of 302 redirect for HTMX requests, preventing broken UI fragments
8688

8789
- **Advanced Security**
8890
- Role and privilege-based authorization
@@ -504,6 +506,29 @@ user:
504506
flushRate: 10000
505507
```
506508
509+
### HTMX Support
510+
511+
When HTMX-powered pages make requests (polling, fragment loading, etc.) and the user's session expires, Spring Security's default 302 redirect causes HTMX to swap the full login page HTML into each target element, breaking the UI.
512+
513+
The framework automatically detects HTMX requests (via the `HX-Request` header) and returns a proper 401 response instead:
514+
515+
- **Status**: `401 Unauthorized`
516+
- **Header**: `HX-Redirect: <loginUrl>` (triggers HTMX full-page redirect)
517+
- **Body**: `{"error": "authentication_required", "message": "Session expired. Please log in.", "loginUrl": "<loginUrl>"}`
518+
519+
Non-HTMX browser requests continue to receive the standard 302 redirect to the login page.
520+
521+
**Overriding the default behavior:**
522+
523+
To provide a custom `AuthenticationEntryPoint`, define your own bean and the framework's default will back off automatically:
524+
525+
```java
526+
@Bean
527+
public AuthenticationEntryPoint authenticationEntryPoint() {
528+
return new MyCustomAuthenticationEntryPoint();
529+
}
530+
```
531+
507532
## User Management
508533

509534
### Registration

0 commit comments

Comments
 (0)