Реализация AutheticationEntrypoint просто записывает ответ JSON и отправляет клиенту выдается ошибка 403.
Проблема в том, что по какой-то причине, когда я пытаюсь получить доступ к защищенному ресурсу без учетных данных, запрос перенаправляется на /error, поскольку я можно увидеть в журналах (уровень DEBUG).
Код: Выделить всё
SecurityConfiguration.javaКод: Выделить всё
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests()
.anyRequest().authenticated()
.and()
.formLogin().disable()
.httpBasic().disable()
.csrf().disable()
.headers()
.frameOptions().sameOrigin()
.and()
.exceptionHandling()
.authenticationEntryPoint(new ApplicationAuthenticationEntryPoint())
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.build();
}
}
Код: Выделить всё
ApplicationAuthenticationEntryPoint.javaКод: Выделить всё
public class ApplicationAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write(mapper.createObjectNode()
.put("timestamp", LocalDateTime.now().toEpochSecond(ZoneOffset.of("-3")))
.put("message", "Access denied to resource")
.toString());
}
}
Код: Выделить всё
2023-03-10T14:59:49.528-03:00 DEBUG 58236 --- [nio-8080-exec-8] o.s.security.web.FilterChainProxy : Securing GET /test
2023-03-10T14:59:49.528-03:00 DEBUG 58236 --- [nio-8080-exec-8] o.s.s.w.a.AnonymousAuthenticationFilter : Set SecurityContextHolder to anonymous SecurityContext
2023-03-10T14:59:49.529-03:00 DEBUG 58236 --- [nio-8080-exec-8] o.s.security.web.FilterChainProxy : Securing GET /error
2023-03-10T14:59:49.529-03:00 DEBUG 58236 --- [nio-8080-exec-8] o.s.s.w.a.AnonymousAuthenticationFilter : Set SecurityContextHolder to anonymous SecurityContext
Является ли это /error страницей ошибок по умолчанию из Spring Boot? Если да, то как? Поскольку вся безопасность обеспечивается фильтрами, к моменту возникновения исключения аутентификации она еще не достигла стека MVC.
Возможно, мне не хватает знаний о том, как работает Spring Security.< /p>
Почему это происходит?
Подробнее здесь: https://stackoverflow.com/questions/756 ... -this-case