私は自分のサイトにログインを書いていますが、残念ながらいくつか問題があります。
ロギング用の MenagedBean:
@ManagedBean
@SessionScoped
public class LoginMB {
private static final String PERSISTENCE_UNIT_NAME = "ejbPU";
private static EntityManagerFactory factory;
private String login;
private String password;
private User user;
--konstruktor, gettery i settery
public String validate() {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("SELECT u FROM User u WHERE u.personalId = :login AND u.password = :password");
q.setParameter("login", login);
q.setParameter("password", password);
try {
user = (User) q.getSingleResult();
if (login.equals(user.getPersonalId()) && password.equals(user.getPassword())) {
return user.getRole();
}
} catch (Exception e) {
System.out.println("Błąd: " + e.getMessage());
return "failed";
}
return "failed";
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "abcd";
}
}
フィルター:
public class FilterLogin implements Filter{
FilterConfig fc;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
fc = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession session = req.getSession(true);
String pageRequested = req.getRequestURL().toString();
if (session.getAttribute("loginMB") == null && !pageRequested.contains("/login.xhtml")) {
resp.sendRedirect("/login.xhtml");
} else {
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
web.xml
<filter>
<filter-name>filter</filter-name>
<filter-class>pl.ePrzychodnia.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
1
</session-timeout>
</session-config>
ロギング ページ:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Logowanie</title>
</h:head>
<h:body>
<h1>Login</h1>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel value="Login"></h:outputLabel>
<h:inputText id="login" value="#{loginMB.login}" label="Login"/>
<h:message for="login" style="color:red" />
<h:outputLabel value="Haslo"></h:outputLabel>
<h:inputSecret id="pass" value="#{loginMB.password}" label="Password"/>
<h:message for="pass" style="color:red" />
<h:commandButton value="Zaloguj" action="#{loginMB.validate()}"></h:commandButton>
<h:commandButton value="Załóż konto" action="#{userMB.createAccount()}"></h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
別のページに移動しようとしたときにセッションが期限切れになると、エラーが発生します。
エラーが発生しました: viewId:/adminPanel.xhtml - ビュー /adminPanel.xhtml を復元できませんでした。
代わりに login.xhtml ページに移動します。なんで?誰かを助けます?