Filtr logowania.

0

Witam. Piszę logowanie na mojej stronie i niestety mam pewne problemy.

Mój bean JSF z logowaniem:

@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";
    }
} 

Filter:

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.
    }

}

w web.xml dodałem:

    <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>

Strona z logowaniem:

<?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> 

Jak widać ustawiłem sobie sesję na minutę żeby sprawdzić czy dobrze wszystko reaguje na jej wygaśnięcie. Jednak po wygaśnięciu sesji jak chcę przejść na inną stronę to nie przekierowuje mnie na stronę z logowaniem tylko dostaję błąd:

An Error Occured:
viewId:/adminPanel.xhtml - View /adminPanel.xhtml could not be restored.

Jeśli ktoś może to proszę o pomoc. Mam jeszcze wątpliwości co do tego pobierania parametru w metodzie doFilter:

if (session.getAttribute("loginMB") == null && !pageRequested.contains("/login.xhtml")) 

czy dobrze to zrobiłem. Tego parametru jako tako nie muszę chyba nigdzie deklarować? Tak nazywa się mój bean więc chyba nie jest to potrzebne? Przynajmniej w przykładzie, na którym się wzorowałem nie zauważyłem czegoś takiego aby było on jakoś deklarowany na stronach.

0

Znacznik

<url-pattern>

w pliku web.xml nie jest poprawnie zakończony.

0
antoniaklja napisał(a):

Znacznik

<url-pattern>

w pliku web.xml nie jest poprawnie zakończony.

<url-pattern>/*</url-pattern> 

To niby jak mam go zakończyć?

0

Problem rozwiązany.

1 użytkowników online, w tym zalogowanych: 0, gości: 1