Цель: возвращать пользовательское сообщение об ошибке при указании неверного токена (например, «123»). Кажется, мой accessDeniedHandler не вызывается. Напротив, когда я вообще не указываю JWT, он работает нормально и возвращается пользовательское сообщение об ошибке (
Код: Выделить всё
authenticationEntryPointЭто код, который я использую (я изменил пример Microsoft, поскольку в нем использовались устаревшие методы):
Код: Выделить всё
@Autowired
private ErrorHandler errorHandler;
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrfCustomizer -> csrfCustomizer.disable())
.with(AadResourceServerHttpSecurityConfigurer
.aadResourceServer(), Customizer.withDefaults())
.authorizeHttpRequests(requests -> requests
.requestsMatchers(antMatcher("/users/**")).permitAll()
.requestMatchers(antMatcher(HttpMethod.GET, "/admin")).hasRole(ADMIN_ROLE)
.anyRequest().authenticated()
)
.exceptionHandling(exceptionHandlingCustomizer -> exceptionHandlingCustomizer.authenticationEntryPoint(errorHandler))
.exceptionHandling(exceptionHandlingCustomizer -> exceptionHandlingCustomizer.accessDeniedHandler(errorHandler))
.sessionManagement(sessionManagement -> sessionManagementCustomizer.sessionCreationPolicy(sessionCreationPolicy.STATELESS)));
return http.build();
}
Код: Выделить всё
@Configuration
public class ErrorHandler authenticationEntryPoint, AccessDeniedHandler {
@Override
public void commence() {
// this is called
}
@Override
public void handle() {
// this is not called
}
}
Код: Выделить всё
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrfCustomizer -> csrfCustomizer.disable())
.oauth2ResourceServer(httpSecurityOAuth2ResourceServerConfigurer -> httpSecurityOAuth2ResourceServerConfigurer
.authenticationEntryPoint(errorHandler)
.accessDeniedHandler(errorHandler)
)
.authorizeHttpRequests(requests -> requests
.requestsMatchers(antMatcher("/users/**")).permitAll()
.requestMatchers(antMatcher(HttpMethod.GET, "/admin")).hasRole(ADMIN_ROLE)
.anyRequest().authenticated()
)
.sessionManagement(sessionManagement -> sessionManagementCustomizer.sessionCreationPolicy(sessionCreationPolicy.STATELESS)));
return http.build();
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... yconfigure
Мобильная версия