0

My xhtml web pages are called from an external system using a simple request, I suppose to check if the user name - given from the request - is valid or not, if valid he suppose to get to the home page, other than that he is redirected to the external system. I created a filter to get the request:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;


    // skip the resources library
    if (!req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
        res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP
                                                                                // 1.1.
        res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        res.setDateHeader("Expires", 0); // Proxies.
    }

    Object attr = req.getParameter("user");
    String language = (String) req.getParameter("language");

    if (attr != null && language != null) {
        // check user in db
        String userAdmin = (String) attr;

        ResultStatus result = myDao.findUser(userAdmin);
        if (result.getStatus().equals(ResponseStatus.SUCCESS)) {
            // User is logged in, so just continue request.
            User user = new User();
            user.setAccountId(result.getAccount().getAccountId());



            chain.doFilter(req, res);
        } else {
            // User is not logged in, so redirect to index.
            res.sendRedirect(req.getContextPath() + "/test.xhtml");
        }
    } else {
        // User is not logged in, so redirect to index.
        res.sendRedirect(req.getContextPath() + "/test.xhtml");
    }
}

I need to create session for the user in the filter if he is a valid user, so I can get the user object at any back bean.

4
  • You have access to the request in req variable, just call req.getSession and you'll get the HttpSession to store/restore/invalidate the user. Jun 30, 2014 at 15:02
  • I did this req.getSession().setAttribute(USER_TOKEN, newUser); to set the user and this FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); User flag = (User) session.getAttribute(USER_TOKEN); to get it at a baking bean, is this right??
    – Yara Mousa
    Jun 30, 2014 at 15:14
  • You don't need to use FacesContext in a Servlet Filter. Jun 30, 2014 at 15:14
  • I'll put it simple: JSF is irrelevant for your problem. Use plain HttpServletRequest and HttpSession to handle this problem. Jun 30, 2014 at 15:16

1 Answer 1

0

To set it just do this :

req.getSession().getServletContext().setAttribute(LoginFilter.USER_KEY, user);
                    chain.doFilter(req, res);

To get it :

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
User user = (User) request.getSession().getServletContext().getAttribute(LoginFilter.USER_KEY);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.