3

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/?.

Ganesh chaitanya
  • 638
  • 6
  • 18
  • Did you get this working? – Roro Jan 30 '19 at 21:54
  • No luck yet. I ended up deploying React in a separate node server and Spring boot project on WLS. – Ganesh chaitanya Jan 31 '19 at 10:39
  • OK. I'm new to react. How do you run in a "separate node server"? I run it in my cmd line while developing but I don't want to do that in production...how did you do it? – Roro Feb 06 '19 at 23:30
  • You should user webpack server for deployment in production. You can refer to https://stackoverflow.com/questions/35054082/webpack-how-to-build-production-code-and-how-to-use-it to get more information on how to use it. – Ganesh chaitanya Feb 08 '19 at 01:29

0 Answers0