【问题标题】:j_security check always redirects me to the authentication failed error pagej_security check 总是将我重定向到身份验证失败错误页面
【发布时间】:2012-11-10 07:30:35
【问题描述】:

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

问题描述: 当我使用有效凭据提交登录表单时,j_security check 会将我重定向到 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 文件夹中也没有看到任何错误

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

【问题讨论】:

    标签: authentication tomcat j-security-check


    【解决方案1】:

    我知道这个问题已经存在一年多了,但是您是否尝试在更改 tomcat-users.xml 文件后重新启动 Tomcat?我遇到了类似的问题,重新启动 Tomcat 后,它工作正常。

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2012-01-07
        • 2016-10-01
        • 2014-01-09
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多