从Spring Boot中的重定向后获取原始URL

huangapple go评论70阅读模式
英文:

Get original url from after redirect in Spring Boot

问题

有没有办法从Spring Boot中的外部服务获取原始重定向URL?例如,如果http://example.com/redirect将重定向到我的应用程序的路由http://localhost/test,我是否可以提取"http://example.com/redirect"?

英文:

Are there any ways to get original redired URL from external service in Spring Boot? for example, if http://example.com/redirect redirects to my application with route http://localhost/test, can I extract "http://example.com/redirect"?

答案1

得分: 2

Short answer: 不。

This isn't really a Spring or Java question, so I won't address that.

Any information you need should be present in your redirect URL. Typically, this is useful in scenarios where you would want to redirect a user back to a specific page after performing an action.

Say I try to access /user-profile but I'm not logged in. I would be redirected to /login since /user-profile requires that a user be logged in. However, for a seamless user experience, I'd prefer to send the user back to /user-profile after successful login.

To achieve this, I wouldn't actually redirect the user to /login, but /login?return_to=%2Fuser-profile. Now, when I receive the request at /login, I look for the return_to parameter. That tells me where the user came from, and I'm able to send them back to that location after they have successfully logged in.

英文:

Short answer: No.

This isn't really a Spring or Java question, so I won't address that.

Any information you need should be present in your redirect URL. Typically, this is useful in scenarios where you would want to redirect a user back to a specific page after performing an action.

Say I try to access /user-profile but I'm not logged in. I would be redirected to /login since /user-profile requires that a user be logged in. However, for a seamless user experience, I'd prefer to send the user back to /user-profile after successful login.

To achieve this, I wouldn't actually redirect the user to /login, but /login?return_to=%2Fuser-profile. Now, when I receive the request at /login, I look for the return_to parameter. That tells me where the user came from, and I'm able to send them back to that location after they have successfully logged in.

huangapple
  • 本文由 发表于 2023年6月21日 23:58:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定