Код: Выделить всё
CookieValidationFilter
[*] с углового фронта, я делаю пост в безопасную конечную точку. Доступен в бэкэнд -запросе, что привело к:
[*]
Код: Выделить всё
401 Unauthorized< /code> < /p>
[list]
ошибки Cors < /li>
no-referrer
[/list]
[*] Однако, если я добавляю некоторые операторы ведения журнала отладки внутри фильтра - например, заголовки печати - проблема уходит, и она начинает работать, как и ожидалось. No change in frontend, backend, or browser config — just the presence of logging seems to change behavior.
[*]The frontend sets the SessionCookie correctly, and I can see it in browser Dev Tools under the "Cookies" tab.
[*]When the API fails, the request in the Network Tab не показывает файл cookie Header .
[*] Когда работает API (с добавленными журналами), cookie отправляется правильно, а бэкэнд обычно проходит. Безопасный; SameSite = none .
Мой код такой, как ниже:
1. ContentCachingRequestFilter :
Код: Выделить всё
@Component
public class ContentCachingRequestFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper((HttpServletRequest) servletRequest);
filterChain.doFilter(requestWrapper, servletResponse);
}
}
< /code>
[h4] 2. Cookievalidationfilter
Код: Выделить всё
@Component
public class CookieValidationFilter extends OncePerRequestFilter {
private final ObjectMapper objectMapper;
public CookieValidationFilter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String cookieHeader = request.getHeader(HttpHeaders.COOKIE);
boolean hasSessionCookie = cookieHeader != null && Arrays.stream(cookieHeader.split(";"))
.map(String::trim)
.anyMatch(c -> c.startsWith("SessionCookie=") && !c.equalsIgnoreCase("SessionCookie="));
if (!hasSessionCookie) {
ApiResponse apiResponse = ApiResponse.builder().httpStatus(Constants.UNAUTHORISED_ACCESS_CODE)
.statusMessage(Constants.UNAUTHORIZED_MSG)
.userMessage(Constants.YOU_ARE_NOT_AUTHORIZED_TO_ACCESS_THE_FEATURE_MSG).errorMessage(null)
.data(null).messageSeverity(null).validationErrors(null).build();
response.setStatus(Constants.UNAUTHORISED_ACCESS_CODE);
response.setContentType("application/json");
response.getWriter().write(objectMapper.writeValueAsString(apiResponse));
return;
}
filterChain.doFilter(request, response);
}
}
Почему доступ к заголовкам или ведению журнала (например, с request.getheadernames () ) повлияет на то, получен ли заголовок cookie , я получен в заповеднике, запрашивается, запрашивается, что запрашивает, что запрашивает, что запрашивает, что запрашивает, что -то запрашиваемое, что -то, что -то не запускает. Порядок фильтра, который объяснил бы это противоречивое поведение? ContentCachingRequestFilter < /li>
[*] Обеспечение правильной настройки CORS (
Код: Выделить всё
allowCredentials(true)
[*] Обеспечение использования фронтальных средств: true
[*] cookie отправляется с samesite = none; Безопасный; HttpOnly
Still, the behavior is inconsistent unless debug logging is added — which doesn't make sense.
Environment:
[*]Java 8
[*]Spring Boot 2.x
Tomcat Embedded < /li>
Работаю локально с включенными HTTPS < /li>
< /ul>
Любые идеи, почему это происходит или как обеспечить постоянную обработку cookie в пользовательских фильтрах, не полагаясь на журналы отборов?>
Подробнее здесь: https://stackoverflow.com/questions/796 ... -are-added