Spring boot and react when refreshing from "/home" it redirects back to /index.html"

Viewed 41

I am using the Spring Boot with React for security login as shown here: https://www.baeldung.com/spring-security-login-react

The App.js file is:

function App() {
  let history = useHistory();
  const [formData, setFormData] = React.useState({
    username: "",
    password: ""
  })

  function handleChange(event){
    const {name, value} = event.target  
    setFormData(prevFormData => ({...prevFormData, [name]: value}))
  }

  function handleSubmit() {
    history.push("/home");
  }
  
  return (
    <>
    <Switch>
      <Route exact path = "/index.html"  render= {(props)=> <Login handleSubmit={handleSubmit} handleChange={handleChange} {...props} />}/>
      <Route path = "/home"  render = {(props) => <WelcomeHome email={formData.username} {...props} />}/>
      <Route path = "/log-out" render = {(props)=> <Login handleSubmit={handleSubmit} handleChange={handleChange} {...props}/>}/>
      <Route path= "/test" render = {() => <Test/>}/>
    </Switch>
   
    </>
    
  );
}

export default App;

And the Web security configuration in the spring boot project has the conifguration:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable().authorizeRequests().antMatchers("/api/v*/registration/**", "/api/v*/user/**",
                        "/index*", "/static/**", "/*.js", "/*.json", "/*.ico").
                permitAll().anyRequest().authenticated().and().formLogin()
                .loginPage("/index.html")
                .loginProcessingUrl("/perform_login")
                .defaultSuccessUrl("/home",true)
                .failureUrl("/index.html?error=true").and().logout(logout -> logout
                .logoutUrl("/log-out")
                .logoutSuccessUrl("/index.html")
                .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE)))
                .deleteCookies("JSESSIONID","remember-me")
                .clearAuthentication(true)
                .invalidateHttpSession(true).permitAll()
        );

    }

The problem is that when logging in it is redirected to /home from /index.html, however when refreshing the application is redirected back to /index.html, and all I want is to remain on /home when refreshing

0 Answers
Related