繁体   English   中英

AngularJS,Spring Social和CORS

[英]AngularJS, Spring Social and CORS

我有一个使用Spring和Spring Social构建的应用程序。

我能够连接到社交网络,效果很好。 然后,我有了一个好主意,那就是在前端使用angular而不是jsp,并着手替换我以前用Angular使用http帖子连接到社交网络的html表单。

现在我得到了错误

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/auth?client_id=233119510011-epdnrh651t…%2Fconnect%2Fgoogle&scope=email&state=e29561c9-a538-47dd-8b92-ca0aaa8b413b. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. 

表单帖子可以工作,但是angularjs帖子不能。 是什么赋予了? 我使用fidler查看了失败的角度发布,将其与工作的html表单发布进行了比较,发现了这一点。

使用html表单工作POST

 POST http://localhost:8080/connect/google HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 54
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:8080/myscene/account-settings
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: _ga=GA1.1.679586999.1424266167; JSESSIONID=74A29A5F0B04B937281A7FAC4DD7B102
X-Forwarded-For: 12.13.14.15

_csrf=c71a3881-d272-42de-b8b1-c7647f7e7609&scope=email

这是发布该帖子的代码

<form action='<c:url value="/connect/google" />' method="POST">
                        <input name="${_csrf.parameterName}"
                            value="${_csrf.token}" /> <input name="scope"
                            value="email" />
                        <button type="submit" class="common_button">Connect with
                            Google</button>
                    </form>

使用angularjs不起作用的POST

POST http://localhost:8080/connect/google HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 54
Origin: http://localhost:8080
X-XSRF-TOKEN: c71a3881-d272-42de-b8b1-c7647f7e7609
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Cache-Control: max-age=0
Referer: http://localhost:8080/myscene/account-settings
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: _ga=GA1.1.679586999.1424266167; JSESSIONID=74A29A5F0B04B937281A7FAC4DD7B102
X-Forwarded-For: 12.13.14.15

_csrf=c71a3881-d272-42de-b8b1-c7647f7e7609&scope=email

这是发布该帖子的代码

$http({
                  method  : 'POST',
                  url     : '/connect/google',
                  data    : $.param(oauth2Scope),  // pass in data as strings
                  headers : { 
                                'Content-Type': 'application/x-www-form-urlencoded',
                                'X-XSRF-TOKEN'   : token,
                                'Cache-Control'  : 'max-age=0',
                                'Accept'         : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
                            }  
                 })

我在正确的轨道上吗? 我发现这很棘手,因此任何帮助都值得欢迎! 希望有人可以指出正确的方向。

编辑1:

我已经创建了过滤器服务器端,但是一切都无法正常进行,因此还需要做其他事情。

编辑2:

另外,值得注意的是,产生错误的请求是Spring Social Framework对Google服务器的GET。 我导致问题的角度POST是到localhost:8080,由Spring Social处理。

编辑3:所以我已经注意到,仅当spring social将GET发送到google / facebook而不是我自己的网站时,这才是问题。 对于我自己的网站,我可以使用角度罚款来提交ajax帖子。

另外,我注意到无法正常使用Google的GET包含Origin,而无法正常使用Google的GET。 这可能是问题吗?

过滤

@Component
public class SimpleCORSFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) resp;

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {
    }

}

您的servlet / rest服务必须设置CORS标头,并且我注意到$http在调用之前检查了HTTP OPTIONS。

添加此功能的最佳方法是添加标题条目的过滤器。

请参阅Tomcat CORS过滤器

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM