Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS problems with spring boot security

I use spring boot, spring security and want to avoid any cors actions. Im trying the second answer from here (Can you completely disable CORS support in Spring?) where I need to add custom filter to cors, here is the filter:

@Component
public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
                                    final FilterChain filterChain) throws ServletException, IOException {

        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, HEAD");
        response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
        response.addHeader("Access-Control-Expose-Headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addIntHeader("Access-Control-Max-Age", 10);
        filterChain.doFilter(request, response);
    }
}

So Ive created the class insert there CorsFilter and I got the mistake:

'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'fa.backend.core.security.CorsFilter'

My security configuration looks like this:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().antMatcher("/users").authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .and()
                .addFilterBefore(
                        new JWTLoginFilter(tokenAuthentication, authenticationManagerBean()),
                        UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

Could you tell me how to fix it? My aim is to disable any cors actions, because from frontend I recieve:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8081' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
like image 812
Sergey Vladimirovich Avatar asked Jan 29 '23 04:01

Sergey Vladimirovich


1 Answers

remove the 'cors().and' part after http in the configure override. Following is the snippet.

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/users").authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .and()
                .addFilterBefore(
                        new JWTLoginFilter(tokenAuthentication, authenticationManagerBean()),
                        UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
like image 186
Adil Khalil Avatar answered Jan 30 '23 17:01

Adil Khalil