当其中一个超时时从 2 Spring 个 Web 应用程序注销

Logout from 2 Spring Web Applications when one of them is Timed-out

我需要在第一个 Web 应用程序超时时从第一个 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();
}

当我尝试在浏览器中打开 "url/logoutTatami" 时,它可以正常工作并且会话无效,但是当我的第一个应用程序向 "url/logoutTatami" 发送 GET 请求时,会话没有无效并且我没有收到任何错误。我的问题是如何使某些 GET 请求的会话无效(我也尝试使用 POST - 也不起作用)。有可能吗?哦,也许我的问题有更好的解决方案?

您是否有可能在同一个浏览器中访问您的两个网络应用程序 window?我认为它在浏览器中有效,因为您已经在该浏览器中有一个会话,所以 Spring 安全知道要注销谁。尝试以隐身模式访问注销 url。

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