文文在搬砖 2017-04-09 00:54 采纳率: 0%
浏览 860

springmvc拦截器跳转问题

public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
HttpSession session = httpServletRequest.getSession();
Object UserId = session.getAttribute("userId");

        String InterUri =  httpServletRequest.getServletPath();
    String requesturl = httpServletRequest.getRequestURI();
    String contextpath = httpServletRequest.getContextPath();
    String url = requesturl.substring(contextpath.length());




    if ( null ==UserId){
        if (httpServletRequest.getCookies()!=null) {
            Cookie[] cookies = httpServletRequest.getCookies();
            if (cookies != null && cookies.length > 0) {



                String usercook = null;

                for (Cookie cookie : cookies) {
                    if ("UserCookie".equals(cookie.getName())) {
                         usercook = cookie.getValue();
                        String[] Susercook = usercook.split(",");
                        if (Susercook.length == 3) {
                            int userId = Integer.parseInt(Susercook[0]);
                            User user = userService.findbuId(userId);
                            String username = Susercook[1];
                            String password;
                            if (username.indexOf("@") > 0) {
                                password = Susercook[2];
                                if (username.equals(user.getEmail()) && password.equals(user.getPassword())) {
                                    session.setAttribute("UserId", userId);
                                   break;
                                }
                            } else {//手机登录
                                password = Susercook[2];
                                if (username.equals(user.getPhone()) && password.equals(user.getPassword())) {
                                    session.setAttribute("userId", userId);
                                     break;

                                }
                            }

                        }

                    }
                }
             if (usercook!=null){

                 return true;
             }

            }
        }

    }
    else{//id

// httpServletResponse.sendRedirect(httpServletRequest.getContextPath() +"/Main.jsp");

        if(null != allowUrls && allowUrls.length>=1){
            for(String path : allowUrls) {
                if(url.contains(path)) {

                    return true;
                }
            }
        }

    }

    return true;

}

有没有大神帮下忙,为什么 responsesendredirect(页面)根本没反应 还不能return false,不然静态资源又会被拦截,跳页面时候 httpServletRequest.getServletPath();全是静态资源路径没有跳转的页面路径,我的跳转不经过controller跳转,直接页面跳页面,session也得刷新一下页面才能拿得到,感觉好影响项目效率。在线等,好急啊,做了好几天没解决。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog