I have back-end Spring boot application with Spring security enabled with FormLogin authentication. Below is my Security Configuration.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint());
http.authorizeRequests().antMatchers("/index.html").permitAll().antMatchers("/j_security_check").permitAll().antMatchers("/api/**").authenticated();
http.csrf().disable();
http.formLogin().usernameParameter("j_username")
.passwordParameter("j_password").loginPage("/index.html").loginProcessingUrl("/j_security_check")
.defaultSuccessUrl("/index.html", true).permitAll().failureUrl("/index.html?error=true");
http.logout().logoutUrl("/dashboard/logout").invalidateHttpSession(true).clearAuthentication(true)
.logoutSuccessUrl("/index.html").deleteCookies("JSESSIONID");
}
}
And my View Contoller class as below:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/")
.setViewName("forward:/index.html");
registry.addViewController("/home")
.setViewName("forward:/index.html");
}
@Override
public void addResourceHandlers(
ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("/WEB-INF/view/react/build/static/");
registry.addResourceHandler("/*.*")
.addResourceLocations("/WEB-INF/view/react/build/");
registry.addResourceHandler("/index.html")
.addResourceLocations("/WEB-INF/view/react/build/index.html");
}
}
index.html is generated by "npm run build" on the react app. And generated build folder is moved to static resources of Spring boot project. I have routes configured in react app like /login, /home. Once a successful login happens in /login component user will be directed to /home page using :
this.props.history.push("/home");
After Spring boot is started I'm, able to access the login page in index.html, and once I click login, I'm being redirected to /home and able to see Home Component for a second and again being re-directed to localhost:8080/?. Im not sure how its redirecting localhost:8080/?.