相关疑难解决方法(0)

Angular 4 - 在每个请求中设置withCredentials - cors cookie

我的角度客户端与后端分离,我在后端启用了cors,一切正常,但我的身份验证失败,因为cookie没有添加到请求中.

在线搜索后,我发现我应该设置{withCredentials : true}每个http请求.我设法在一个请求上执行它并且它可以工作,但不是在所有请求上.

我尝试使用BrowserXhr 如何在Angular2中的所有请求的请求标头中发送"Cookie"?但它不起作用,它也被弃用了.

我也尝试过RequestOptions但它没有用.

如何在每个http请求中设置{withCredentials:true}?

后来编辑:

@Injectable()
export class ConfigInterceptor implements HttpInterceptor {

  constructor(private csrfService: CSRFService) {

  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    let token = this.csrfService.getCSRF() as string;
    const credentialsReq = req.clone({withCredentials : true, setHeaders: { "X-XSRF-TOKEN": token } });
    return next.handle(credentialsReq);
  }
}
Run Code Online (Sandbox Code Playgroud)

cookies http cors oauth-2.0 angular

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

xmlhttprequest和set-cookie&cookie

我想我误解了用xmlhttprequest管理cookies.我有一个响应的服务器XMLHttpRequest在JavaScript做,我的服务器返回Allow-Control-Access-Origin,Access-Control-Allow-Headers,Access-Control-Expose-HeadersAccess-Control-Allow-Credentials用正确的值标题.

我正在使用javascript在服务器上进行摘要身份验证,没有问题,我收到WWW-Authenticate来自服务器的标题,我处理并向服务器发送带有所有摘要响应的授权标头,一切正常.问题是,当摘要挑战成功时,我的服务器返回一个Set-Cookie标头,我必须得到它并添加到我的所有xhr请求的其余部分.浏览器(使用Chromium和Chrome)不允许我访问标题:

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

好吧,在XMLHTTPREQUEST Level 2中它说:"返回响应中的所有头文件,除了那些字段名称为Set-Cookie或Set-Cookie2的头文件"好的,所以我不能接受它,但有什么办法?使用Chrome Api进行Cookie(目前我还没有注意到它的注意事项),但我希望以标准的方式做到可行.随着:

xhr.withCredentials = true;
Run Code Online (Sandbox Code Playgroud)

意味着浏览器自动获取set-cookie并发送cookie头?

javascript cookies xmlhttprequest setcookie cors

14
推荐指数
1
解决办法
3万
查看次数

没有为Angular2发布请求设置响应中的Set-cookie

当我在Angular2中发出put请求时,我在响应中收到了预期的set-cookie.但是我的浏览器(尝试过Chrome和Firefox)都拒绝设置cookie.

相反,当我使用Angular 1应用程序调用同一 API端点时,cookie被正确设置.

响应标头是:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://example.com
Allow:GET, PUT, HEAD, OPTIONS
Content-Type:application/json
Date:Thu, 28 Jan 2016 14:41:38 GMT
P3P:policyref="http://www.example.com/p3p.xml", CP="NON DSP COR CURa TIA"
Server:WSGIServer/0.1 Python/2.7.6
Set-Cookie:sessionid=994wl49qfsizog5bqmt57sgx9q2toa25; expires=Mon, 28-Mar-2016 14:41:37 GMT; Max-Age=5183999; Path=/
Set-Cookie:csrf=u7UQhpAphTsGYKRU6jFlLFt6NoYAhNMS; Domain=api.example.com; expires=Thu, 26-Jan-2017 14:41:38 GMT; Max-Age=31449600; Path=/
Vary:Accept, Cookie
Run Code Online (Sandbox Code Playgroud)

后端在Django 1.8中编程.

有没有人经历过同样的事情或有建议如何解决这个问题?

cookies angular

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

设置请求cookie angular2 http post请求

我有一个登录服务,执行http请求(到PHP后端服务器)并返回一个cookie.登录后,需要在客户端进行的所有进一步请求中使用此cookie.

login服务:

   login(userCredentials:UserCredentials):Observable<any> {    
        this.request.ServiceType = "Connect"
        this.request.SessionId = "sessionlessrequest"
        this.request.Compression = "no"
        this.request.Parameters = userCredentials;
        let jsonRequest = JSON.stringify(this.request)
        return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
            { headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded'
            })
            }).map( (responseData) => {
            this.apiResponse = responseData.json() as ApiResponse
            if (!this.apiResponse.Error) {
                this.sessionData.next(this.apiResponse.Data as UserSessionData)
                this.sessionData.asObservable().share
            } else {
                console.log(this.apiResponse.ErrorMessage)
            }
            return null
        })
