简体   繁体   English

Tomcat过滤器来管理会话

[英]Tomcat filter to manage sessions

I have two web application. 我有两个Web应用程序。 Application A needs to call rest services of Application B. I wanted to put some security on rest services of Application B, so I came out with this workflow: 应用程序A需要调用应用程序B的其余服务。我想为应用程序B的其余服务设置一些安全性,因此我提出了以下工作流程:

When the user authenticate on Application A it performs also an authentication on Application B with a servlet on B that returns a cookie. 当用户在应用程序A上进行身份验证时,它还会通过返回Bookie的B上的Servlet在应用程序B上执行身份验证。 The servlet is something like: Servlet类似于:

    authenticate(postData): // throw Exceptions

    HttpSession session = request.getSession(true);

    Cookie cookie = new Cookie("JSESSIONID", session.getId());
    cookie.setDomain(request.getContextPath());
    cookie.setDomain(request.getServerName());
    response.addCookie(cookie);

Application A stores the cookie value in the user session, and then when needed Application A use the stored cookie to calls B. 应用程序A在用户会话中存储cookie值,然后在需要时应用程序A使用存储的cookie来调用B。

On BI have a filter in front of my rest services that is assuming to handle requests to check if these are authenticated or not. 在BI上,我的其余服务前面有一个过滤器,该过滤器假定处理请求以检查这些请求是否已通过身份验证。

The filter do something like: 过滤器执行以下操作:

    HttpServletRequest servletRequest = (HttpServletRequest) request;
    HttpSession session = servletRequest.getSession(false);

    if (null == session) {

        LOGGER.error("KO");
        HttpServletResponse servletResponse = (HttpServletResponse) request;
        servletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    } else {
        // OK
        chain.doFilter(request, response);
    }

now, I was expecting that 现在,我期待着

request.getSession(false);

will returned previously created session, cause it were tomcat who created the session in the authenticate, but it isn't working. 将返回以前创建的会话,这是因为tomcat在身份验证中创建了会话,但无法正常工作。

What I'm missing? 我缺少什么?

Sorry, the error was here clear and visible: 抱歉,错误在这里清晰可见:

cookie.setDomain(request.getContextPath());
cookie.setDomain(request.getServerName());

That now became: 现在变成:

cookie.setPath(request.getContextPath());
cookie.setDomain(request.getServerName());

and it worked fine. 而且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM