我需要使用 url 中的令牌 id(或者可能在请求标头中 - 但这暂时不重要)来验证一些休息服务。我正在尝试使用 java 配置来设置它,并以此帖子为指导。我的问题是我不知道如何处理提供者身份验证失败时抛出的“BadCredentialsException”。这是我的安全配置:
public static class SecurityConfigForRS extends
WebSecurityConfigurerAdapter {
@Autowired
TokenAuthenticationProvider tokenAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.authenticationProvider(tokenAuthenticationProvider);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean()
throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.regexMatcher("^/rest.*")
.addFilterBefore(
new TokenAuthenticationFilter(
authenticationManagerBean()),
AbstractPreAuthenticatedProcessingFilter.class)
.and().csrf().disable();
}
}
现在我跳过其他实现 - 如果它有帮助,我将在稍后发布它们。
当令牌丢失或无效时,TokenAuthernticationProvider
抛出一个BadCredentialsException
. 我需要抓住这个并发回一个401-Unauthorized
. 是否有可能做到这一点?