【问题标题】:How to use session's property in java application如何在java应用程序中使用会话的属性
【发布时间】:2013-05-06 00:42:34
【问题描述】:

我已为我的应用程序启用安全性,除非用户登录,否则无法访问这些受保护的资源。

但是,我需要在我的 java 应用程序中运行多个测试。我想知道是否可以从登录页面(浏览器)登录到我的应用程序,使用它的 session-id,并为我的测试获取经过身份验证的对象(java 应用程序 )?

如果有帮助,我正在使用 Spring Security。

我正在寻找一般的想法,如果需要时间来解释如何做到这一点。

【问题讨论】:

    标签: java session jakarta-ee spring-security session-variables


    【解决方案1】:

    Spring Security 的Authentication 拥有当前用户的主体和授权角色的句柄。可以通过以下方式访问:

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    

    Spring 的RequestAttributes 具有当前会话ID 的句柄。可以通过以下方式访问:

    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
    String sessionId = attributes.getSessionId();
    

    如果RequestContextFilter 已在 web.xml 中配置,则此方法有效。

    【讨论】:

    • 感谢您的回复。但是,您应该知道 TEST-APPLICATION 是独立运行的。所以它可能既不知道也无权访问SecurityContextHolder。有没有办法使用 session-id ,检索它并使用它的内容?
    猜你喜欢
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2016-02-05
    • 1970-01-01
    相关资源
    最近更新 更多