我有一个简单的 Spring MVC 应用程序,它将在某些 Controller 操作后将用户重定向到新页面。例如:
@Controller
public class ResponseController {
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveObj(HttpServletRequest request, @ModelAttribute(“someObject”) Object obj) {
// logic to validate and save object
return "redirect:/obj/" + id;
}
}
此 Spring MVC 应用程序与其他几个实例一起在负载平衡器后面运行。每个都有自己的主机名。负载均衡器配置了 SSL,但实例未配置。
我想知道的是覆盖默认重定向行为的最佳实践。目前,该应用程序以 https://mysite.com 开头。 ,但在用户执行需要重定向的操作后,应用程序会将用户重定向到 http://some.hostnameX.com 。
目前,我有一个属性用于帮助将用户重定向到 mysite.com。
loadbalancer.url=https://mysite.com
然后,在 Controller 中,我有以下内容:
@Value("loadbalancer.url")
String loadbalanerUrl;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveObj(HttpServletRequest request, @ModelAttribute(“someObject”) Object obj){
// logic to validate and save object
return "redirect:" + loadbalancerUrl + "/obj/" + id;
}
是否有更好的方法来覆盖此行为?
最佳答案
您还可以在每个实例上设置它(在它们自己的 app.properties
或 app.yml
中):
server.use-forward-headers=true
执行此操作,HttpServletRequest
对象将获取负载均衡器主机及其端口。
关于spring - 覆盖 SpringMVC 应用程序中的默认重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22844218/