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



我已经设置了一个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/**"下调用任何端点时,他们都应该经过身份验证——事实上,他们确实经过了身份验证。

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

我甚至加入了"的默认身份验证入口点",只是想看看这是否有帮助,但没有。

请问,我该如何解决这个问题?

在尝试了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);
}

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

最新更新