Spring boot 获取正确的IP和协议[http/https]

8 篇文章 0 订阅

       在使用spring boot服务的时候,一般都会加反向代理,这时候并不能正确获取真实的IP,网上解决方案说的比较明确,都可以解决问题。很少有人会问如何获取http/https,因为很少人用程序获取真实的访问路径,可能都把地址加到配置文件里了。

       nginx的配置

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-Proto  $scheme;

       第四个配置是帮助spring boot获取正确协议头的配置。 同时也需要在application.yml里加增加一个配置,当然如果你直接从头上获取,也可以不增加配置。

server:
  forward-headers-strategy: native

参数是native和framework两者选择,当然还有none不做考虑。framework的话可以自己增加一个过滤器进行header的处理。

以上处理适合spring boot 2.2后的版本。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了对CoAP(Constrained Application Protocol)的支持。CoAP是一种专门用于物联网设备的轻量级通信协议,它可以在低带宽和网络带宽受限的环境下工作。 要使用Spring Boot与CoAP进行通信,你需要添加Spring Boot的CoAP依赖项。可以通过在Maven或Gradle构建文件中添加以下依赖项来实现: Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.eclipse.californium</groupId> <artifactId>californium-core</artifactId> <version>2.0.0-M7</version> </dependency> ``` Gradle: ```gradle dependencies { implementation 'org.springframework.boot:spring-boot-starter-integration' implementation 'org.eclipse.californium:californium-core:2.0.0-M7' } ``` 添加依赖项后,你可以通过创建一个CoapServer实例来启动一个CoAP服务器,并使用CoapClient实例与CoAP服务器进行通信。以下是一个简单的示例: ```java import org.eclipse.californium.core.CoapServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.integration.annotation.IntegrationComponentScan; import org.springframework.integration.annotation.MessagingGateway; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.coap.CoapHeaders; import org.springframework.integration.coap.inbound.CoapInboundGateway; import org.springframework.integration.coap.outbound.CoapOutboundGateway; import org.springframework.integration.dsl.IntegrationFlow; import org.springframework.integration.dsl.IntegrationFlows; import org.springframework.integration.ip.dsl.Tcp; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @SpringBootApplication @IntegrationComponentScan public class CoapApplication { public static void main(String[] args) { SpringApplication.run(CoapApplication.class, args); } @Bean public CoapServer coapServer() { CoapServer coapServer = new CoapServer(); coapServer.add(new HelloWorldResource()); return coapServer; } @Bean public IntegrationFlow coapInboundFlow() { return IntegrationFlows.from(coapInboundGateway()) .channel(coapRequestChannel()) .handle("myService", "handleCoapMessage") .channel(coapResponseChannel()) .handle(coapOutboundGateway()) .get(); } @Bean public CoapInboundGateway coapInboundGateway() { CoapInboundGateway coapInboundGateway = new CoapInboundGateway(); coapInboundGateway.setRequestChannel(coapRequestChannel()); coapInboundGateway.setPath("/hello"); return coapInboundGateway; } @Bean public MessageChannel coapRequestChannel() { return new DirectChannel(); } @Bean public MessageChannel coapResponseChannel() { return new DirectChannel(); } @Bean public CoapOutboundGateway coapOutboundGateway() { CoapOutboundGateway coapOutboundGateway = new CoapOutboundGateway("coap://localhost/hello"); coapOutboundGateway.setOutputChannel(coapResponseChannel()); return coapOutboundGateway; } @Bean public MyService myService() { return new MyService(); } public static class HelloWorldResource extends CoapResource { public HelloWorldResource() { super("hello"); } @Override public void handleGET(CoapExchange exchange) { exchange.respond("Hello, world!"); } } public static class MyService { @ServiceActivator public Message<String> handleCoapMessage(Message<?> message) { String payload = message.getPayload().toString(); String path = message.getHeaders().get(CoapHeaders.PATH).toString(); System.out.println("Received CoAP message with payload: " + payload + ", path: " + path); return new GenericMessage<>("Hello, world!"); } } @MessagingGateway(defaultRequestChannel = "coapRequestChannel") public interface CoapClient { @Tcp(host = "localhost", port = 5683) String send(String payload); } } ``` 在上面的示例中,我们创建了一个CoapServer实例,并将其添加到一个名为“hello”的CoAP资源中。该资源响应GET请求并返回“Hello, world!”字符串。 我们还创建了一个名为“myService”的Spring Bean,并在其中声明了一个@ServiceActivator方法,该方法接收一个CoAP消息并返回一个响应消息。 我们创建了一个CoapInboundGateway实例来接收来自CoAP客户端的请求,并将其转换为Spring Integration消息。我们还创建了一个CoapOutboundGateway实例,用于向CoAP服务器发送请求并将其转换为Spring Integration消息。 最后,我们创建了一个名为“CoapClient”的Spring集成MessagingGateway,用于向CoAP服务器发送请求。通过调用CoapClient的send方法,我们可以向CoAP服务器发送请求并获取响应。 这是一个简单的示例,演示了如何使用Spring Boot与CoAP进行通信。你可以根据需要进行更改和扩展,以满足你的具体需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值