Run Code Online (Sandbox Code Playgroud)

执行此调用时,cookie响应如下所示:
的getCookie

我看到我从session_start获取session_id(在我的php服务器上)

我在做请求的时候:

searchExams(searchObject:SearchObject):Observable<any>{
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded',)
        let options = new RequestOptions({ headers: headers, withCredentials: true});
        this.request.ServiceType = ServiceType.SearchExams;
        this.request.SessionId = …
Run Code Online (Sandbox Code Playgroud)

cookies http-post session-cookies angular

7
推荐指数
1
解决办法
1万
查看次数

使用外部登录页面的 Spring Security OAuth2 登录

我想知道是否可以将 Spring Security 配置为在 OAuth 登录过程中使用外部托管的登录页面。没关系,这不是一个好主意,并且违背了 OAuth 的目的之一(将登录/凭据/身份验证问题与客户端和资源服务器分开)。

我有以下设置:

  • 客户端 SPA 应用程序,我要使用的登录页面位于其中
  • OAuth Auth 服务器(使用 Spring Security OAuth / @EnableAuthorizationServer
  • 一台或多台 OAuth 资源服务器

我寻求的OAuth流程如下:

  • 用户尝试访问 SPA 中的安全路由
  • 客户端将用户重定向到OAuth服务器的授权页面(参数包括状态、随机数等)
  • OAuth 服务器未检测到令牌并重定向到 SPA 的登录页面
  • 用户登录;发布到登录 url(登录 url 位于 OAuth 服务器上;需要 CORS 配置以允许从 SPA 到 OAuth 服务器的跨源发布)
  • 登陆成功; Spring使用最初存在的参数重定向到最初请求的授权页面
  • 授权重定向到令牌;用户获得令牌;SPA 检测到用户现在拥有令牌并允许访问安全路由

我可以配置 Spring Security 来指定绝对 URL,以便重定向到 SPA 的登录页面:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
        .and()
        ... config ...
        .and()
        .formLogin()
        .loginPage("http://angular-spa-app/login") 
        .loginProcessingUrl("/login")
        .permitAll();
}
Run Code Online (Sandbox Code Playgroud)

当我完成此流程时,我看到第一个请求发送到oauth/authorize?state=...&nonce=...&etc...,然后重定向到http://angular-spa-app/login。但是当我登录时,Spring …

java spring-security spring-security-oauth2

5
推荐指数
1
解决办法
4537
查看次数

为什么我的浏览器不存储ASP MVC CORE 2 cookie?

我有一个Angular(4)客户端(localhost:4200),它调用ASP MVC CORE 2 WebApi.其中一个调用http://localhost:5000/api/session/resume返回一个cookie以及响应.

在动作方法中,我已经返回3个cookie用于测试目的.

    [AllowAnonymous, HttpPost, Route("api/session/resume")]
    public async Task<AccountSignInResponse> Resume([FromBody]SessionResumeCommand command)
    {
        AccountSignInResponse apiResponse = await Mediator.Send(command);
        if (!apiResponse.HasErrors) {
        Response.Cookies.Append("TestCookie", ..., new CookieOptions
        {
            Domain = "localhost",
            Expires = DateTimeOffset.Now.AddDays(100),
            HttpOnly = false
        });
        Response.Cookies.Append("TestCookie4200", ..., new CookieOptions
        {
            Domain = "localhost:4200",
            Expires = DateTimeOffset.Now.AddDays(100),
            HttpOnly = false
        });
        Response.Cookies.Append("TestCookie5000", ..., new CookieOptions
        {
            Domain = "localhost:5000",
            Expires = DateTimeOffset.Now.AddDays(100),
            HttpOnly = false
        });            }
        return apiResponse;
    }
Run Code Online (Sandbox Code Playgroud)

此请求的标头是

Request URL:http://localhost:5000/api/session/resume
Request Method:POST
Status …
Run Code Online (Sandbox Code Playgroud)

cookies asp.net-mvc asp.net-web-api asp.net-core

2
推荐指数
1
解决办法
1047
查看次数