2012-06-12 5 views
1

Я использую сервлет SessionFilter для проверки пользователей, а затем предоставления им доступа к системе. Мои ограниченные файлы находятся в папке с именем «com.shadibandhan.Restricted». Фильтр сеанса работает нормально.Связанный с JSF управляемый компонент http-session вызывает in-instantiation

здесь соответствующий код sessionfilter сервлета

@Override 
public void doFilter(ServletRequest req, ServletResponse res, 
     FilterChain chain) throws IOException, ServletException { 

    HttpServletRequest request = (HttpServletRequest) req; 
    HttpServletResponse response = (HttpServletResponse) res; 
    String servletPath = request.getServletPath(); 
    String contextPath = request.getContextPath(); 
    String remoteHost = request.getRemoteHost(); 
    String url = contextPath + servletPath; 
    boolean allowedRequest = false; 

    if (urlList.contains(servletPath)) { 
     allowedRequest = true; 
    } 

    if (!allowedRequest) { 
     HttpSession session = request.getSession(false); 
     if (null == session) { 

      System.out.println("Session is not present"); 
      response.sendRedirect(contextPath); 
      return; 

     } if (null != session) { 
      //String loggedIn = (String) session.getAttribute("sb_logged_in"); 
      System.out.println("Session is present"); 
      System.out.println("\nSession no. is = " + session.getId()); 

      if (session.getAttribute("logged-in") == "true") { 
       System.out.println("Session logged-in attribute is true, " + session.getAttribute("sessionUsername") + " is logged in."); 

       //ServletContext context = request.getServletContext(); 
       RequestDispatcher dispatcher = request.getRequestDispatcher(servletPath); 
       dispatcher.forward(request, response); 
      } else { 
       System.out.println("Session logged-in attribute is not true"); 
       response.sendRedirect(contextPath); 
      } 
     } 
    } 

    chain.doFilter(req, res); 
} 

Теперь, когда пользователь входит в систему, я поставил его имя пользователя и идентификатор профиля в HttpSession, Вот это боб, который связан со страницей авторизации.

@ManagedBean 
@SessionScoped 
public class UserLoginManagedBean { 

    private User user = null; 
    private String username = null; 
    private String password = null; 
    private ServiceProvider server = null; 
    HttpServletRequest request = null; 
    HttpServletResponse response = null; 
    HttpSession session = null; 
    private Date date; 
    private int profileActiveness=0; 
    private int profileActivenessPercentage=0; 

    public UserLoginManagedBean() { 
     this.user = new User(); 
     this.server = ServiceProvider.getInstance(); 
    } 

    public String validateLogin() { 

     System.out.println("Inside validate login"); 
     boolean isUserValid = false; 

     System.out.println(this.username + " " + this.password); 

     isUserValid = this.authenticate(username, password); 

     if (isUserValid) { 
      //this.user = found; 
      System.out.println("User is valid---Redirecting to messages.xhtml"); 
      return "com.shadibandhan.Restricted/profile.xhtml?faces-redirect=true"; 

     } else { 
      //addGlobalErrorMessage("Unknown login, please try again"); 
      return null; 
     } 
    } 

    public boolean authenticate(String username, String password) { 
     boolean isUserValid = false; 
     String status = null; 

     //isUserValid = this.server.authenticateUser(this.username, this.password); 

     this.user = (User) this.server.getRecordByTwoColumns(User.class, "username" , this.username, "password", this.password); 

     if(null != this.user){ 
      isUserValid = true; 
     }else{ 
      isUserValid = false; 
     } 

     if (isUserValid) { 

      FacesContext context = FacesContext.getCurrentInstance(); 
      this.request = (HttpServletRequest) context.getExternalContext().getRequest(); 
      this.response = (HttpServletResponse) context.getExternalContext().getResponse(); 
      this.session = request.getSession(true); 
//     if there's no session, it'll creat a new one due to the true flag 


      status = this.updateUserRecord(); 


      if (status.equals("success")) { 
       if (null != this.session) { 

        session.setAttribute("sessionUsername", this.user.getUsername()); 
        session.setAttribute("sessionProfileId", this.user.getProfile().getProfileId()); 
        session.setAttribute("logged-in", "true"); 

        System.out.println("Session username is --->" + session.getAttribute("sessionUsername")); 
       } 

      } else { 
       isUserValid = false; 
       FacesMessage msg = new FacesMessage("Something went wrong"); 
       FacesContext.getCurrentInstance().addMessage(null, msg); 
      } 
     } 

     return isUserValid; 
    } 

