Skip to content

Conversation

@lcnicolau
Copy link
Contributor

@lcnicolau lcnicolau commented Apr 28, 2025

Htmx provides a special way to send a redirect instruction to the client, keeping a success code (200) and sending a custom HTTP header from the server (HX-Location / HX-Redirect). Htmx correctly interprets these headers and follows the redirect, replacing the response in the page body.

You can take advantage of this behavior by integrating the HxLocationRedirectAuthenticationFailureHandler, HxLocationRedirectAuthenticationSuccessHandler, HxLocationRedirectLogoutSuccessHandler, HxLocationRedirectAuthenticationEntryPoint and/or HxLocationRedirectAccessDeniedHandler into the SecurityFilterChain bean definition.

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    // probably some other configurations here
    return http
            .formLogin(login -> login
                    .failureHandler(new HxLocationRedirectAuthenticationFailureHandler("/login?failure"))
                    .successHandler(new HxLocationRedirectAuthenticationSuccessHandler("/home?login"))
            ).logout(logout -> logout
                    .logoutSuccessHandler(new HxLocationRedirectLogoutSuccessHandler("/home?logout"))
            ).exceptionHandling(handler -> handler
                    .authenticationEntryPoint(new HxLocationRedirectAuthenticationEntryPoint("/login?unauthorized"))
                    .accessDeniedHandler(new HxLocationRedirectAccessDeniedHandler("/error?forbidden"))
            ).build();
}

For detailed information and a usage example, see:
https://github.com/lcnicolau/cs50-todo-list?tab=readme-ov-file#htmx-redirect-pattern

@wimdeblauwe
Copy link
Owner

Thank you for your contribution. I will look into more detail later, but what I am already missing is an update to the README to explain how to use the classes. I don't know if it would be possible to add some tests as well ?

@lcnicolau lcnicolau requested a review from xhaggi July 14, 2025 20:09
@lcnicolau
Copy link
Contributor Author

Hi @xhaggi
I made the requested changes and left a couple of comments in the revisions. Could you please take another look?

@wimdeblauwe
Copy link
Owner

@lcnicolau Could tests be added?

@lcnicolau
Copy link
Contributor Author

Hi @wimdeblauwe
Added 100% test coverage for the security package, including the existing HxRefreshHeaderAuthenticationEntryPoint class.

@wimdeblauwe
Copy link
Owner

There is indeed 100% test coverage, but it is mocking everything. I would rather see @WebMvcTest based tests for these things. I believe that would provide more value.

@lcnicolau
Copy link
Contributor Author

Thanks @wimdeblauwe
I added the new @WebMvcTest based tests. Should I remove the original mocked tests, or would you prefer to keep both?

@wimdeblauwe
Copy link
Owner

No, you can remove the mocked tests.

@lcnicolau
Copy link
Contributor Author

No, you can remove the mocked tests.

@wimdeblauwe done.

@xhaggi
Copy link
Collaborator

xhaggi commented Nov 25, 2025

@lcnicolau please squash all your commits into the first one.

@lcnicolau lcnicolau force-pushed the htmx-redirect-pattern branch from 6dcd768 to 1d24f3e Compare November 26, 2025 20:01
@lcnicolau lcnicolau changed the title Add support for native HTMX redirects in Spring Security Add support for native htmx redirects in Spring Security Nov 26, 2025
@lcnicolau lcnicolau requested a review from xhaggi November 26, 2025 20:07
@xhaggi xhaggi added this to the 5.0.0 milestone Nov 27, 2025
@wimdeblauwe wimdeblauwe merged commit c52f8e6 into wimdeblauwe:main Nov 27, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants