tomcat中基于表单的验证

04-01-29 wwlhp@jdon
我在tomcat中建立了一个用户,角色跟admin用户相同。
登陆页面login.jsp,代码如下:

<form method="post" action="j_security_check">
<table border="0">
  <tr>
    <td align="center" colspan="2"> Welcome! Please enter your name <br> and password to log in. </td>
  </tr>
  <tr>
    <td align="right"> Name: </td>
	<td> <input type="text" name="j_username" value="" size="15"> </td>
  </tr>
  <tr>
    <td align="right"> Password: </td>
	<td> <input type="password" name="j_password" value="" size="6"> </td>
  </tr>
  <tr>
    <td align="center" colspan="2"> <input type="submit" value="  OK  "> </td>
  </tr>
</table>
</form>


web.xml文件内容如下:

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

  <!--  安全角色  -->
  <security-role>
    <role-name> admin </role-name>
  </security-role>


运行的时候抛出错误,请问这是怎么回事,多谢帮助!

HTTP Status 404 - /tagOpt/j_security_check

--------------------------------------------------------------------------------

type Status report

message /tagOpt/j_security_check

description The requested resource (/tagOpt/j_security_check) is not available.

rypan
2004-01-30 09:19
没在server.xml中配MemoryRealm

wwlhp@jdon
2004-01-30 11:19
能告诉我具体怎么配么? 谢谢了!

agilejava
2004-01-30 13:41
server.xml中有例子的,可能是被注释掉了

wwlhp@jdon
2004-01-30 14:18
原来是
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
debug="0" resourceName="UserDatabase"/>
我把它注释掉了,改为
<Realm className="org.apache.catalina.realm.MemoryRealm"/>
可是还不行,HTTP Status 404 - /tagOpt/j_security_check。

wwlhp@jdon
2004-01-30 14:22
原来是

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 debug="0" resourceName="UserDatabase"/>
<p class="indent">

我给注释掉了,改为

<Realm className="org.apache.catalina.realm.MemoryRealm"/>
<p class="indent">

可还是不行,请各位高手帮忙!

happlyin
2004-01-30 15:42
参考tomcat自带的admin或manager应用,在目录server/webapps中,看看他们的web.xml。

在不行的话,你重新下载tomcat,看看它的server.xml配置。

kitta
2004-01-31 08:53
应该是这样的吧?注意form的action属性。

<%
String remoteUser = request.getRemoteUser();
if(remoteUser==null){ // Unauthenticated request
%>
<form method="POST" action='<%= response.encodeURL("j_security_check") %>'
      name="loginForm">
  <table>
    <tr>
      <th>
        <label for="username">username</label>
      </th>
      <td>
        <input type="text" name="j_username">
      </td>
    </tr>
    <tr>
      <th>
        <label for="password">password</label>
      </th>
      <td>
        <input type="password" name="j_password">
      </td>
    </tr>
    <tr>
      <td>
        <input type="submit" value="Login">
      </td>
      <td>
        <input type="reset" value="Reset">
       </td>
     </tr>
  </table>
</form>

<script language="JavaScript" type="text/javascript">
  <!--
    document.forms["loginForm"].elements["j_username"].focus()
  // -->
</script>
<% } else {  // authenticated request %>
<table>
<tr><td>Welcome <%=remoteUser%>!</td></tr>
<tr><td align="right"><a href="logout.jsp">Logout</a></td></tr>
</table>
<% } %>
<p class="indent">