我的角度客户端与后端分离,我在后端启用了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) 我想我误解了用xmlhttprequest管理cookies.我有一个响应的服务器XMLHttpRequest在JavaScript做,我的服务器返回Allow-Control-Access-Origin,Access-Control-Allow-Headers,Access-Control-Expose-Headers并Access-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头?
当我在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中编程.
有没有人经历过同样的事情或有建议如何解决这个问题?
我有一个登录服务,执行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)
我看到我从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) 我想知道是否可以将 Spring Security 配置为在 OAuth 登录过程中使用外部托管的登录页面。没关系,这不是一个好主意,并且违背了 OAuth 的目的之一(将登录/凭据/身份验证问题与客户端和资源服务器分开)。
我有以下设置:
@EnableAuthorizationServer)我寻求的OAuth流程如下:
我可以配置 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 …
我有一个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 ×5
angular ×3
cors ×2
asp.net-core ×1
asp.net-mvc ×1
http ×1
http-post ×1
java ×1
javascript ×1
oauth-2.0 ×1
setcookie ×1