我已经将keycloak适配器集成到了我的spring boot应用程序中,如文档中所述。工作流程正在运行。最初,我在属性中使用会话令牌存储,但我想摆脱它,成为无状态的。因此,我将选项更改为cookie令牌存储。
我可以看到我的Spring Boot应用程序确实在KEYCLOAK_ADAPTER_STATE cookie (screenshot)中发送了密钥罩访问令牌。不出所料,它被标记为HTTP cookie。
然而,当我查询我后端时,进一步的angular http请求不会发送cookie,即使我在查询中打开了withCredentials选项。
let options : RequestOptions = new RequestOptions();
options.withCredentials = true;
this.http.get('myservice', options)
如何在无状态http调用中使用该标记?
非常感谢!
发布于 2019-12-13 19:33:34
正如HTTP拦截器中所解释的,默认情况下,对于从应用程序到服务器的所有angular-keycloak documentation请求,HttpClient拦截器将添加格式为: Authorization的Authorization: Authorization。
您可以在HTTPClient拦截器配置中配置要从添加HTTP Authorization头部和密钥罩令牌中排除的请求URL。
HttpClient拦截器可以在app-init.ts (参见angular-keycloak文档)的密钥罩初始化函数中进行配置,如下所示;
import { KeycloakService } from 'keycloak-angular';
export function initializer(keycloak: KeycloakService): () => Promise<any> {
return (): Promise<any> => keycloak.init({
config: {
url: 'http://localhost:8080/auth',
realm: 'your-realm',
clientId: 'client-id'
},
initOptions: {
onLoad: 'login-required',
checkLoginIframe: false
},
enableBearerInterceptor: true,
bearerPrefix: 'Bearer',
bearerExcludedUrls: [
'/assets',
'/clients/public']
});
}
发布于 2018-03-24 02:46:42
angular HTTP拦截是@
/common/http的一个主要特性。通过拦截,您可以声明拦截器来检查和转换从应用程序到服务器的HTTP请求。同样的拦截器也可以在返回应用程序的过程中检查和转换服务器的响应。多个拦截器形成一个向前和向后的请求/响应处理程序链
从‘../auth.service’导入{ AuthService };
@Injectable()导出类AuthInterceptor实现了HttpInterceptor {构造函数(私有身份验证: AuthService) {} intercept(req: HttpRequest,next: HttpHandler) { //从服务获取身份验证令牌。const authToken = this.auth.getAuthorizationToken();//克隆请求并将原始headers替换为//克隆的headers,并根据授权更新。const authReq = req.clone({ headers: req.headers.set('Authorization',authToken) });//将带header的克隆请求发送给下一个处理器return next.handle(authReq);}}
..。
我认为您应该将
options.withCredentials = true;
设置为任何请求
import { Injectable } from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http';
import {Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/do';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private router: Router) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
withCredentials: true
});
return next
.handle(req)
.do(event => {
if (event instanceof HttpResponse) {}
}, (error: any) => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401) {
this.router.navigate(['/login']);
}
}
});
}
}
发布于 2018-03-24 17:11:57
我找到问题了。访问我的页面的初始请求URL是" /app /mymodule“,因此keycloak HTTP令牌被绑定到/app路径;但是我查询后端的JS http请求是”//myservice“,所以浏览器不会发送cookie,因为根路径不匹配。我将我的服务移到了/app/services下,现在令牌已按预期发送,并正确验证了:)。
https://stackoverflow.com/questions/49455720
复制相似问题