-
Notifications
You must be signed in to change notification settings - Fork 61
Add support for native htmx redirects in Spring Security #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...va/io/github/wimdeblauwe/htmx/spring/boot/security/HxLocationBoostedRedirectStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.security; | ||
|
|
||
| import io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxLocation; | ||
| import io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponseHeader; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.security.web.DefaultRedirectStrategy; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import static io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxRequestHeader.HX_BOOSTED; | ||
| import static org.springframework.http.HttpStatus.OK; | ||
|
|
||
| /** | ||
| * htmx-friendly redirect strategy to be used with any security handler that performs redirects. | ||
| * <p> | ||
| * When instantiated by the default constructor, it checks for htmx requests and responds with {@link HttpStatus#OK}, | ||
| * including the target URL in the {@link HtmxResponseHeader#HX_LOCATION} header. | ||
| * <p> | ||
| * It also sets the {@code headers} and {@code target} parameters, instructing the client to include the | ||
| * {@code HX-Boosted} header in the new request, and to swap the response into the {@code body} element. | ||
| * <p> | ||
| * Example: | ||
| * <pre> {@code | ||
| * HX-Location: { | ||
| * "path":"/login?unauthorized", | ||
| * "headers":{"HX-Boosted":"true"}, | ||
| * "target":"body" | ||
| * } | ||
| * } </pre> | ||
| * <p> | ||
| * These parameters are useful if you want to take advantage of existing controller optimizations, to render a fragment | ||
| * instead of the full page for non-boosted, htmx-driven requests: | ||
| * <p> | ||
| * <pre> {@code | ||
| * @GetMapping("/login") | ||
| * String login(HtmxRequest request) { | ||
| * return request.isHtmxRequest() && !request.isBoosted() | ||
| * ? "pages/login :: content" | ||
| * : "pages/login"; | ||
| * } | ||
| * } </pre> | ||
| * <p> | ||
| * In case you don’t need this "boosted" behavior, use the {@link HxLocationRedirectStrategy} instead. | ||
| * <p> | ||
| * For non-htmx requests, it delegates to the {@link DefaultRedirectStrategy}. | ||
| * | ||
| * @author LC Nicolau | ||
| * @see <a href="https://htmx.org/headers/hx-location/">HX-Location Response Header</a> | ||
| * @see <a href="https://htmx.org/reference/#headers">HTTP Header Reference</a> | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HxLocationBoostedRedirectStrategy extends HxLocationRedirectStrategy { | ||
|
|
||
| private final JsonMapper jsonMapper; | ||
|
|
||
| public HxLocationBoostedRedirectStrategy() { | ||
| this(OK); | ||
| } | ||
|
|
||
| public HxLocationBoostedRedirectStrategy(HttpStatus status) { | ||
| super(status); | ||
| this.jsonMapper = new JsonMapper(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void sendHxLocationRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { | ||
| super.sendHxLocationRedirect(request, response, boosted(url)); | ||
| } | ||
|
|
||
| protected String boosted(String url) { | ||
| HtmxLocation location = new HtmxLocation(url); | ||
| location.setTarget("body"); | ||
| location.setHeaders(Map.of(HX_BOOSTED.getValue(), "true")); | ||
| return jsonMapper.writeValueAsString(location); | ||
| } | ||
|
|
||
| } | ||
42 changes: 42 additions & 0 deletions
42
...o/github/wimdeblauwe/htmx/spring/boot/security/HxLocationRedirectAccessDeniedHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.security; | ||
|
|
||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.security.access.AccessDeniedException; | ||
| import org.springframework.security.web.RedirectStrategy; | ||
| import org.springframework.security.web.access.AccessDeniedHandler; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Handles navigation on {@link HttpStatus#FORBIDDEN} access by delegating to the {@link HxLocationRedirectStrategy}, | ||
| * providing an htmx-friendly redirect mechanism. | ||
| * <p> | ||
| * This class is not used by the library itself, but users of the library can use it to configure their security for | ||
| * native htmx redirects. | ||
| * | ||
| * @author LC Nicolau | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HxLocationRedirectAccessDeniedHandler implements AccessDeniedHandler { | ||
|
|
||
| private final String redirectUrl; | ||
| private final RedirectStrategy redirectStrategy; | ||
|
|
||
| public HxLocationRedirectAccessDeniedHandler(String redirectUrl) { | ||
| this(redirectUrl, new HxLocationRedirectStrategy(HttpStatus.FORBIDDEN)); | ||
| } | ||
|
|
||
| public HxLocationRedirectAccessDeniedHandler(String redirectUrl, RedirectStrategy redirectStrategy) { | ||
| this.redirectUrl = redirectUrl; | ||
| this.redirectStrategy = redirectStrategy; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
| redirectStrategy.sendRedirect(request, response, redirectUrl); | ||
| } | ||
|
|
||
| } |
42 changes: 42 additions & 0 deletions
42
...hub/wimdeblauwe/htmx/spring/boot/security/HxLocationRedirectAuthenticationEntryPoint.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.security; | ||
|
|
||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.security.core.AuthenticationException; | ||
| import org.springframework.security.web.AuthenticationEntryPoint; | ||
| import org.springframework.security.web.RedirectStrategy; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Handles navigation on {@link HttpStatus#UNAUTHORIZED} access by delegating to the {@link HxLocationRedirectStrategy}, | ||
| * providing an htmx-friendly redirect mechanism. | ||
| * <p> | ||
| * This class is not used by the library itself, but users of the library can use it to configure their security for | ||
| * native htmx redirects. | ||
| * | ||
| * @author LC Nicolau | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HxLocationRedirectAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
|
||
| private final String redirectUrl; | ||
| private final RedirectStrategy redirectStrategy; | ||
|
|
||
| public HxLocationRedirectAuthenticationEntryPoint(String redirectUrl) { | ||
| this(redirectUrl, new HxLocationRedirectStrategy(HttpStatus.UNAUTHORIZED)); | ||
| } | ||
|
|
||
| public HxLocationRedirectAuthenticationEntryPoint(String redirectUrl, RedirectStrategy redirectStrategy) { | ||
| this.redirectUrl = redirectUrl; | ||
| this.redirectStrategy = redirectStrategy; | ||
| } | ||
|
|
||
| @Override | ||
| public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
| redirectStrategy.sendRedirect(request, response, redirectUrl); | ||
| } | ||
|
|
||
| } |
44 changes: 44 additions & 0 deletions
44
...wimdeblauwe/htmx/spring/boot/security/HxLocationRedirectAuthenticationFailureHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.security; | ||
|
|
||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.security.core.AuthenticationException; | ||
| import org.springframework.security.web.RedirectStrategy; | ||
| import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
| import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Handles a failed authentication attempt by delegating to the {@link SimpleUrlAuthenticationFailureHandler}, using | ||
| * {@link HxLocationRedirectStrategy} to provide an htmx-friendly redirect mechanism. | ||
| * <p> | ||
| * This class is not used by the library itself, but users of the library can use it to configure their security for | ||
| * native htmx redirects. | ||
| * | ||
| * @author LC Nicolau | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HxLocationRedirectAuthenticationFailureHandler implements AuthenticationFailureHandler { | ||
|
|
||
| private final AuthenticationFailureHandler delegate; | ||
|
|
||
| public HxLocationRedirectAuthenticationFailureHandler(String defaultFailureUrl) { | ||
| this(defaultFailureUrl, new HxLocationRedirectStrategy(HttpStatus.UNAUTHORIZED)); | ||
| } | ||
|
|
||
| public HxLocationRedirectAuthenticationFailureHandler(String defaultFailureUrl, RedirectStrategy redirectStrategy) { | ||
| var handler = new SimpleUrlAuthenticationFailureHandler(); | ||
| handler.setDefaultFailureUrl(defaultFailureUrl); | ||
| handler.setRedirectStrategy(redirectStrategy); | ||
| this.delegate = handler; | ||
| } | ||
|
|
||
| @Override | ||
| public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { | ||
| delegate.onAuthenticationFailure(request, response, exception); | ||
| } | ||
|
|
||
| } |
52 changes: 52 additions & 0 deletions
52
...wimdeblauwe/htmx/spring/boot/security/HxLocationRedirectAuthenticationSuccessHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.security; | ||
|
|
||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.web.RedirectStrategy; | ||
| import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
| import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Handles post-authentication navigation by delegating to the {@link SavedRequestAwareAuthenticationSuccessHandler}, | ||
| * using {@link HxLocationRedirectStrategy} to provide an htmx-friendly redirect mechanism. | ||
| * <p> | ||
| * This class is not used by the library itself, but users of the library can use it to configure their security for | ||
| * native htmx redirects. | ||
| * | ||
| * @author LC Nicolau | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HxLocationRedirectAuthenticationSuccessHandler implements AuthenticationSuccessHandler { | ||
|
|
||
| private final AuthenticationSuccessHandler delegate; | ||
|
|
||
| public HxLocationRedirectAuthenticationSuccessHandler(String defaultSuccessUrl) { | ||
| this(defaultSuccessUrl, false); | ||
| } | ||
|
|
||
| public HxLocationRedirectAuthenticationSuccessHandler(String defaultSuccessUrl, boolean alwaysUse) { | ||
| this(defaultSuccessUrl, alwaysUse, new HxLocationRedirectStrategy()); | ||
| } | ||
|
|
||
| public HxLocationRedirectAuthenticationSuccessHandler(String defaultSuccessUrl, RedirectStrategy redirectStrategy) { | ||
| this(defaultSuccessUrl, false, redirectStrategy); | ||
| } | ||
|
|
||
| public HxLocationRedirectAuthenticationSuccessHandler(String defaultSuccessUrl, boolean alwaysUse, RedirectStrategy redirectStrategy) { | ||
| var handler = new SavedRequestAwareAuthenticationSuccessHandler(); | ||
| handler.setDefaultTargetUrl(defaultSuccessUrl); | ||
| handler.setAlwaysUseDefaultTargetUrl(alwaysUse); | ||
| handler.setRedirectStrategy(redirectStrategy); | ||
| this.delegate = handler; | ||
| } | ||
|
|
||
| @Override | ||
| public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
| delegate.onAuthenticationSuccess(request, response, authentication); | ||
| } | ||
|
|
||
| } |
52 changes: 52 additions & 0 deletions
52
.../github/wimdeblauwe/htmx/spring/boot/security/HxLocationRedirectLogoutSuccessHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package io.github.wimdeblauwe.htmx.spring.boot.security; | ||
|
|
||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.web.RedirectStrategy; | ||
| import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; | ||
| import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Handles post-logout navigation by delegating to the {@link SimpleUrlLogoutSuccessHandler}, using | ||
| * {@link HxLocationRedirectStrategy} to provide an htmx-friendly redirect mechanism. | ||
| * <p> | ||
| * This class is not used by the library itself, but users of the library can use it to configure their security for | ||
| * native htmx redirects. | ||
| * | ||
| * @author LC Nicolau | ||
| * @since 5.0.0 | ||
| */ | ||
| public class HxLocationRedirectLogoutSuccessHandler implements LogoutSuccessHandler { | ||
|
|
||
| private final LogoutSuccessHandler delegate; | ||
|
|
||
| public HxLocationRedirectLogoutSuccessHandler(String logoutSuccessUrl) { | ||
| this(logoutSuccessUrl, false); | ||
| } | ||
|
|
||
| public HxLocationRedirectLogoutSuccessHandler(String logoutSuccessUrl, boolean alwaysUse) { | ||
| this(logoutSuccessUrl, alwaysUse, new HxLocationRedirectStrategy()); | ||
| } | ||
|
|
||
| public HxLocationRedirectLogoutSuccessHandler(String logoutSuccessUrl, RedirectStrategy redirectStrategy) { | ||
| this(logoutSuccessUrl, false, redirectStrategy); | ||
| } | ||
|
|
||
| public HxLocationRedirectLogoutSuccessHandler(String logoutSuccessUrl, boolean alwaysUse, RedirectStrategy redirectStrategy) { | ||
| var handler = new SimpleUrlLogoutSuccessHandler(); | ||
| handler.setDefaultTargetUrl(logoutSuccessUrl); | ||
| handler.setAlwaysUseDefaultTargetUrl(alwaysUse); | ||
| handler.setRedirectStrategy(redirectStrategy); | ||
| this.delegate = handler; | ||
| } | ||
|
|
||
| @Override | ||
| public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
| delegate.onLogoutSuccess(request, response, authentication); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.