当其中一个是Timed-out时,从2个Spring Web应用程序注销

时间:2015-12-28 12:40:58

标签: java spring session timeout invalidation

我需要在第二个超时时从第一个Web应用程序注销,所以我的第二个应用程序有这个:

@Component
public class SessionTimeoutListener implements ApplicationListener<SessionDestroyedEvent> {

@Inject
private Environment env;

private final Logger log = LoggerFactory.getLogger(SessionTimeoutListener.class);

@Override
public void onApplicationEvent(SessionDestroyedEvent event)
{
    log.warn(event.getId());
    log.warn(event.toString());
    sendLogoutRequest();
}

public void sendLogoutRequest() {
    String portalLogutURL = env.getProperty("portalURL") + "/logoutTatami";
    log.debug(portalLogutURL);
    try{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(portalLogutURL);
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            System.out.println(response1.getStatusLine());
            HttpEntity entity1 = response1.getEntity();
            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }
    }catch (IOException e){
        e.printStackTrace();
    }

}

}

当第二个应用程序超时时,方法正常并发送请求。

第一个应用

@RequestMapping(value = "/logoutTatami",method = RequestMethod.GET)
public void logout(HttpServletRequest request) {
    HttpSession session = request.getSession();

    log.warn("logging out " + counter++);
    log.warn("logging out session with id " + session.getId());


    session.invalidate();
}

当我尝试打开&#34; url / logoutTatami&#34;在浏览器中它工作,会话无效,但是当我的第一个应用程序发送GET请求到&#34; url / logoutTatami&#34;会话没有失效,我没有错误。我的问题是我如何在一些GET请求上使会话无效(我也试过使用POST - 也没有工作)。它甚至可能吗?对我的问题可能有更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

您是否有机会在同一浏览器窗口中访问两个Web应用程序?我认为它在浏览器中有效,因为您已经在该浏览器中有一个会话,因此Spring Security知道要注销的人。尝试以隐身模式访问退出网址。

由于这是两个不同的Web应用程序,因此它们具有不同的会话处理程序。 Webapp1不知道webapp2中的会话。最简单的解决方案是创建一种方法,将登录webapp1的用户的会话ID存储到webapp2。您可以使用Map来存储这些会话。当webapp1 timedout将当前会话ID传递给webapp2时。搜索该会话,然后使其无效。