    public String logOut() { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     System.out.println("inside logout method"); 
     this.request = (HttpServletRequest) context.getExternalContext().getRequest(); 

     if (null != this.request) { 

      this.session = request.getSession(false); 
      session.invalidate(); 
      System.out.println("Session is now invalidated"); 
      return "../index.xhtml?faces-redirect=true"; 
     } else { 
      System.out.println("You're already signed out"); 
      return null; 
     } 
    } 

    private String updateUserRecord() { 
     String status = null; 

     Date lastLoginDate=this.user.getLastLogin(); 
     Date currentDate= new Date(); 
     this.profileActiveness=this.user.getProfileActiveness(); 


       SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); 

     try { 
      lastLoginDate = format.parse(lastLoginDate.toString()); 
      currentDate = format.parse(currentDate.toString()); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     }  

     // Get msec from each, and subtract. 
     long diff = currentDate.getTime() - lastLoginDate.getTime(); 
     long diffSeconds = diff/1000;   
     long diffMinutes = diff/(60 * 1000);   
     long diffHours = diff/(60 * 60 * 1000);      
     System.out.println("Time: " + diff + " ."); 
     System.out.println("Time in seconds: " + diffSeconds + " seconds.");   
     System.out.println("Time in minutes: " + diffMinutes + " minutes.");   
     System.out.println("Time in hours: " + diffHours + " hours."); 
     if(diffHours<12) 
     { 
      if(profileActiveness<8){ 
      profileActiveness++; 
      profileActivenessPercentage=(int) (profileActiveness*12.5); 
      this.user.setProfileActiveness(this.profileActiveness); 
      } 
      } 
     if(diffHours>71) 
     { 
      if(profileActiveness>2){ 
      profileActiveness-=2; 
      profileActivenessPercentage=(int) (profileActiveness*12.5); 
      this.user.setProfileActiveness(this.profileActiveness); 
      } 
      else{ 
      profileActiveness=0; 
      } 
     } 



     this.user.setLastLogin(this.getCurrentDate()); 
     this.user.setLoginStatus(true); 

     status = this.server.updateObject(this.user); 

     return status; 
    } 

    // ... 
} 

И, в другой управляемый компонент (запрос-Scoped) с именем, MessagesManagedBean, когда я пытаюсь получить идентификатор профиля после того, как пользователь вошел в систему, он работает как шарм.

Теперь, у меня есть два вопроса:

  1. Всякий раз, когда я пытаюсь получить доступ к странице с ограниченной папки, содержащей боба, связанный с ним, имеющим некоторым кодом, связанный с HTTP сессии как в этом case MessagesManagedBean, он дает мне ошибку Can not экземпляра bean-компонента, потому что я получаю атрибут в конструкторе , почему?
  2. Даже если я не вошел в систему, он вызывает конструктор bean , когда я пытаюсь получить доступ к странице, связанной с ней.

ответ

3

Вы продолжаете запрашивать chain.doFilter() после звонка response.sendRedirect(). sendRedirect() просто устанавливает заголовок ответа Location с новым URL-адресом, который будет обрабатываться браузером. Но если вы продолжите запрос chain.doFilter(), тогда весь процесс JSF будет выполнен.

Вам необходимо добавить заявление return; после вызова sendRedirect() для выхода из фильтра.

} else { 
    System.out.println("Session logged-in attribute is not true"); 
    response.sendRedirect(contextPath); 
    return; 
} 

Unrelated к конкретной проблеме, вы, крупный дизайн ошибка в сеансе области видимости боб. Вы никогда не должны назначать HTTP-запрос, ответ и сеанс как переменную экземпляра компонента. Это делает вашу сессию обработанной bean threadunsafe. Удалите все эти свойства и объявите их threadlocal внутри одного и того же блока методов.

+0

Это сработало. спасибо и спасибо за другие полезные советы :) –

+0

Добро пожаловать. – BalusC

+0

А как насчет моего вопроса 2? Всякий раз, когда я пытаюсь получить доступ к странице даже без входа в систему, он вызывает конструктор bean ... ??? –

Смежные вопросы