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, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.