使用请求方法POST进行API调用时出现问题。预检OPTIONS方法失败。我在weblogic 12c上运行ATG。以下是我的CORS代码
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import atg.servlet.*;
import atg.servlet.pipeline.*;
public class CORSHeaderServlet extends InsertableServletImpl{
public CORSHeaderServlet () {}
public void service (DynamoHttpServletRequest request,
DynamoHttpServletResponse response)
throws IOException, ServletException
{
//add headers to response.
response.addHeader("Access-Control-Allow-Origin" ,"*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
response.addHeader("Access-Control-Allow-Headers","Origin, Content-Type, X-Auth-Token, X-PINGOTHER");
response.addHeader("Access-Control-Max-Age", "86000");
passRequest (request, response);
}
}
浏览器错误为
Access to XMLHttpRequest at 'http://localhost:7003/rest/model/atg/userprofiling/ProfileActor/login' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status
Java错误是
]] Root cause of ServletException.
javax.servlet.ServletException: The request method type is not supported: OPTIONS
at atg.rest.servlet.RestPipelineServlet.serviceRESTRequest(RestPipelineServlet.java:493)
at atg.rest.servlet.RestPipelineServlet.service(RestPipelineServlet.java:274)
at atg.servlet.pipeline.PipelineableServletImpl.passRequest(PipelineableServletImpl.java:157)
at atg.servlet.pipeline.PipelineableServletImpl.service(PipelineableServletImpl.java:320)
at atg.rest.servlet.RestPipelineServlet.service(RestPipelineServlet.java:278)
Truncated. see log file for complete stacktrace
对此将提供任何帮助。
答案 0 :(得分:0)
RestPipelineServlet
应该将请求传递给正确的RestProcessor
。查看RestProcessor
界面,它似乎仅实现您要使用的METHODS
的子集(doc)。因此,对于您的解决方案,您可能必须在doRESTOptions
中实现ActorProcessor
方法并从那里开始工作。
将其注入到应用程序中并非易事。在METHOD
中已实现的方法上也可能有RestPipelineServlet
可用的验证,您需要研究它们并进行覆盖/扩展。