How to apply spring security filter only on secured endpoints?

前端 未结 6 572
时光说笑
时光说笑 2020-12-01 05:25

I have the following Spring Security configuration:

    httpSecurity
            .csrf()
            .disable()
            .exceptionHandling()
                     


        
6条回答
  •  -上瘾入骨i
    2020-12-01 05:49

    I have an application with the same requirement and to solve it I basically restricted Spring Security to a given ant match patter (using antMatcher) as follows:

    http.antMatcher("/api/**").authorizeRequests() //
            .anyRequest().authenticated() //
            .and()
            .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
    

    You can read it as follows: for http only invoke these configurations on requests matching the ant pattern /api/** authorizing any request to authenticated users and add filter authenticationTokenFilterBean() before UsernamePasswordAuthenticationFilter. For all others requests this configuration has no effect.

提交回复
热议问题