0

我正在使用我编写的过滤器进行会话管理,并使用 managedbean 注释我试图将验证成功发送到 filter 。

@ManagedBean(name = "customer")
@SessionScoped
public class CustomerBean implements Serializable{


    public String checkValidCustomer(){
         FacesContext context = FacesContext.getCurrentInstance();
         HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
         HttpSession session = request.getSession(true);
        Customer cust = new Customer();
        cust.setUsername(getUsername());
        cust.setPassword(getPassword());
        String returnValue = customerBo.checkValidCustomer(cust); 
        if(returnValue == "success"){
        session.setAttribute("customer", returnValue);
        }
        else
        {
            session.setAttribute("customer", null);
        }
        return returnValue;

我在 retunValue 字符串中获得了成功,所以我尝试将值作为值传递给 managedBean 注释。myfilter 代码就像。

@WebFilter("/faces/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {    
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String cust = (String) req.getSession().getAttribute("customer");
        if (cust != null && cust.equals("success")) {
            chain.doFilter(request, response);
        } else {
            resp.sendRedirect(req.getContextPath() + "/faces/default.xhtml");
        }
    }

我得到 String cust 的空值,当我打开登录页面时,它显示这个网页有一个重定向循环。你能告诉我哪里做错了吗?

4

1 回答 1

0

尝试这个...

HttpServletRequest request = (HttpServletRequest) req;
          HttpServletResponse response = (HttpServletResponse) resp;
          System.out.println("Inside the filter.............."  );
          HttpSession session = request.getSession();
          User u = null;
          if(session.getAttribute("customer")!=null){
              u = (User) session.getAttribute("customer");
          }       
          if (u!= null)
          {
              System.out.println("user does exits.." + u.getUname() );// u can see who was logged here
              chain.doFilter(req, resp);          
          }else{
              System.out.println("user does exits..");        
          }
于 2013-07-11T04:43:28.067 回答