Spring Boot HTTPS重定向问题

我们有个项目使用spring boot开发, 使用nginx做反向代理,在nginx里边配置了https证书,但是在重定向的时候,老是重定向到http,解决方法如下:

1.在nginx中添加如下配置

 proxy_set_header X-Forwarded-Proto $scheme;

比如

 location /xxx/{
	 client_max_body_size 50m;
	 proxy_set_header X-Forwarded-Proto $scheme;
	 proxy_pass http://127.0.0.1:7002/xxx/;
  }

2.在application.properties添加如下配置

server.use-forward-headers=true
server.tomcat.protocol-header=X-Forwarded-Proto

完美解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用RedirectView或重定向方法来实现重定向并添加请求头。以下是两个简单的示例: 1. 使用RedirectView ``` @GetMapping("/redirect-with-header") public RedirectView redirectWithHeader() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("http://www.example.com/"); redirectView.setExposeModelAttributes(false); redirectView.addStaticAttribute("headerName", "headerValue"); return redirectView; } ``` 在上面的示例中,首先创建一个RedirectView实例,并设置要重定向到的URL。然后,可以使用addStaticAttribute方法向重定向请求中添加请求头。这里将添加一个名为“headerName”的请求头,值为“headerValue”。 2. 使用重定向方法 ``` @GetMapping("/redirect-with-header") public ResponseEntity<Void> redirectWithHeader() { HttpHeaders headers = new HttpHeaders(); headers.add("headerName", "headerValue"); return ResponseEntity.status(HttpStatus.FOUND) .headers(headers) .location(URI.create("http://www.example.com/")) .build(); } ``` 在上面的示例中,首先创建一个HttpHeaders实例,并添加一个名为“headerName”的请求头,值为“headerValue”。然后,使用ResponseEntity构建器返回一个HTTP 302(FOUND)响应,并将请求头和重定向目标URL(使用URI.create方法创建)添加到响应中。 注意:使用RedirectView或重定向方法都可以实现重定向并添加请求头,具体取决于实际需求和个人偏好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值