Merge pull request #3157 from DSpace/backport-3152-to-dspace-8_x

[Port dspace-8_x] Forward client's user-agent instead of Node's
This commit is contained in:
Tim Donohue
2024-07-03 13:51:46 -05:00
committed by GitHub

View File

@@ -27,6 +27,14 @@ export class ForwardClientIpInterceptor implements HttpInterceptor {
*/
intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const clientIp = this.req.get('x-forwarded-for') || this.req.connection.remoteAddress;
return next.handle(httpRequest.clone({ setHeaders: { 'X-Forwarded-For': clientIp } }));
const headers = { 'X-Forwarded-For': clientIp };
// if the request has a user-agent retain it
const userAgent = this.req.get('user-agent');
if (userAgent) {
headers['User-Agent'] = userAgent;
}
return next.handle(httpRequest.clone({ setHeaders: headers }));
}
}