转发后是否允许重定向请求?

发布于 2024-11-09 08:27:44 字数 244 浏览 0 评论 0原文

在某些情况下,我想强制注销用户。我正在使用 Spring Security,我知道如何执行此操作的唯一方法是转发/重定向到 /logout (或 Spring 侦听注销尝试的任何 URL)。由于理论上用户可以阻止其浏览器遵循重定向,因此我宁愿转发到注销 URL,因为执行注销逻辑非常重要。由于 Spring 总是会在(不)成功注销后进行重定向,我想知道这是否会成为问题。那么,简而言之,在请求转发后是否允许重定向,或者是否会导致 IllegalStateException?

In certain scenarios I want to forcefully logout a user. I'm using Spring Security and the only way I know how to do this is to forward/redirect to /logout (or whatever URL Spring listens to for logout attempts). Since in theory a user could stop his browser from following a redirect, I'd rather do a forward to the logout URL, as it's very important that the logout logic is carried out. Since Spring will always do a redirect after a (un)successful logout, I'm wondering if this will be a problem. So, in short, is redirecting allowed after the request has already been forwarded, or will it result in an IllegalStateException?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-11-16 08:27:44

那么,简而言之,请求转发后是否允许重定向,还是会导致 IllegalStateException?

不,绝对没问题。响应本身不知道转发——它纯粹发生在服务器的内部。转发只是一种将控制从一个服务器组件内部转移到另一个服务器组件的机制。

相反,您通常无法在重定向后转发,因为重定向“提交”了响应,并且无法撤消。

So, in short, is redirecting allowed after the request has already been forwarded, or will it result in an IllegalStateException?

No, it's absolutely fine. The response itself has no knowledge of the forwarding - it occurs purely within the internals of the server. Forwarding is simply a mechanism for internal transfer of control from one server component to another.

In contrast, you generally cannot forward after redirecting, since redirecting "commits" the response, and there's no undoing that.

伤痕我心 2024-11-16 08:27:44

仅当提交响应时,您才会收到 IllegalStateException。因此,如果转发的资源在重定向之前提交响应,那么您将得到 IllegalStateException。当响应标头已经发送时,响应被提交。当您向响应主体写入一个字节并刷新它时,可能会发生这种情况。重定向需要未提交的响应,因为重定向需要通过设置带有空白正文的 Location 标头来完成。

在良好的 MVC 方法中,JSP 是响应的一部分,因此每当您通过 scriptlet 或 JSTL ,那么你将面临 IllegalStateException 的风险。但如果您不在任何地方这样做,则无需担心。

You will only get an IllegalStateException when the reponse is committed. So if the forwarded resource commits the response before redirecting, then you will get IllegalStateException. The response is committed when the response headers are already been sent. This can happen when you write a byte to the response body and flush it. A redirect requires an uncommitted response because a redirect needs to be done by setting a Location header with a blank body.

In a decent MVC approach, the JSP is part of the response, so whenever you send a redirect from inside a JSP by either a scriptlet or a JSTL <c:redirect>, then you will risk IllegalStateException. But if you don't do that anywhere, there's nothing to worry about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文