简体   繁体   English

对预检请求的响应未通过访问控制检查:不存在“Access-Control-Allow-Origin”标头。 服务器错误

[英]Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present . Server error

I am using angular js in frontend and java hibernate at backend. 我在前端使用角度js,在后端使用java hibernate。 My website worked perfectly fine a few days ago but recently I made some changes in the table structure. 几天前我的网站工作得非常好,但最近我对表格结构进行了一些修改。 After those changes when i tried to integrate angular js I faced with the following error message. 在那些更改后,当我尝试集成角度js时,我面临以下错误消息。

XMLHttpRequest cannot load localhost:8080/SignInMainServlet. XMLHttpRequest无法加载localhost:8080 / SignInMainServlet。 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'localhost:3000' is therefore not allowed access. 因此不允许原点'localhost:3000'访问。

I read a bit about preflight request and header permissions but all possible solutions failed. 我读了一下有关预检请求和标题权限的内容,但所有可能的解决方案都失败了。

I have a index page which has some get requests and are working perfectly fine when I initially load the page or refresh it later. 我有一个索引页面,它有一些获取请求,并且在我最初加载页面或稍后刷新它时工作正常。 But as soon as I call the post request for SignInMainServlet the error pops up. 但是一旦我调用SignInMainServlet的post请求,就会弹出错误。 After this when I refresh my page none of the get request works and shows the same error. 在此之后,当我刷新页面时,get请求都不起作用并显示相同的错误。

Here is my post request 这是我的帖子请求

return $http({
            method: 'POST',
            url: 'http://localhost:8080/SignInMainServlet',
            data: $httpParamSerializerJQLike({
                login_source: source,
                accessToken: code,
                referrer_code: referral_id
            }),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
        });

And the server side for his request is : 服务器端的请求是:

String referrer_code=request.getParameter("referrer_code");
String login_source=request.getParameter("login_source");
String accessToken=request.getParameter("accessToken");

SignInMainManager m = new SignInMainManager(accessToken, referrer_code, login_source,REFERRAL_CODE_COUNT);
String result = m.signIn();


response.setContentType("text/html");  

response.addHeader("Access-Control-Allow-Origin","*");
response.addHeader("Access-Control-Allow-Methods"," GET, POST, OPTIONS");
response.addHeader("Access-Control-Allow-Headers","Content-Type");

PrintWriter out = response.getWriter(); 


out.println(result);
out.close();//closing the stream 

I monitored the network while sending the request and gets the following: 我在发送请求时监控网络并获得以下信息:

General 一般

Request URL:localhost:8080/SignInMainServlet Request Method:OPTIONS Status Code:200 OK Remote Address:127.0.0.1:8080 请求URL:localhost:8080 / SignInMainServlet请求方法:选项状态代码:200 OK远程地址:127.0.0.1:8080

Response Headers 响应标题

Allow:GET, HEAD, POST, TRACE, OPTIONS Content-Length:0 Date:Sun, 05 Feb 2017 10:49:56 GMT Server:Jetty(8.1.14.v20131031) 允许:GET,HEAD,POST,TRACE,OPTIONS内容长度:0日期:2017年2月5日星期日10:49:56 GMT服务器:Jetty(8.1.14.v20131031)

Request Headers 请求标题

Accept: / Accept-Encoding:gzip, deflate, sdch, br Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 Access-Control-Request-Headers:authorization Access-Control-Request-Method:POST Connection:keep-alive Host:localhost:8080 Origin:localhost:3000 Referer:localhost:3000/ User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 接受: / Accept-Encoding:gzip,deflate,sdch,br Accept-Language:en-GB,en-US; q = 0.8,en; q = 0.6 Access-Control-Request-Headers:authorization Access-Control-Request-方法:POST连接:keep-alive主机:localhost:8080原产地:localhost:3000 Referer:localhost:3000 / User-Agent:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 55.0。 2883.87 Safari / 537.36

I created this filter and now it's working with all requests. 我创建了这个过滤器,现在它正在处理所有请求。

package com.your.package;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;    

@WebFilter(asyncSupported = true, urlPatterns = { "/*" })
public class Filter implements javax.servlet.Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ((HttpServletResponse) response).addHeader("Access-Control-Allow-Origin", "*");
        ((HttpServletResponse) response).addHeader("Access-Control-Allow-Methods", "GET, OPTIONS, HEAD, PUT, POST");
        ((HttpServletResponse) response).addHeader("Access-Control-Allow-Headers", "Content-Type");
        ((HttpServletResponse) response).addHeader("Access-Control-Max-Age", "86400");
        // pass the request along the filter chain
        chain.doFilter(request, response);
    }

    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
}

If you're using a web.xml you can follow this. 如果您使用的是web.xml,则可以按照此处进行操作。 https://amodernstory.com/2014/12/27/using-cors-headers-with-java-example/ https://amodernstory.com/2014/12/27/using-cors-headers-with-java-example/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对预检请求的响应未通过访问控制检查:不存在“Access-Control-Allow-Origin”标头 - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present 如何修复对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-Origin'? - How to fix Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'? 如何处理“对预检请求的响应未通过没有访问控制允许来源 header 存在于请求的资源上”来自 angular - How to handle “Response to preflight request doesn't pass No Access-control-Allow-Origin header is present on requested resource ” from angular in BE 预检请求未通过访问控制检查:否 'Access-Control-Allow-Origin' - preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' 在飞行前响应中,Access-Control-Allow-Headers不允许在Request标头字段中使用cors enable Access-Control-Allow-Origin - cors enable in Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response 存在“ Access-Control-Allow-Origin”标头 - 'Access-Control-Allow-Origin' header is present 请求中不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header present in request CORS 策略已阻止从源访问 XMLHttpRequest:对预检请求的响应未通过访问控制检查: - Access to XMLHttpRequest from origin has been blocked by CORS policy: Response to preflight request doesn't pass access control check: CORS:对预检请求的响应未通过访问控制检查:预检请求不允许重定向 - CORS : Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request Angular 7 如何将 Access-Control-Allow-Origin 标头添加到预检发布请求? - Angular 7 How to add Access-Control-Allow-Origin header to preflight post request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM