Spring security filter not firing up

前端 未结 2 1659
孤城傲影
孤城傲影 2020-12-22 02:54

My authentication filters are not firing up under request.

I have 2 security configurations, one for the login endpoint only, authenticating with the Authentic

2条回答
  •  臣服心动
    2020-12-22 03:19

    Your filters extend UsernamePasswordAuthenticationFilter and this filter by default is only applied for URL /login, see UsernamePasswordAuthenticationFilter:

    This filter by default responds to the URL /login.

    If you want to change the default to another URL, see AbstractAuthenticationProcessingFilter#setFilterProcessesUrl:

    Sets the URL that determines if authentication is required

    Your modified code:

    public AuthenticationFromCredentialsFilter authenticationFromCredentialsFilter() throws Exception {
        AuthenticationFromCredentialsFilter authenticationFromCredentialsFilter = new AuthenticationFromCredentialsFilter();
        authenticationFromCredentialsFilter.setAuthenticationManager(authenticationManagerBean());
        authenticationFromCredentialsFilter.setFilterProcessesUrl("/api/users/login");
        return authenticationFromCredentialsFilter;
    }
    

    If you want to use a pattern, see AbstractAuthenticationProcessingFilter:

    This filter will intercept a request and attempt to perform authentication from that request if the request matches the setRequiresAuthenticationRequestMatcher(RequestMatcher).

    Your modified code:

    public AuthenticationFromTokenFilter authenticationFromTokenFilter() throws Exception {
        AuthenticationFromTokenFilter authenticationFromTokenFilter= new AuthenticationFromTokenFilter();
        authenticationFromTokenFilter.setAuthenticationManager(authenticationManagerBean());
        authenticationFromTokenFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/api/**");
        return authenticationFromTokenFilter;
    }
    

提交回复
热议问题