我正在尝试实施User-Auth。应用程序...我需要验证,如果用户已经在我的数据库中,那么他可以登录并运行某些操作。
我使用Wildfly作为App。服务器和JSF。我已经安装了H2 DB本地。我的认证代码:
@SuppressWarnings("javadoc")
// @Stateless
@SessionScoped
@ManagedBean
@WebServlet(description = "Authentification", urlPatterns = { "/Authentification" })
public class Authentification extends HttpServlet implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String originalURL;
@Inject
private EntityService<Employee> userService;
@Override
public void init() throws ServletException {
}
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
if (this.username != null && this.password != null) {
final List<Employee> employees = userService.loadAll(Employee.FINDALL, Employee.class);
for (final Employee employee : employees) {
if (employee.getLastName() == this.username) {
System.out.println(this.username + " found!");
response.sendRedirect("index.xhtml");
} else {
System.out.println(this.username + " not found!");
}
}
}
}
public void lotout() {
final FacesContext context = FacesContext.getCurrentInstance();
final HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
// TODO: Abmelden
request.logout();
} catch (final ServletException e) {
// TODO: Exceptionsbehandlung
context.addMessage(null, new FacesMessage("Logout failed."));
}
}
public String getUsername() {
return username;
}
public void setUsername(final String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
public String getOriginalURL() {
return originalURL;
}
public void setOriginalURL(final String originalURL) {
this.originalURL = originalURL;
}
}
现在是我的xhtml页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:form>
<h:outputLabel for="username" value="Username" />
<h:inputText id="username" value="#{Authentification.username}"
maxsize="20" required="true" />
<br />
<h:outputLabel for="password" value="Password" />
<h:inputSecret id="password" value="#{Authentification.password}"
maxsize="10" required="true" />
<br />
<h:commandButton value="Login"
action="#{Authentification.doPost}" />
</h:form>
</html>
一旦我启动了应用程序。我从Wildfly那里得到了这个错误:
16:41:31,760 ERROR [io.undertow.request] (default task-30) UT005023: Exception handling request to /sep-gruppe-5/login.xhtml;jsessionid=sDqBv4bcos5dJznjwBhmQv2nXxPlWt9HVlsYwXXl.w1289: javax.servlet.ServletException: /login.xhtml @10,37 value="#{Authentification.username}": Target Unreachable, identifier 'Authentification' resolved to null
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
我认为最重要的部分是:
Caused by: javax.el.PropertyNotFoundException: /login.xhtml @10,37 value="#{Authentification.username}": Target Unreachable, identifier 'Authentification' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
我不知道我现在失踪了什么......应用程序。无法获得Class Authentification ...为什么这样?请帮忙。