Redirection in Custom Authentication Class

I am creating a custom local authentication class for OAuth 2.0 IDP.

When I want to change between authentication code and token I would need to get code and for that I need to authenticate on IDP.

I have managed to set-up my Access Manager so, that it starts class when I click on authentication method on login site. But when I want to redirect to login site of an IDP I get an error, saying that I can't redirect to IDP, because response has already been sent.

When I try with function "response.sendRedirect(myUrl)" in initializeRequest function I get this:

<amLogEntry> 2021-12-16T10:54:21Z DEBUG NIDS Application:
Method: NIDPServletContext.goJSP
Thread: ajp-nio-127.0.0.1-9019-exec-14
Forwarding to JSP: /jsp/top.jsp </amLogEntry>

<amLogEntry> 2021-12-16T10:54:21Z DEBUG NIDS Application:
Method: NIDPServletContext.goJSP
Thread: ajp-nio-127.0.0.1-9019-exec-14
Unable to forward to JSP: top
Exception message: "Cannot forward after response has been committed"

In browser I get the error:

CORS header 'Access-Control-Allow-Origin' missing

Even if I set Header policy before.

m_Response.setHeader("Access-Control-Allow-Origin", "*");

m_Response.sendRedirect(myUrl);