AngularJS,Spring Social和CORS

时间:2015-03-10 17:53:19

标签: java spring spring-security cors spring-social

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

我能够连接到社交网络,它运作良好。然后我有一个好主意,在前端而不是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社交框架对Google服务器的GET。导致问题的我的角度POST是localhost:8080并由Spring Social处理。

编辑3: 所以我注意到这只是一个问题,只有当春季社交发送GET到google / facebook而不是我自己的网站时。对于我自己的网站,我可以使用angular fine提交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() {
    }

}

1 个答案:

答案 0 :(得分:1)

您的servlet / rest服务必须设置CORS标头,我注意到$http在呼叫之前检查http OPTIONS。

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

请参阅Tomcat CORS filter