spring-security: Spring security version 6.0.1 jump to a wrong url after login success

Describe the bug When trying to log in, if the log in successful, then it will redirect to the wrong URL: http:localhost:9090/error?continue here it must redirect to / URL, to the URL which it comes from, but it goes to /error?continue

To Reproduce The below code processes the wrong URL

	/**
	 * Indicates the URL that the user agent used for this request.
	 * @return the full URL of this request
	 */
	@Override
	public String getRedirectUrl() {
		String queryString = createQueryString(this.queryString, this.matchingRequestParameterName);
		return UrlUtils.buildFullRequestUrl(this.scheme, this.serverName, this.serverPort, this.requestURI,
				queryString);
	}

matchingRequestParameterName = continue requestURI = error so it will become /error?continue

Expected behavior It can redirect to the URL which it comes from or redirect to root path /

Note: the previous version (5.3.4) works well

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

I’m experiencing exactly the same issue. The DefaultSavedRequest object is already set to error?continue api path. I couldn’t find where the assignment was.

Spring Boot 3.x and Spring Security 6.0.1 have many challenages… Can I get a hint about the solution?

@marcusdacoregio Thanks for the reply! Here I have tried a lot to figure out the problem, but I did not get any solution. So here I have no way, but only to send you some screen shots, which let you know the configuration I did, and the DEBUG information. The below screenshot is my configuration: Snipaste_2023-02-11_00-06-35

And this screenshot show the request URL already set /error, I have don nothing just started the application and sent a GET request by browser as http://localhost:9999/user/list OR http://localhost:9999 which these URLs both needs to be authenticated, but I get the below result, which is very a strange error, that when and where the error has been set the request URL?!

Snipaste_2023-02-10_23-51-07

So you can check these screenshots if figured out the problem. Thanks in advance!

I had the same issue where my application would redirect to /error after login. I solved it by adding this line:

.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll()

In context:

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(authz -> authz
                        .dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll()
                        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                        .requestMatchers("/svg/*").permitAll()
                        .requestMatchers("/img/*").permitAll()
                        .anyRequest().authenticated())
                .formLogin(login -> login.loginPage("/login").permitAll())
                .logout(LogoutConfigurer::permitAll);

        return http.build();
    }

There was no need to set the shouldFilterAllDispatcherTypes to false.

@ujkim it’s hard to guess what is happening since there is a lot going on. Did you try adding logging.level.org.springframework.security=TRACE to your application.properties and check the console log for a clue on what is happening?