在jsf中使会话无效之后,它仍在给出会话值

时间:2014-12-04 14:28:59

标签: jsf session

注销时我确实删除了会话对象和无效会话,如下所示

public String logout() throws IOException {
        logger.info("logout() : satarted----- ");
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.getSessionMap().remove("visitorComponent");
        System.out.println("*************_->"+ec.getSessionMap().remove("visitorComponent"));
        ec.invalidateSession();
        ec.redirect(ec.getRequestContextPath() + "/logout.xhtml");
        return null;
//      return "logout?faces-redirect=true";
    }

但仍然在过滤器类中给出值,过滤类代码如下

public class AuthorizationFilter implements Filter {

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

        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();

        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setHeader("Cache-Control", "no-cache,no-store,must-revalidate"); // HTTP 1.1
        httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
        httpResponse.setDateHeader("Expires", 0);

        if (session.getAttribute("visitorComponent") != null) {
            System.out.println("-sdf>"+((VisitorComponent) session.getAttribute("visitorComponent")).getAdmin());
        }
        System.out.println("->"+session.getAttribute("visitorComponent"));
        System.out.println("=url>"+req.getRequestURI());
        System.out.println("=>"+req.getRequestURI().endsWith("login.xhtml"));
        if (session.getAttribute("visitorComponent") != null || req.getRequestURI().endsWith("index.xhtml")) {
            chain.doFilter(request, httpResponse);
        } else {
            System.out.println("---in else--");
//            HttpServletResponse res = (HttpServletResponse) response;
            httpResponse.sendRedirect("index.xhtml");
            return;
        }

    }

你能帮助任何一个人,我需要做什么吗?

2 个答案:

答案 0 :(得分:0)

最后我找到了解决方案,但副作用就在那里。

我的解决方案是,以前我没有手动保存登录对象,因为我在会话范围内的登录类。如果我以这种方式做它不明确的对象也因为在启动应用程序登录类时实例化了。

@ManagedBean(name = "visitorComponent")
@SessionScoped
public class VisitorComponent {

  Admin admin = new Admin();

 public String login() {
     // some code to verify login details
     this.admin = adminService.getAdminObject(adminId);
 }

现在我通过获取会话对象映射手动完成并将我的登录对象放在会话映射中。

@ManagedBean(name = "visitorComponent")
@SessionScoped
public class VisitorComponent {

  Admin admin = new Admin();

 public String login() {
     // some code to verify login details
     this.admin = adminService.getAdminObject(adminId);
     // Session object creating to get the session values
                    ExternalContext externalContext = FacesContext.getCurrentInstance()
                            .getExternalContext();
                    Map<String, Object> sessionMap = externalContext.getSessionMap();
                    sessionMap.put("abcadminBean", this.admin);
 }

现在它的工作正常无效,但问题出现在我的登录页面中 我有

我对我的过滤类有疑问,就像下面的

public class AuthorizationFilter implements Filter {

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

        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();

        if (session.getAttribute("abcadminBean") != null || req.getRequestURI().endsWith("index.xhtml")) {
            chain.doFilter(request, response);
        } else {
            FacesContext fc = FacesContext.getCurrentInstance();
            if (fc == null) {
                // Create new Lifecycle.
                LifecycleFactory lifecycleFactory = (LifecycleFactory)
                    FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
                Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

                // Create new FacesContext.
                FacesContextFactory contextFactory  = (FacesContextFactory)
                    FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
                fc = contextFactory.getFacesContext(
                    ((HttpServletRequest) request).getSession().getServletContext(), request, response, lifecycle);
            }
            System.out.println("fc->"+fc);
            ExternalContext ec = fc.getExternalContext();
            ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
//            HttpServletResponse res = (HttpServletResponse) response;
//          ((HttpServletResponse) response).sendRedirect("index.xhtml");
            return;
        }

    }

我在这里重定向我的页面,我认为这是问题所在 你能不能请任何人帮助我,我的UI更改需要做什么?

答案 1 :(得分:0)

最后我抓住了解决方案。 为什么因为它没有加载所有的primefaces UI效果, 对于我们的过滤器正在检查的每个请求,所以在Filter类中我们必须编写条件以允许我们的CSS和JS文件如下所示

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

        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();

        if (session.getAttribute("loginDetails") != null
                || session.getAttribute("empLoginDetails") != null
                || req.getRequestURI().endsWith("index.xhtml")
                || req.getRequestURI().endsWith("forgetpass.xhtml")
                || req.getRequestURI().endsWith("empLogin.xhtml")
                || req.getRequestURI().endsWith("logout.xhtml")
                || req.getRequestURI().endsWith("ajax-loader.gif.xhtml")
                || req.getRequestURI().contains(".js")
                || req.getRequestURI().contains(".css")) {
            chain.doFilter(request, response);
        } else {
            FacesContext fc = FacesContext.getCurrentInstance();
            if (fc == null) {
                // Create new Lifecycle.
                LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
                        .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
                Lifecycle lifecycle = lifecycleFactory
                        .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

                // Create new FacesContext.
                FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
                        .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
                fc = contextFactory.getFacesContext(
                        ((HttpServletRequest) request).getSession()
                                .getServletContext(), request, response,
                        lifecycle);
            }

            RequestDispatcher dispatcher = request
                    .getRequestDispatcher("/index.xhtml");
            dispatcher.forward(request, response);
            return;
        }

现在我的应用程序可以正常使用过滤器类