IT/Spring

Spring, nginx proxy HATEOAS

민쌍 2021. 4. 7. 13:02

nginx, Spring Hateoas, proxy_pass

Proxy server 뒤에 backend Server 구성시 url forward

구성

frontend, proxy : nginx

backend : spring boot

문제

Spring HATEOAS 적용시 link url 부분은 proxy 부분이 제거된 상태로 return

요청 url : http://localhost/api/erp/inv/onhand/87/3203?direction=ASC&page=0&size=5&sort=seq,asc

"_links":{"first":{"href":"http://localhost/inv/onhand/87/3203?direction=ASC&page=0&size=5&sort=seq,asc"},"self":{"href":"http://localhost/inv/onhand/87/3203?direction=ASC&page=0&size=5&sort=seq,asc"},"next":{"href":"http://localhost/inv/onhand/87/3203?direction=ASC&page=1&size=5&sort=seq,asc"},"last":{"href":"http://localhost/inv/onhand/87/3203?direction=ASC&page=1&size=5&sort=seq,asc"}

요청 url - api/erp 부분

응답 links - api/erp 부분 누락

정상 결과

"_links":{"first":{"href":"http://localhost/api/erp/inv/onhand/87/3203?direction=ASC&page=0&size=5&sort=seq,asc"},"self":{"href":"http://localhost/api/erp/inv/onhand/87/3203?direction=ASC&page=0&size=5&sort=seq,asc"},"next":{"href":"http://localhost/api/erp/inv/onhand/87/3203?direction=ASC&page=1&size=5&sort=seq,asc"},"last":{"href":"http://localhost/api/erp/inv/onhand/87/3203?direction=ASC&page=1&size=5&sort=seq,asc"}

nginx -p 80

proxy_pass

/api/erp → localhost:34000

spring boot -p 34000

server {
        listen       80;
        server_name   localhost;
        access_log   logs/domain1.access.log  main;
        #charset koi8-r;  

        root dist; 

        location /api/erp {
            rewrite ^/api/erp(.*)$ $1 break;

            proxy_set_header Host              $host;
            proxy_set_header X-Real-IP         $remote_addr;
            proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Port  $server_port;
            **proxy_set_header X-Forwarded-Prefix /api/erp;**

            proxy_pass http://localhost:34000;
        }

 

 

package com.api.common;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ForwardedHeaderFilter;

@Configuration
public class HateoasConfig
{
    ***@Bean
    FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter()
    {
        FilterRegistrationBean<ForwardedHeaderFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new ForwardedHeaderFilter());
        return bean;
    }***
}

 

 


springdoc:
  version: '1.0.0'
  api-docs:
    path: /v3/api-docs
    groups:
      enabled: true
  swagger-ui:
    path: /swagger-ui.html
    operations-sorter: alpha
    tags-sorter: alpha
    disable-swagger-default-url: true
    display-query-params-without-oauth2: true
  paths-to-match: /**
  default-consumes-media-type: application/json
  default-produces-media-type: application/json

How to configure Spring HATEOAS behind proxy?

Centralize handling of "Forwarded" headers to ForwardedHeaderFilter [SPR-16668] · Issue #21209 · spring-projects/spring-framework

728x90
반응형