【问题标题】:Custom Authentication Entrypoint not being called on failed Authentication身份验证失败时未调用自定义身份验证入口点
【发布时间】:2020-02-21 12:51:57
【问题描述】:

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

在我的网络安全配置中:

@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/**”下的任何端点时,他们应该经过身份验证 - 事实上,他们是。

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

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

请问,我该如何解决?

【问题讨论】:

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


    【解决方案1】:

    在玩弄了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);
         }
    

    老实说,我不知道为什么我以前的东西不起作用。很多人都将其发布为解决入口端点问题的方法。但我想也许 Spring 发生了一些我不知道的变化。

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 2022-08-10
      • 2018-11-02
      • 1970-01-01
      • 2020-04-01
      • 2022-12-10
      • 2020-10-29
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多