Menu

Showing posts with label Get requested page URL in Spring boot. Show all posts
Showing posts with label Get requested page URL in Spring boot. Show all posts

Get requested page URL from request in Spring Boot

How to get the requested URL information in a Spring security project so that we can redirect the user to the secure or right page after successful login. On every request Spring security stores the following information in the session key "SPRING_SECURITY_SAVED_REQUEST". This way will help us to get the last requested URL or request from where a user started the authentication request that redirected the user to the login page.

SPRING_SECURITY_SAVED_REQUEST key spring boot-request-url
SPRING_SECURITY_SAVED_REQUEST

Below is the code snippet to get the requested url from the "SPRING_SECURITY_SAVED_REQUEST" key.

DefaultSavedRequest savedRequest = (DefaultSavedRequest)request.getSession()
        .getAttribute("SPRING_SECURITY_SAVED_REQUEST");
    	if(savedRequest != null) {
    		String previousPageUrl = savedRequest.getRedirectUrl();
    		
    	} 

This will helps us to redirect the users to the secure page after successful login. Thank you, hope this solves your problem.