将他们在登录前查看的内容重定向回实际页面 [英] Redirecting back to actual page what they were viewing before login

查看:21
本文介绍了将他们在登录前查看的内容重定向回实际页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的用户被重定向到登录页面,如何使他们在登录后被重定向到 -original- 目标页面(他们第一次点击的页面)?

If my user is redirected to a login page, how to make it so that, after they log in, they get redirected to the -original- destination page (the one they first clicked on)?

推荐答案

在重定向到登录页面时,将最初请求的 URL 作为请求参数传递.

Pass the originally requested URL as request parameter on the redirect to the login page.

String from = URLEncoder.encode(request.getRequestURI(), "UTF-8");

if (request.getQueryString() != null) {
    from += "?" + request.getQueryString();
}

response.sendRedirect("login.jsp?from=" + from);

login.jsp中,将其作为隐藏输入字段传递给登录表单提交目标.

In login.jsp, pass it to the login form submit target as a hidden input field.

<input type="hidden" name="from" value="${fn:escapeXml(param.from)}" />

(注意:fn:escapeXml() 可防止您在 HTML 中重新显示用户控制的数据时遭受 XSS)

(note: fn:escapeXml() prevents you from XSS when redisplaying user-controlled data in HTML)

在登录动作中,检查是否存在,然后进行相应处理.

In the login action, check if it is there and then handle accordingly.

String from = request.getParameter("from");

if (from != null && !from.isEmpty()) {
    response.sendRedirect(from);
} else {
    response.sendRedirect("home.jsp");
}

这篇关于将他们在登录前查看的内容重定向回实际页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