jsf - 在 Jsf 2 中使用 servlet 3.0 登录的用户身份验证不起作用

标签 jsf tomcat servlets authentication jsf-2

尝试创建一个带有登录页面并使用 Servlet 3.0 登录方法的应用程序。我已经在 tomcat 中定义了领域并相应地配置了 web.xml。但是当我尝试访问任何页面甚至登录页面时,它不会被呈现并且我可以在代码中看到 JSF 标签。我不知道我是否遗漏了什么。详情如下 以下是我放在 web.xml 中的内容。

<security-constraint>
<display-name>Restricted</display-name>
  <web-resource-collection>
    <web-resource-name>Restricted Area</web-resource-name>
    <url-pattern>/faces/*</url-pattern>
    <http-method>GET</http-method>
    <http-method>POST</http-method>
  </web-resource-collection>
  <auth-constraint>
<role-name>user</role-name>
  </auth-constraint>
 </security-constraint>
 <login-config>
   <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/index.xhtml</form-login-page>
        <form-error-page>/index.xhtml</form-error-page>
    </form-login-config>
</login-config>
<security-role>
  <role-name>user</role-name>
</security-role>

以下是tomcat中定义的realm

<Realm className="org.apache.catalina.realm.JDBCRealm" driverName="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/db_name?user=user&amp;password=password"
            userTable="users" userNameCol="username" userCredCol="userpass"
            userRoleTable="user_roles" roleNameCol="rolename" digest="MD5"/>

登录密码是

public String login(){
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    try {
        //Login via the Servlet Context
        request.login(username, password);
        return "success";
    } catch (ServletException e) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Login", null));
        e.printStackTrace();
    }
    return "failure";
}

让我知道我做错了什么


更新 谢谢你们,它开始工作了。我不得不更改 web.xml 中的一些 url 映射并重新排列文件夹以进行安全设置。目前它正在工作,但现在遇到了一个奇怪的问题。我已经定义了

<form-login-config>
 <form-login-page>/index.xhtml</form-login-page>
 <form-error-page>/index.xhtml</form-error-page>
</form-login-config>

现在 web.xml 在

上显示错误
<form-login-page>

并提示

Description Resource Path Location Type error: Attribute form-error-page references to /index.jsp that does not exist in web content web.xml /XXXX/WebContent/WEB-INF

我不知道这是从哪里来的,因为我没有使用任何 jsp 文件。任何线索

最佳答案

因此,FacesServlet没有被调用。这意味着登录页面的 URL 与 FacesServlet 的 URL 模式不匹配。正如您在 web.xml 中定义的那样.根据您在/faces/*上设置的限制区域的URL模式,我假设它与您为 FacesServlet 定义的 URL 模式相同.

因此,相应地修复它:

<form-login-page>/faces/index.xhtml</form-login-page>
<form-error-page>/faces/index.xhtml</form-error-page>

但是,我建议使用 FacesServlet 的映射在 *.xhtml相反,这样您就不必担心将 /faces在路径中,也不会在最终用户删除 /faces 时最终用户能够看到原始 JSF 源代码。来自浏览器地址栏中的 URL。


更新:这是特定于 IDE 的验证错误。您正在使用的 IDE 假装比实际更智能。如果您的 webapp 运行正常,请忽略它。如有必要,您可以在其首选项中关闭 IDE XML 验证。

关于jsf - 在 Jsf 2 中使用 servlet 3.0 登录的用户身份验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7667553/

相关文章:

java - jar 的最佳去处

java - 创建 session 时如何获取IP地址?

java - 如何从一个 Servlet 传递另一个 ArrayList?

java - Richfaces 文件上传 UI 外观

java - Eclipse 自定义 java 编译器

jsf - 如何在 JSF 页面中插入不间断空格字符?

java - 将值(例如 id)从一个 servlet 发送到另一个(jsp+servlet+mysql)

<f :viewParam> do not localize to <f:view locale>, 但到默认语言环境的验证/转换错误

java - 将格式化输入流流式传输到服务器的问题

java - 嵌入式 Tomcat 启用 SSL