在Ionic应用程序中一起使用CORS和CSRF

bre*_*leq 12 csrf spring-security cors angularjs ionic-framework

我正在使用基于我使用Jhipster开发的AngularJS网站的Ionic Framework开发一个Android应用程序.由于我已经在我的Web应用程序中运行了服务器代码,因此我选择Ionic作为UI工作并在需要时调用服务器,但我在开发环境中遇到了一些问题.

  1. 当我使用Ionic 服务器运行我的应用程序时,我需要使用CORS向服务器发出请求.
  2. 我的Web应用程序是使用带有Spring Security的CSRF令牌开发的

我正在使用以这种方式配置的Apache CORS过滤器:

private void initCORSFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) {
    FilterRegistration.Dynamic corsFilter = servletContext.addFilter("cors", new CorsFilter());
    Map<String, String> parameters = new HashMap<>();
    parameters.put("cors.allowed.origins", "http://localhost:3000");
    parameters.put("cors.allowed.headers", "x-auth-token, x-requested-with, Content-Type, Accept, cache-control, x-csrf-token, Origin, Access-Control-Request-Method, Access-Control-Request-Headers");
    parameters.put("cors.allowed.methods", "POST, PUT, GET, DELETE");
    parameters.put("cors.exposed.headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
    parameters.put("cors.support.credentials", "true");
    corsFilter.setInitParameters(parameters);
    corsFilter.addMappingForUrlPatterns(disps, true, "/*");
}
Run Code Online (Sandbox Code Playgroud)

然后我使用angular-csrf-cross-domain插件来帮助跨域csrf请求:

.config(function ($urlRouterProvider,csrfCDProvider) {
    $urlRouterProvider.otherwise('/');
    //enable CSRF
    csrfCDProvider.setHeaderName('X-CSRF-TOKEN');
    csrfCDProvider.setCookieName('CSRF-TOKEN');
});
Run Code Online (Sandbox Code Playgroud)

然后我尝试向我的本地服务器发送一个帖子请求:

angular.module('consamiApp')
.factory('Register', function ($resource) {
    //globalURL is https://localhost:8080
    return $resource(globalURL+'api/register', {}, {
    });
});
.
.
.
createAccount: function (account, callback) {
    var cb = callback || angular.noop;

    return Register.save(account,
        function () {
            return cb(account);
        },
        function (err) {
            this.logout();
            return cb(err);
    }.bind(this)).$promise;
}
Run Code Online (Sandbox Code Playgroud)

但是我在firefox控制台中收到此消息:

跨源锁定请求:相同的源策略(同源策略)阻止在https:// localhost:8080/api/register中读取远程资源.(原因:CORS标题'Access-Control-Allow-Origin'不存在)

新的消息

当我提交我正在测试的表单时,AngularJs向服务器发出2个CORS请求:OPTIONS和POST,请求的结果是200 OK和403 Forbidden.这些是2个请求和响应的标头:

选项请求标头:

Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Origin: http://localhost:3000
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Run Code Online (Sandbox Code Playgroud)

选项答案标题:

Access-Control-Allow-Origin: http://localhost:3000
Content-Length: 0
Date: Tue, 30 Jun 2015 22:07:58 GMT
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=485A653AEAC8B8756DD3057BBF7FB862; Path=/; Secure; HttpOnly
CSRF-TOKEN=e8b3396c-63b2-47bf-9ad6-c1454628eb3b; Path=/
X-Application-Context: application:dev:8080
access-control-allow-credentials: true
access-control-allow-headers: origin,access-control-request-headers,x-requested-with,x-csrf-token,content-type,access-control-request-method,cache-control,x-auth-token,accept
access-control-allow-methods: POST
access-control-max-age: 1800
Run Code Online (Sandbox Code Playgroud)

POST请求标题:

Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: application/json, text/plain, */*
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
Referer: http://localhost:3000/
Content-Length: 109
Origin: http://localhost:3000
Cookie: _ga=GA1.1.123103160.1428358695; connect.sid=s%3AwD4KP4WBfhGO0JpFND3LpCzW.augts9fos9NMaZw%2B7XrNuilgaM8ocwSxaEUeDlIaVJ4; JSESSIONID=93200F4F4AFCEB28F10B130841808621
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Run Code Online (Sandbox Code Playgroud)

POST答案标题:

Content-Type: application/json;charset=UTF-8
Date: Tue, 30 Jun 2015 22:07:58 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)

有什么我没注意到的吗?该离子的官方博客表示,在部署应用程序时,但至少在测试中,我真的需要解决这个问题,我不应该担心CORS问题.你能给我任何选择吗?

aor*_*vre 5

您是否安装并配置了自Cordova 5.0.0以来CORS查询必需的cordova插件白名单

安装它:

cordova plugin add cordova-plugin-whitelist
Run Code Online (Sandbox Code Playgroud)

配置config.xml

您可以使用*保留当前设置,或更改限制性规则

在index.html上添加一个html策略,你也应该添加一个策略.要授权一切,这里是:

 <meta http-equiv="Content-Security-Policy" content="default-src *;
 style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'
 'unsafe-eval'"
Run Code Online (Sandbox Code Playgroud)

要验证您是否正确安装了插件,请使用以下命令将其检入您的cordova插件:

cordova plugins 
Run Code Online (Sandbox Code Playgroud)

您将在其中看到cordova-plugin-whitelist.

其次,您可能需要添加withCredentials到$ http(因此是$ resource)查询:

  .config(function($httpProvider) {
        $httpProvider.defaults.withCredentials = true;
    });
Run Code Online (Sandbox Code Playgroud)


bre*_*leq 3

当我编辑问题并看到带有 HttpOnly 子句的 OPTIONS 响应标头时,我开始相信问题出在我在开发环境中使用的自签名证书。

设置 Cookie:JSESSIONID=485A653AEAC8B8756DD3057BBF7FB862;路径=/;安全的; 仅HTTP

所以我决定在网络服务器中禁用 https 协议并且它工作正常。感谢您的帮助。