繁体   English   中英

JSF登录过滤器

[英]JSF login filter

我在网站上写了一个登录名,很不幸,我遇到了一些问题。

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-无法还原View /adminPanel.xhtml。

而是转到login.xhtml页面。 为什么? 帮助别人?

您需要处理ViewExpiredException,如果您在会话超时后尝试单击任何内容,则会发生该异常。 JSF将尝试还原视图,因此为什么会出现异常

检出: 如何在JSF 2中处理会话到期和ViewExpiredException?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM