How to properly logout of a Java EE 6 Web Application after logging in

前端 未结 3 631
野的像风
野的像风 2020-11-30 01:50

A pretty simple requirement. After logging into web J2EE 6 application, how can I have the user logout again?

Most (all?) the books and tutorials I have seen show h

3条回答
  •  情深已故
    2020-11-30 02:50

    Two step process -

    1.create the logout page
    2.create a session bean with a logout method

    STEP A: The Logout Page

    Hello #{userSession.username},

    STEP B: Session Bean Backing Code (snippet)

    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.invalidate();
        return "/index?faces-redirect=true";
    }
    
    public boolean isUserLoggedIn() {
        String user = this.getUsername();
        boolean result = !((user == null)|| user.isEmpty());
        return result;
    }
    
    /** Get the login username if it exists */
    public String getUsername() {
        String user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
        return user;
    }    
    

提交回复
热议问题