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

4
  • Did you get this working? Commented Jan 30, 2019 at 21:54
  • No luck yet. I ended up deploying React in a separate node server and Spring boot project on WLS. Commented Jan 31, 2019 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? Commented Feb 6, 2019 at 23:30
  • You should user webpack server for deployment in production. You can refer to stackoverflow.com/questions/35054082/… to get more information on how to use it. Commented Feb 8, 2019 at 1:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.