blocked cors localhost:3000 springboot issue

I tried to fetch localhost:8080 spring-boot application and I got the following error in the console.

Access to XMLHttpRequest at 'http://localhost:8080/' from origin 'http://localhost:3000' has been blocked by CORS policy:

I did the following changes in SpringSecurityConfig class file:

    
@Override
protected void configure(HttpSecurity http) throws Exception {    
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().exceptionHandling()
    .authenticationEntryPoint(restAuthEntry).and()
    .authorizeRequests((request) -> request.antMatchers("/api/v1/auth/login").permitAll()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll())
    .addFilterBefore(new JWTAuthenticationFilter(userService, jWTTokenHelper),
            UsernamePasswordAuthenticationFilter.class);
    http.csrf().disable().cors().and().headers().frameOptions().disable();
}

This solved..

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, PHP, DevOps technologies and Opensource Frameworks. Github Repo: Purab Youtube Chanel Purab Code Book Connect to on LinkedIn or Follow @purab on Twitter or Write Mail to purabdk@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *