简体   繁体   English

Java Servlet从一个Servlet重定向到另一个Servlet,然后再返回到初始Servlet

[英]Java Servlet redirecting from one Servlet to another and then back to the initial Servlet

I had a question about Java Servlets. 我有一个关于Java Servlets的问题。

lets say I am on a servlet webpage, 'somePage'. 假设我在servlet网页上,'somePage'。 I want to log in (using another servlet, 'login' servlet). 我想登录(使用另一个servlet,'登录'servlet)。 So i click on the log-in link on the 'somePage' and get redirected to the 'login' page. 所以我点击'somePage'上的登录链接,然后重定向到'登录'页面。 I type in my name and password and they are both correct. 我输入我的姓名和密码,他们都是正确的。 the login page has successfully logged me in. 登录页面已成功登录。

(now asking about coding for the 'login' servlet) How do I code the 'login' page so that it will redirect the successfully logged in person back to the, 'somePage' webpage? (现在询问有关'登录'servlet的编码)如何编写“登录”页面以便将成功登录的人重定向回“somePage”网页?

Main Question: How does the login page know the page which initially redirected to it is the 'somePage' page? 主要问题:登录页面如何知道最初重定向到它的页面是“somePage”页面?

I have checed out a lot of the request parameters, but non tell me, yes, you were directed from page, 'somePage'. 我已经删除了很多请求参数,但是没有告诉我,是的,你是从页面引导的,'somePage'。 These are the the paramater i have looked at: 这些是我看过的参数:

String authType = request.getAuthType();
String pathInfo = request.getPathInfo();
String pathTranslated = request.getPathTranslated();
String getUserName = request.getRemoteUser();
String remoteAdd = request.getRemoteAddr();
String uriString = request.getRequestURI();
String sessionID = request.getRequestedSessionId();
String serverName = request.getServerName();
Integer serverPort = request.getServerPort();
String servletPath = request.getServletPath();

I know some of these are obvously not going to give me the answer I am looking for, but I figure one of the HttpServletRequest parameters has got to tell the login page who asked for it to be displayed. 我知道其中一些是不会给我正在寻找的答案,但我认为其中一个HttpServletRequest参数必须告诉登录页面谁要求它显示。 Any help would be greatly appreciated. 任何帮助将不胜感激。 I'm going to continue my search for the answer. 我将继续寻找答案。 I've tried to search for this question, but haven't found an answer. 我试图搜索这个问题,但还没有找到答案。

Instead implementing yourself you should consider using form based authentification for your web app. 相反,您应该考虑为您的网络应用程序使用基于表单的身份验证

Almost every servlet container supports this. 几乎每个servlet容器都支持这个。

At first you have to configure security. 首先,您必须配置安全性。 This depends on your application server. 这取决于您的应用程序服务器。 Ie with Jetty you can use a database approach with tables for users and their roles or LDAP, etc. 即使用Jetty,您可以使用数据库方法,为用户及其角色或LDAP等提供表格。

In web.xml you turn on form based authentification: web.xml您打开基于表单的身份验证:

<login-config>
  <auth-method>FORM</auth-method>
  <form-login-config>
    <form-login-page>/logon.jsp</form-login-page>
    <form-error-page>/logonError.jsp</form-error-page>
  </form-login-config>
 </login-config>

You specify two JSP pages you have to provide. 您指定了必须提供的两个JSP页面。 logon.jsp is the login page for inserting user name and password. logon.jsp是用于插入用户名和密码的登录页面。 logonError.jsp is shown, if user name and password are invalid. 如果用户名和密码无效,则显示logonError.jsp

The whole login workflow is handled by the application server. 整个登录工作流程由应用程序服务器处理。

If the user first goes to a protected URL, the application server presents the login page instead. 如果用户首先访问受保护的URL,则应用程序服务器会显示登录页面。 As a convention the input fields for user name and passwort should be named j_username and j_password . 作为惯例,用户名和密码的输入字段应命名为j_usernamej_password When the user submits the login form the server checks, if the user crendentials are valid (according to its configuration). 当用户提交登录表单时,服务器检查,如果用户凭据有效(根据其配置)。 If so the user is redirected to the original page. 如果是,则将用户重定向到原始页面。 Otherwise the login error page is shown. 否则,将显示登录错误页面。

If you really want to implement it yourself then you can implement a servlet filter so that all calls to protected resources have to pass your filter. 如果您真的想自己实现它,那么您可以实现一个servlet过滤器,以便所有对受保护资源的调用都必须通过您的过滤器。 In your filter you can check, if there is already a session present and if the user has successfully logged in. Then the normal call can proceed. 在您的过滤器中,您可以检查是否已存在会话以及用户是否已成功登录。然后可以继续正常呼叫。 Otherwise you can forward to your login page and store the original URL in the session. 否则,您可以转到登录页面并将原始URL存储在会话中。 After a successfull login you can read the original URL out of your session context and redirect to the page the user wanted to see in the first place. 成功登录后,您可以从会话上下文中读取原始URL,并重定向到用户首先想要查看的页面。

There are different ways of doing this. 有不同的方法可以做到这一点。 One way is to have your login page support a continue CGI parameter that gives the URL to which to redirect after the login is successful. 一种方法是让您的登录页面支持一个continue CGI参数,该参数提供登录成功后重定向的URL。 Another way to do this is to use the "Referer" header that was passed to the login page, and redirect to that URL. 另一种方法是使用传递给登录页面的“Referer”标头,并重定向到该URL。

For the former, you can use ServletRequest.getParameterMap() to get the CGI arguments and determine if there is a CGI parameter named continue (or whatever name you choose to give to that CGI parameter); 对于前者,您可以使用ServletRequest.getParameterMap()来获取CGI参数,并确定是否存在名为continue的CGI参数(或您选择为该CGI参数指定的任何名称); for the latter, you can use HttpServletRequest.getHeader() to get the "Referer" header. 对于后者,您可以使用HttpServletRequest.getHeader()来获取“Referer”标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM