0

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

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

如果有帮助,我正在使用弹簧安全性。

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

4

1 回答 1

0

Spring Security 的Authentication处理当前用户的主体和授权角色。可以使用以下方式访问它:

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

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

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

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

于 2013-05-11T13:20:27.840 回答