j_security check始终将我重定向到身份验证失败错误页面

时间:2012-11-21 23:12:10

标签: authentication tomcat j-security-check

这是我第一次尝试在java Web应用程序中使用j_security检查表单身份验证。我正在使用Eclipse 3.6和Apache Tomcat 6.0.28。

问题描述: 当我使用有效凭据提交登录表单时,j_security检查会将我重定向到error.html中定义的错误页面。当我使用无效凭据提交时,它也会将我带到error.html页面。这很好,但是对于有效的用户,我希望在登录后被带到受保护的资源。

的login.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form method=post action="j_security_check">
 Username   <input type="text" name="j_username"><br />
 Password   <input type="password" name= "j_password"><br />
    <input type="submit" value="submit">
</form>
</body>
</html>

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">

  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <welcome-file-list>
    <welcome-file>/ui/index.xhtml</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <login-config>
    <auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/security/login.html</form-login-page>
<form-error-page>/security/error.html</form-error-page>
</form-login-config>
</login-config>

<security-constraint> 
    <display-name>URLsConstraintMechanism</display-name>
    <web-resource-collection>
        <web-resource-name>clientURL</web-resource-name>
        <url-pattern>/ui/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>PUT</http-method>
        <http-method>POST</http-method>
        <http-method>DELETE</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>tomcat</role-name>
    </auth-constraint>
</security-constraint>


<security-role>
  <description>The Only Secure Role</description>
  <role-name>tomcat</role-name>
</security-role>

</web-app>

Tomcat的users.xml中

     <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>

我在IDE的控制台或$ CATALINA_HOME / logs文件夹中也没有看到任何错误

请原谅任何遗漏,因为我是这个论坛的新手。我已经搜索了现有的帖子,但到目前为止,没有任何建议对我有用。

2 个答案:

答案 0 :(得分:0)

要检查的是 tomcat-users.xml 文件,如果从eclipse运行tomcat,eclipse会创建一对单独的 server.xml tomcat-users.xml ,它与tomcat安装目录中的默认值不同。通过双击服务器选项卡下的tomcat服务器上的服务器配置路径,尝试找到正确的配置参数。

同时检查 server.xml 中的Realm,如果您使用tomcat-users.xml文件存储用户和密码使用Memory Realm

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

答案 1 :(得分:0)

我知道这个问题已经超过一年了,但您是否尝试在更改tomcat-users.xml文件后重新启动Tomcat?我遇到了类似的问题,在我重新启动Tomcat之后,它运行良好。

Tomcat不会自动重新读取文件tomcat-users.xml,需要重新启动Tomcat才能重新读取它。

相关问题