How to apply spring security filter only on secured endpoints?

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

I have the following Spring Security configuration:

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


        
6条回答
  •  醉梦人生
    2020-12-01 05:57

    If you use the

    .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    

    You can define in the constructor the specific path it will apply to:

    public class JwtAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    
        public JwtAuthenticationFilter(AuthenticationManager authenticationManager) {
            super("/api/**");
            this.setAuthenticationManager(authenticationManager);
        }
    
        @Override
        protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
            return super.requiresAuthentication(request, response);
        }
    

    The requiresAuthentication method will be used to know if that endpoint needs authentication.

提交回复
热议问题