Set custom login url in Spring Security UsernamePasswordAuthenticationFilter JWT authentication

前端 未结 2 1693
Happy的楠姐
Happy的楠姐 2020-12-29 14:38

I\'m following this auth0\'s tutorial to secure my application using JWT.

I\'ve ended up with the following WebSecurity configuration:

@EnableWebSecu         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 14:44

    You are extending org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter which itself extends org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter. In this last class, there is a setter called setFilterProcessesUrl which is intended to do just this:

    setFilterProcessesUrl

    public void setFilterProcessesUrl(String filterProcessesUrl)

    Sets the URL that determines if authentication is required

    Parameters: filterProcessesUrl

    This is the link to that javadoc section

    So in your WebSecurityConfigurerAdapter you could do just like this:

    @Bean
    public JWTAuthenticationFilter getJWTAuthenticationFilter() {
        final JWTAuthenticationFilter filter = new JWTAuthenticationFilter(authenticationManager());
        filter.setFilterProcessesUrl("/api/auth/login");
        return filter;
    }
    

    And then in your configure method in the same class just reference it instead of creating new instance:

    .addFilter(getJWTAuthenticationFilter())
    

提交回复
热议问题