【问题标题】:Forcing spring hateoas to generate https links instead of http强制spring hatoas生成https链接而不是http
【发布时间】:2016-10-17 17:48:55
【问题描述】:

我正在使用 spring-boot:1.3.3spring-hateoas:0.19.0spring-data-rest-core:2.4。 4.

{
  "_embedded" : {
    "projects" : [ {
      "status" : "ACTIVE",
      "storageRegion" : "US",
      "dataSize" : 96850,
      "freemiumUnits" : 1,
      "_links" : {
        "self" : {
          "href" : "http://example.com/x-region-us/api/data/projects/2c9f93b755359a4a015535c19b1f0006"
        },
        "project" : {
          "href" : "http://example.com/x-region-us/api/data/projects/2c9f93b755359a4a015535c19b1f0006"
        },

这是 spring-hateoas 提供的内容示例。过了一会儿,我将我的应用程序切换到 SSL

使用 traverson.js 通过“_links”jump(hop) 时出现问题。发生错误:

traverson.min.js:2 混合内容:页面位于 'https://example.com/project-new' 已通过 HTTPS 加载,但已请求 不安全的 XMLHttpRequest 端点 'http://example.com/x-region-us/api/data/submittalActions'。这 请求已被阻止;内容必须通过 HTTPS 提供。

有没有办法强制 spring 在 "_links 中通过 HTTP 生成 HTTPS 链接" json 的一部分?

【问题讨论】:

  • 当您通过 https 调用 REST 端点时会自动发生这种情况。
  • @zeroflagL 这不会发生。我通过https 调用端点,但是"_links" 属性中的所有href 都带有http 前缀...
  • 应用前面是否有代理或网络服务器?
  • 我猜客户端通过https调用nginx服务器,通过http调用Spring Boot。这将导致您描述的结果。在 SO 上有一个类似的问题,来自 Spring 开发人员的评论或回答。可以将某个标头设置为 AFAIR。
  • @Raca 如果您有兴趣:stackoverflow.com/questions/38464001/…

标签: https spring-boot hateoas spring-hateoas


【解决方案1】:

如果你使用 Apache Http Server,你需要在配置文件中添加这一行:

RequestHeader set X-Forwarded-Proto "https"

【讨论】:

  • 我认为应该更喜欢标准化版本:RequestHeader set Forwarded: proto=https X-... 标头在理论上是“实验性的”。两个版本都可以工作。
【解决方案2】:

将以下标题添加到 NginX

proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Prefix $http_x_forwarded_prefix;
proxy_set_header X-Forwarded-Host $http_x_forwarded_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Protocol https;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Url-Scheme https;
proxy_http_version 1.1;

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2015-02-09
    • 1970-01-01
    • 2021-09-12
    相关资源
    最近更新 更多