我有一个基于表单的认证web应用程序。在登录页面,我把一个链接到一个公共登记表。注册在被用于认证的数据库添加用户。
现在,是可以做一个自动登录的登记后,新用户完成,而无需返回到登录页面?
UPDATE
更多信息,按要求:
数据源在$ CATALINA_BASE / conf目录/ server.xml中:
...
<GlobalNamingResources>
...
<Resource auth="Container" type="javax.sql.DataSource" name="jdbc/gporder"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/gporder"
maxActive="100" maxIdle="30" maxWait="10000"
username="xxx" password="yyy"/>
...
</GlobalNamingResources>
...
资源链接和领域在$ MYWAR / META-INF / context.xml的:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/gporder">
<ResourceLink global="jdbc/gporder" name="jdbc/gporder"
type="javax.sql.DataSource"/>
<Realm className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/gporder" debug="99" localDataSource="true"
digest="MD5" roleNameCol="role" userCredCol="password_hash"
userNameCol="username" userRoleTable="rolemap" userTable="users"/>
</Context>
还有什么?有一个JSP与HTML登记表,以及一个处理当表单提交POST的servlet。他们都太长,无法粘贴在这里,但这个servlet建立一个新用户,并将其保存在数据库中(通过休眠)。
在此之后,一个重定向的初始页面,这将导致Tomcat的重定向到登录页面,而不是上完成。所以我的问题是:是否有使用在注册表单中输入的用户名和密码,强行登陆,并避免进一步的重定向登录页面上的方法吗?
我想避免依赖于Tomcat的内部类。
这里是一个可能的解决方案:登记必须包含在登录过程英寸
在登记表的链接包含在登录表单,坚韧的两种形式也可以共享相同的页面。下面是login.jsp的代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Authentication</title>
</head>
<body>
<h1>Authentication</h1>
<p>Please enter your username and password below, then click on the
'Login' button</p>
<form action="j_security_check" method="POST">
<dl>
<dt><label for="j_username">Username: </label></dt>
<dd><input type="text" id="j_username" name="j_username"></dd>
<dt><label for="j_password">Password: </label></dt>
<dd><input type="password" id="j_password" name="j_password"></dd>
<dd><input type="submit" name="login" value="Login"></dd>
</dl>
</form>
<p>If you don't own an account yet, and would like to register,
<a href="register.jsp">please click here</a></p>
</body>
</html>
这里是登记表,register.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<h1>Registration</h1>
<p>Please fill in the form below, then click on the 'Register'
button</p>
<form action="register" method="post">
<dl>
<dt><label for="username">Username: </label></dt>
<dd><input type="text" id="username" name="username"></dd>
<dt><label for="password">Password: </label></dt>
<dd><input type="password" id="password" name="password"></dd>
<dt><label for="password">Verification: </label></dt>
<dd><input type="password" id="verification" name="verification"></dd>
<dt><label for="firstname">First name: </label></dt>
<dd><input type="text" id="firstname" name="firstname"></dd>
<dt><label for="lastname">Last name: </label></dt>
<dd><input type="text" id="lastname" name="lastname"></dd>
<dt><label for="email">E-mail address: </label></dt>
<dd><input type="text" id="email" name="email"></dd>
<dd><input type="submit" name="register" value="Register"></dd>
</dl>
</form>
</body>
</html>
提交后,注册字段被张贴到在数据库中创建一个新用户,然后重定向一个servlet / j_security_check:
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setFirstName(request.getParameter("firstname"));
user.setLastName(request.getParameter("lastname"));
user.setEmail(request.getParameter("email"));
// ... register the user, then if everything is OK, do:
String url = request.getContextPath() + "/j_security_check";
response.sendRedirect(url + "?j_username="
+ URLEncoder.encode(username, "UTF-8")
+ "&j_password="
+ URLEncoder.encode(password, "UTF-8"));
就像说。给你一个合理的建议,你不给大量的信息。
我会做这样的:
我有两个想法。可以创建一个新的HttpSession和尝试,并把用户到会话中。或HTTP变量传递到登录页面的用户名和密码,并使用JavaScript来自动提交表单。
这个问题似乎是类似this question。
简单的答案是调用request.login(username, password);
登录用户中,然后将其重定向到正确的页面。