失败的身份验证未调用自定义身份验证入口点

时间:2020-02-20 07:53:53

标签: spring-boot spring-security oauth-2.0

我已经设置了OAUTH授权服务器,该服务器应该允许客户端请求令牌。还应该允许管理员用户执行其他操作。

在我的Web安全配置中:

@Configuration
@EnableWebSecurity
public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
    private @Autowired CustomAuthenticationProvider authenticationProvider;
    private @Autowired CustomAuthenticationEntryPoint entryPoint;

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().httpBasic().and().cors().and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(entryPoint)
                .defaultAuthenticationEntryPointFor(entryPoint, new AntPathRequestMatcher("/api/v1/**"));
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }
}

理想情况下,当管理员用户尝试在“ / api / v1 / **”下调用任何终结点时,它们应该经过身份验证-实际上,它们是经过验证的。

现在的问题是,当身份验证失败时,身份验证条目端点将被忽略。我不明白为什么会这样。

我什至包括了“的默认身份验证入口点”,以查看是否有帮助,但这没有帮助。

请,我该如何解决?

1 个答案:

答案 0 :(得分:0)

在研究了http安全配置之后,我从本文(https://www.baeldung.com/spring-security-basic-authentication)中汲取了灵感,并将其更改为:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().cors().and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic().authenticationEntryPoint(entryPoint);
     }

老实说,我不知道为什么以前没有用过。许多人已经将其发布为解决入口端点问题的解决方案。但是我想也许春天里发生了一些我不知道的变化。