Skip to content
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

On formLogin -> authenticationSuccessHandler -> new RedirectServerAuthenticationSuccessHandler("/location") fails redirect #14872

Closed
hth opened this issue Apr 9, 2024 · 6 comments
Assignees
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@hth
Copy link

hth commented Apr 9, 2024

Describe the bug
On successful login, I am trying to redirect but it always defaults to "/"

To Reproduce
Steps to reproduce the behavior.

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class MultiSecurityHttpConfig {

  http
      .authorizeExchange((authorize) -> authorize
                    .pathMatchers("/resources/**", "/signup", "/about").permitAll()
                    .pathMatchers("/home/**").hasRole("USER")
                    .pathMatchers("/admin/**").hasRole("ADMIN")
                    .pathMatchers("/db/**").access((authentication, context) ->
                            hasRole("ADMIN")
                                    .check(authentication, context)
                                    .filter(decision -> !decision.isGranted())
                                    .switchIfEmpty(hasRole("DBA").check(authentication, context))
                    )
                    .anyExchange().denyAll())
      .formLogin(formLogin -> formLogin
                 .authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/home/landing")));

Expected behavior
After successful login, should get re-directed to "/home/landing"

Sample

A link to a GitHub repository with a minimal, reproducible sample.

Reports that include a sample will take priority over reports that do not.
At times, we may require a sample, so it is good to try and include a sample up front.

@hth hth added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Apr 9, 2024
@hth hth changed the title On formLogin -> authenticationSuccessHandler -> new RedirectServerAuthenticationSuccessHandler("/location") On formLogin -> authenticationSuccessHandler -> new RedirectServerAuthenticationSuccessHandler("/location") fails redirect Apr 9, 2024
@abimael-turing
Copy link

abimael-turing commented Apr 24, 2024

I was unable to replicate the error using the code you provided. Upon testing the following code snippet, it executed successfully:

@Bean
SecurityFilterChain springSecurityFilterChain(HttpSecurity http) throws Exception {

    AuthenticationSuccessHandler successHandler = (request, response, authentication) -> response.sendRedirect("/home/landing");
    
    http.authorizeHttpRequests((requests) -> requests
                    .requestMatchers("/home/**").authenticated()
                    .requestMatchers("/resources/**", "/signup", "/about").permitAll())
            .formLogin(form -> form.successHandler(successHandler))
            .httpBasic(withDefaults());
    return http.build();
}

Could you please provide a more comprehensive code snippet for further analysis?

@hth
Copy link
Author

hth commented Apr 25, 2024

Sample code listed at https://github.com/hth/spring-security-samples
Run module configured-login
User name: user@hth.github.com
Password: password
Expected to redirect -> "/home/landing"

Successful login first time gives you 'Access Denied'

Steps to follow @abimael-turing

  1. http://localhost:8092
  2. Enter credentials
  3. Access denied shown

File

@abimael-turing
Copy link

on_formLogin

Hi @hth,

I wanted to update you that I followed your instructions, and everything is working as expected on my end. There are no errors or 'Access denied' messages. If you need any further assistance or have additional instructions, please let me know. Thank you.

@abimael-turing
Copy link

image

@hth I am running configured-login

@hth
Copy link
Author

hth commented May 6, 2024

Screen Recording 2024-05-05 at 11 28 31 PM

Please see the steps and match with the gif @abimael-turing

  1. http://localhost:8092/
  2. Enter credentials
  3. Access denied shown

Expected to redirect after successful login
Should have redirected to http://localhost:8092/home/landing

Note: Your starting link has to be http://localhost:8092/

@sjohnr
Copy link
Member

sjohnr commented May 6, 2024

Thanks for getting in touch @hth, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add a minimal sample that reproduces this issue if you feel this is a genuine bug.

Please note that the sample you have linked is not minimal and contains numerous customizations unrelated to the redirect handler.

@sjohnr sjohnr closed this as completed May 6, 2024
@sjohnr sjohnr self-assigned this May 6, 2024
@sjohnr sjohnr added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

3 participants