简体   繁体   中英

Using spring-HATEOAS behind Secure protocol https

I'm using HATEOAS for my REST API which is behind https, but link stay in http.

"links": [
      {
        "rel": "self",
        "href": "http://..."
      },
      {
        "rel": "object",
        "href": "..."
      }
    ]

Is it possible to config HATEOAS to point on my https ?

I resolved my problem by specifing the header as part of my virtual host configuration

<VirtualHost *:443>
    RequestHeader set X-Forwarded-Proto "https"
    ProxyPreserveHost On
    ServerName www.example.com

    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/server.crt
    SSLCertificateKeyFile /etc/apache2/ssl/server.key

    <Location/>
        SSLRequireSSL
    </Location>

    ProxyPass / http://127.0.0.1:9000/
    ProxyPassReverse / http://127.0.0.1:9000/
</VirtualHost>

Here's one way, via ControllerLinkBuilder :

Link selfLink = new Link(ControllerLinkBuilder.linkTo(methodOn(Mycontroller.class).someMethod()).toUriComponentsBuilder().scheme("https").build().toUriString(), Link.REL_SELF);

That should produce a Link with the self rel and a URI of the following form: https://host:port/contextroot/pathtocontrollermethod

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM