【问题标题】:Basic Authentication when redirecting to external url in JSF重定向到 JSF 中的外部 url 时的基本身份验证
【发布时间】:2019-03-21 23:00:46
【问题描述】:

重定向到 JSF 中的外部 url 时如何向请求添加基本身份验证。

我需要从 JSF 应用程序单击按钮下载报告(由 SSRS 提供的 Excel 格式),报告的 url 格式为 -

http://report-server-host/ReportServer/Pages/ReportViewer.aspx?reportName&rs:Format=Excel

我找到了一种使用 FacesContext 从支持 bean 重定向到此页面的方法。

FacesContext.getCurrentInstance().getExternalContext().redirect(path); 

但是report-server需要基本认证,做这个重定向的时候怎么加?

【问题讨论】:

  • 外部上下文是一个 servlet-context。有效地使这甚至不是一个 jsf 问题。

标签: jsf redirect servlets


【解决方案1】:

您可以像这样在 URL 本身中添加用户名和密码

http://username:password@report-server-host/ReportServer/Pages/ReportViewer.aspx?reportName&rs:Format=Excel


要将基本身份验证标头转换为用户名:密码格式,请使用 Base64 解码器。

如果标头类似于Basic {TOKEN},其中{TOKEN} 是Base64 编码字符串(类似于dXNlcm5hbWU6cGFzc3dvcmQ=),那么重定向URL 应该是

  String auth = new String(Base64.getDecoder().decode(TOKEN));

  path = path.replaceFirst("://", "://" + auth + "@");

  FacesContext.getCurrentInstance().getExternalContext().redirect(path);

由于我没有评论的声誉,所以我在这里回复您的评论。 username:password@ 部分不需要进行 URL 转义。

【讨论】:

  • 谢谢,但用户名中包含“/”,密码中包含“@”。如何转义这些字符?
  • 我在添加编码后的用户名:密码后尝试打开网址,浏览器仍然再次要求用户/密码组合。
  • 现在我的网址是 - token@10.0.0.34/ReportServer/Pages/…
猜你喜欢
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2011-07-02
相关资源
最近更新 更多