我为我的应用程序启用了安全性,除非用户已登录,否则无法访问这些受保护的资源。
但是,我需要在我的java应用程序中运行多个测试。我想知道是否可以从登录页面(浏览器)登录我的应用程序,使用其session-id,并为我的测试获取经过身份验证的对象( java应用程序 )?
如果有帮助,我正在使用Spring security。
我正在寻找一般的想法,如果需要时间来解释如何去做。
答案 0 :(得分:0)
Spring Security的Authentication可以处理当前用户的主要角色和授权角色。可以使用以下方式访问它:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Spring的RequestAttributes有一个当前会话ID的句柄。可以使用以下方式访问它:
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
String sessionId = attributes.getSessionId();
如果已在web.xml中配置RequestContextFilter,则此工作正常。