3737import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
3838import org .springframework .security .oauth2 .client .web .OAuth2LoginAuthenticationFilter ;
3939import org .springframework .security .oauth2 .core .oidc .user .OidcUser ;
40+ import org .springframework .security .web .savedrequest .RequestCache ;
41+ import org .springframework .security .web .savedrequest .SavedRequest ;
4042import org .springframework .util .MultiValueMap ;
4143import org .springframework .web .util .UriComponentsBuilder ;
4244
@@ -59,6 +61,9 @@ public class GeonetworkOAuth2LoginAuthenticationFilter extends OAuth2LoginAuthen
5961 @ Autowired
6062 OAuth2SecurityProviderUtil oAuth2SecurityProviderUtil ;
6163
64+ @ Autowired
65+ RequestCache requestCache ;
66+
6267 public GeonetworkOAuth2LoginAuthenticationFilter (ClientRegistrationRepository clientRegistrationRepository , OAuth2AuthorizedClientService authorizedClientService ) {
6368 super (clientRegistrationRepository , authorizedClientService );
6469 }
@@ -117,24 +122,54 @@ protected void successfulAuthentication(HttpServletRequest request,
117122 try {
118123 SecurityContextHolder .getContext ().setAuthentication (authResult );
119124
125+ // Use Spring Security's SavedRequest mechanism to get the original request URL
126+ // The request should have been saved by GeonetworkOidcPreAuthActionsLoginFilter before
127+ // redirecting the user to the OIDC provider login
128+ String redirectURL = null ;
129+
130+ if (requestCache != null ) {
131+ SavedRequest savedRequest = requestCache .getRequest (request , response );
132+ if (savedRequest != null ) {
133+ redirectURL = savedRequest .getRedirectUrl ();
134+ Log .debug (Geonet .SECURITY , "Retrieved original request from SavedRequest: " + redirectURL );
135+ } else {
136+ Log .debug (Geonet .SECURITY , "No SavedRequest found in RequestCache" );
137+ }
138+
139+ if (redirectURL != null ) {
140+ Log .info (Geonet .SECURITY , "Redirecting to " + redirectURL );
120141
121- //cf GN keycloak
122- String redirectURL = findQueryParameter (request , "redirectUrl" );
123- if (redirectURL != null ) {
124- try {
125- URI redirectUri = new URI (redirectURL );
126- if (redirectUri != null && !redirectUri .isAbsolute ()) {
127- response .sendRedirect (redirectUri .toString ());
128- } else {
129- // If the redirect url ends up being null or absolute url then lets redirect back to the context home.
130- Log .warning (Geonet .SECURITY , "Failed to perform login redirect to '" + redirectURL + "'. Redirected to context home" );
142+ // Removing original request, since we want to
143+ // retain current headers.
144+ // If request remains in cache, requestCacheFilter
145+ // will reinstate the original headers and we don't
146+ // want it.
147+ requestCache .removeRequest (request , response );
148+
149+ response .sendRedirect (redirectURL );
150+ }
151+ } else {
152+ Log .debug (Geonet .SECURITY , "RequestCache is not available" );
153+
154+ redirectURL = findQueryParameter (request , "redirectUrl" );
155+ if (redirectURL != null ) {
156+ Log .debug (Geonet .SECURITY , "Retrieved redirect URL from query parameter: " + redirectURL );
157+
158+ try {
159+ URI redirectUri = new URI (redirectURL );
160+ if (!redirectUri .isAbsolute ()) {
161+ response .sendRedirect (redirectUri .toString ());
162+ } else {
163+ // If the redirect url ends up being null or absolute url then lets redirect back to the context home.
164+ Log .warning (Geonet .SECURITY , "Failed to perform login redirect to '" + redirectURL + "'. Redirected to context home" );
165+ response .sendRedirect (request .getContextPath ());
166+ }
167+ } catch (URISyntaxException e ) {
131168 response .sendRedirect (request .getContextPath ());
132169 }
133- } catch ( URISyntaxException e ) {
170+ } else {
134171 response .sendRedirect (request .getContextPath ());
135172 }
136- } else {
137- response .sendRedirect (request .getContextPath ());
138173 }
139174
140175 // Set users preferred locale if it exists. - cf. keycloak
0 commit comments