【问题标题】:Enable Session in Apache tomcat在 Apache Tomcat 中启用会话
【发布时间】:2014-08-14 05:53:55
【问题描述】:

我在设置会话和检索会话时遇到了一个奇怪的问题。

我有一个 Java Spring MVC 应用程序。但是客户端没有使用JSP。它是一个使用 AngularJS 开发的独立应用程序。在登录时,我正在登录控制器中设置会话。

@RequestMapping(value = "/loginUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ObejctResponse loginUser(@RequestBody User user, HttpServletRequest request) {

    //code checking username and password
    HttpSession session = request.getSession();
    session.setAttribute(USERID, username);
    session.setAttribute(USER, user);
    session.setAttribute(LOGGED_IN, true);

    //code
}

我有拦截器来检查会话是否为空。如果为 null,则返回 false。而在客户端,如果它没有返回任何内容,它将注销并转到登录页面。

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object object) throws Exception {
        boolean authenticate=false;
        String pathRequest = request.getPathInfo().toLowerCase();

        if(pathRequest.matches(".*(loginuser|logoutuser|switchuser|restservice|resetpassword|changepassword|checkemailid).*")) {
            authenticate = true;
        }
        /* String servletPath = request.getServletPath().toLowerCase();
        if(servletPath.matches(".*(rest).*")) {
            authenticate = true;
        } */
        if(!authenticate) {
            HttpSession session = request.getSession(true); /*don't create if it doesn't exist*/
            if(session.getAttribute(USER) != null) {
                authenticate = true;
            }
        }
        return authenticate;
    }

我尝试在我的本地 tomcat 中部署这两个战争并且它工作正常。

在数字海洋机器中,当我尝试部署两者时,它正在登录,然后当我点击任何链接时,它会请求拦截器并返回 false,导致前端注销。我跳过了请求身份验证会话,然后将其转到相应的控制器,但抛出 空指针异常 因为我从会话中获取用户 ID。

在本地它工作正常。但在远程机器上不是。除了端口,我没有更改任何配置。我必须检查哪个文件以及我必须做什么才能启用会话?

我在远程机器上使用 apache tomcat 7.0.47,在本地使用 apache tomcat 7.0.53

【问题讨论】:

    标签: java session tomcat spring-mvc


    【解决方案1】:
    HttpSession session = request.getSession(true); /*don't create if it doesn't exist*/
    

    您的评论与您的​​代码相矛盾。其中之一是错误的。如果您不希望创建会话,则应在此处指定 false,而不是 true. 这行代码将始终返回非空会话。

    【讨论】:

    • 当我复制时忘记更改它。但我正在跳过这个请求拦截器。所有网址都包含“休息”。所以我添加它以跳过检查(注释代码)。但是在控制器中它给出了空指针异常
    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2014-01-25
    • 1970-01-01
    • 2013-01-04
    • 2012-12-03
    • 2014-04-27
    相关资源
    最近更新 更多