1616import org .springframework .beans .factory .annotation .Autowired ;
1717import org .springframework .security .core .context .SecurityContextHolder ;
1818import org .springframework .security .oauth2 .client .authentication .OAuth2AuthenticationToken ;
19+ import org .springframework .security .web .savedrequest .RequestCache ;
20+ import org .springframework .security .web .savedrequest .SavedRequest ;
1921import org .springframework .stereotype .Controller ;
2022import org .springframework .ui .ModelMap ;
2123import org .springframework .util .MultiValueMap ;
2224import org .springframework .web .bind .annotation .*;
2325import org .springframework .web .servlet .ModelAndView ;
2426import org .springframework .web .servlet .view .RedirectView ;
2527
28+ import javax .servlet .http .HttpServletRequest ;
2629import javax .servlet .http .HttpServletResponse ;
2730import java .io .BufferedWriter ;
2831import java .io .File ;
@@ -60,6 +63,9 @@ public class IndexController {
6063 @ Autowired
6164 private User user ;
6265
66+ @ Autowired
67+ private RequestCache requestCache ;
68+
6369 @ Autowired
6470 private OAuthProperties jwtGrantSso ;
6571
@@ -101,10 +107,14 @@ public String index(ModelMap model, HttpServletResponse response) throws IOExcep
101107 }
102108
103109 @ GetMapping (path = "/ds/mustAuthenticate" )
104- public ModelAndView mustAuthenticateController (ModelMap model ) throws IOException {
110+ public ModelAndView mustAuthenticateController (ModelMap model , HttpServletRequest req , HttpServletResponse resp ) throws IOException {
105111 model .addAttribute (LAUNCHER_TEXTS , config .getCodeExamplesText ().SupportingTexts );
106112 model .addAttribute (ATTR_TITLE , config .getCodeExamplesText ().SupportingTexts .LoginPage .LoginButton );
107113
114+ SavedRequest savedRequest = requestCache .getRequest (req , resp );
115+ var redirectURL = savedRequest != null && "GET" .equals (savedRequest .getMethod ()) ?
116+ savedRequest .getRedirectUrl () : "/" ;
117+
108118 if (config .getIsConsentRedirectActivated ()) {
109119 config .setIsConsentRedirectActivated (false );
110120 this .session .setAuthTypeSelected (AuthType .JWT );
@@ -114,7 +124,8 @@ public ModelAndView mustAuthenticateController(ModelMap model) throws IOExceptio
114124 config .getUserId (),
115125 config .getImpersonatedUserId (),
116126 config .getBaseURL (),
117- config ));
127+ config ,
128+ "/" ));
118129 }
119130
120131 if (session .isRefreshToken () || config .getQuickstart ().equals ("true" )) {
@@ -128,7 +139,8 @@ public ModelAndView mustAuthenticateController(ModelMap model) throws IOExceptio
128139 config .getUserId (),
129140 config .getImpersonatedUserId (),
130141 config .getBaseURL (),
131- config ));
142+ config ,
143+ redirectURL ));
132144 }
133145
134146 return new ModelAndView (getRedirectView (session .getAuthTypeSelected ()));
@@ -140,7 +152,8 @@ public ModelAndView mustAuthenticateController(ModelMap model) throws IOExceptio
140152 config .getUserId (),
141153 config .getImpersonatedUserId (),
142154 config .getBaseURL (),
143- config ));
155+ config ,
156+ redirectURL ));
144157 } else {
145158 return new ModelAndView ("pages/ds_must_authenticate" );
146159 }
@@ -228,11 +241,16 @@ private void writeCorrectScopesIntoApplication(ApiType apiTypeSelected) throws I
228241 }
229242
230243 @ RequestMapping (path = "/ds/authenticate" , method = RequestMethod .POST )
231- public RedirectView authenticate (ModelMap model , @ RequestBody MultiValueMap <String , String > formParams ) throws IOException {
244+ public RedirectView authenticate (ModelMap model , @ RequestBody MultiValueMap <String , String > formParams , HttpServletRequest req , HttpServletResponse resp ) throws IOException {
232245 if (!formParams .containsKey ("selectAuthType" )) {
233246 model .addAttribute ("message" , "Select option with selectAuthType name must be provided." );
234247 return new RedirectView ("pages/error" );
235248 }
249+
250+ SavedRequest savedRequest = requestCache .getRequest (req , resp );
251+ var redirectURL = savedRequest != null && "GET" .equals (savedRequest .getMethod ()) ?
252+ savedRequest .getRedirectUrl () : "/" ;
253+
236254 List <String > selectAuthTypeObject = formParams .get ("selectAuthType" );
237255 AuthType authTypeSelected = AuthType .valueOf (selectAuthTypeObject .get (0 ));
238256
@@ -243,7 +261,8 @@ public RedirectView authenticate(ModelMap model, @RequestBody MultiValueMap<Stri
243261 config .getUserId (),
244262 config .getImpersonatedUserId (),
245263 config .getBaseURL (),
246- config );
264+ config ,
265+ redirectURL );
247266 }else {
248267 return getRedirectView (authTypeSelected );
249268 }
0 commit comments