相关疑难解决方法(0)

AngularJS会忽略HTTP标头中的Set-Cookie

我正在开发一个基于客户端的AngularJS和服务器端的Java for my API(Tomcat + Jersey for WS)的应用程序.

我的API的某些路径受到限制,如果用户没有会话,则返回的响应状态为401.在客户端,拦截401 http状态以将用户重定向到登录页面.

用户通过身份验证后,我会在服务器端创建会话

httpRequest.getSession(true);
并且发送给客户端的响应确实在其标头中有Set-cookie指令:

Set-Cookie:JSESSIONID=XXXXXXXXXXXXXXXXXXXXX; Domain=localhost; Path=/api/; HttpOnly

问题是cookie永远不会放在客户端.当我检查localhost域的cookie时它是空的,因此下一个请求的头部没有这个cookie,客户端仍然无法访问我的API的受限路径.

客户端和服务器位于同一个域中,但它们没有相同的路径和相同的端口号:

客户: http://localhost:8000/app/index.html

服务器: http://localhost:8080/api/restricted/

附加信息:双方都启用了CORS:

"Access-Control-Allow-Methods", "GET, POST, OPTIONS"
"Access-Control-Allow-Origin", "*"
"Access-Control-Allow-Credentials", true

任何使Set-cookie正常工作的想法?这是与AngularJS相关的问题吗?

javascript cookies setcookie angularjs

67
推荐指数
5
解决办法
8万
查看次数

Angularjs $ http似乎不理解响应中的"Set-Cookie"

我有一个nodejs用Passport模块表示REST api进行身份验证.登录方法(GET)在标头中返回一个cookie.当我从Chrome调用它时,它运行正常,我的cookie在我的浏览器中设置.

但是如果我通过Angularjs的$ http调用它,则cookie不会被设置.

Set-Cookie:connect.sid=s%3Ad7cZf3DSnz-IbLA_eNjQr-YR.R%2FytSJyd9cEhX%2BTBkmAQ6WFcEHAuPJjdXk3oq3YyFfI; Path=/; HttpOnly
Run Code Online (Sandbox Code Playgroud)

如您所见,Set-Cookie存在于http服务响应的标头中.

也许HttpOnly可能是这个问题的根源?如果是,我该如何更改?这是我的快速配置:

app.configure(function () {
    app.use(allowCrossDomain);
    app.use(express.bodyParser());
    app.use(express.cookieParser())
    app.use(express.session({ secret: 'this is a secret' }));
    app.use(flash());
    //passport init
    app.use(passport.initialize());
    app.use(passport.session());
    app.set('port', process.env.PORT || 8080);
});
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助

node.js express angularjs

25
推荐指数
3
解决办法
2万
查看次数

xmlHttp.getResponseHeader +不适用于CORS

我在.NET 4上有一个asp.NET WCF.该服务用于验证用户.我们正在提交用户名和密码,然后应返回包含身份验证cookie的HTTP标头.使用本地托管的测试页面,这是正常的.我现在正在访问头域信息跨域.我已将测试页安装在另一台计算机上,并配置为调用WCF.呼叫正常,呼叫中的"数据"回复正确.但是,我无法使用以下任一方法访问标头信息:

alert(xmlHttp.getAllResponseHeaders());
Run Code Online (Sandbox Code Playgroud)

要么

alert(xmlHttp.getResponseHeader("Set-Cookie"));
Run Code Online (Sandbox Code Playgroud)

使用IE中的调试器和Firefox的"Live HTTP Header"插件,我可以看到正在返回标头信息.

在我的全局ajax页面中,我设置了处理CORS的响应.

private void EnableCrossDomainAjaxCall()
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");


    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {

        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");

        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我用来调用服务的AJAX:

$("#btnLogin").click(function(e) {
    var geturl;
    geturl = $.ajax({
        // type: "POST",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: 'http://10.0.4.66/AuthenticationService.svc/Login?Name=test&password=pwsd',
        // url: '../SecurityServer/AuthenticationService.svc/Login?Name=test&password=pwsd',
        dataType: "jsonp",
        error: function(request, status, error) {
            alert('Error Occured');
        },
        crossdomain: true,
        success: function(data, textStatus, xmlHttp) {
            // alert(xmlHttp.getResponseHeader("Content-Type"));
            document.write(xmlHttp.getResponseHeader("Content-Type") + "<br/>");
            alert(xmlHttp.getAllResponseHeaders()); …
Run Code Online (Sandbox Code Playgroud)

authentication wcf jquery wcf-security cors

22
推荐指数
2
解决办法
2万
查看次数

如何在Angular JS应用程序中处理身份验证

我正在我的角度js应用程序中实现一个auth系统.

我正在计划如下:

  1. 获取用户信息(登录表单中的名称和传递)
  2. 检查用户是否存在
  3. 如果存在服务器响应会话cookie,前端将重定向到某个页面.
  4. 然后用户将执行一些将生成API请求的任务
  5. API请求应包含在步骤3中发送的cookie信息
  6. 服务器检查cookie是否已生成,如果找到cookie,则响应API请求结果.在我的服务中,我正在做类似的事情
    MyApp.service('myAuth', function($http, $q) {
        this.authHeader = null;
        this.checkAuth = function(){
        //do api call and if success sets this.authHeader = response
        }
        this.isAuthenticaed = function(){
            this.authHeader ? return this.authHeder  : return false;
       }

提交登录表单后,我将调用checkAuth并从我的服务器返回我的会话cookie,如何在执行下一个REST调用时添加cookie信息,以及用户在登录后在整个应用程序中导航时我想检查每个时间isAuthenticaed是真还是假,在Angularjs导航到另一个页面时它是否会在第一次调用后设置为true后重置?我的方法是1-6好还是你有什么具体建议吗?顺便说一下,我检查了以前的条目,但那些不是我想知道的.

authentication cookies session angularjs

18
推荐指数
2
解决办法
4万
查看次数

缺少使用CORS响应jQuery请求的标头

我正试图用jquery购买执行CORS请求我得到了奇怪的行为.当我使用curl执行请求时,一切正常.但是当我使用jQuery时,事情发生了可怕的错误.在我的浏览器的"网络"选项卡中,我可以检查请求和来自后端的响应,一切都是正确的在此输入图像描述但是虽然我有Access-Control-Expose-Headers:Set-Cookie标头当我尝试使用console.log打印出来时我得到null(response.getResponseHeader("Set-Cookie")); 当我尝试使用console.log(response.getAllResponseHeaders())在控制台中打印出我的ajax请求的响应头时,我只得到这3个头:

Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Run Code Online (Sandbox Code Playgroud)

我用来执行请求的代码是:

$.ajax({
    method: "POST",
    url: "http://localhost:8808/storage/login",
    data: {
        username: email,
        password: password,
    },
    success: function(data, textStatus, response){
        console.log(response.getAllResponseHeaders());
        console.log(response.getResponseHeader("Set-Cookie"));
        this.setState({showAlert: true, alertMessage: "You are logged in.", alertType: "success"});
    }.bind(this),
    error: function (xhr, status, err) {
        this.setState({showAlert: true, alertMessage: "Wrong email or password", alertType: "danger"});
    }.bind(this)
});
Run Code Online (Sandbox Code Playgroud)

ajax jquery header response reactjs

7
推荐指数
2
解决办法
3061
查看次数