【发布时间】:2026-01-31 02:15:02
【问题描述】:
我有以下功能
@Override
public void onAuthenticationSuccess(HttpServletRequest request
, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
String sessionId = ((WebAuthenticationDetails) authentication
.getDetails()).getSessionId();
}
我正在尝试使用以下代码对服务器进行身份验证:
HttpClient client = new HttpClient();
CommonsClientHttpRequestFactory commons
= new CommonsClientHttpRequestFactory(client);
RestTemplate template = new RestTemplate(commons);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>()
map.add("j_username", "11");
map.add("j_password", "22");
Object result2 = template.postForObject(
"http://localhost:8080/APP/j_spring_security_check", map,Object.class);
问题是,凭据是合法的,我确实输入了 onSuccess,但每次我使用上面的代码时 sessionId 都是空的。
当我通过表单进行身份验证时,它会创建一个会话 ID。 当我设置
<security:http create-session="always" />
它确实有效。但我相信这每次都会打开一个会话,并且可能真的效率低下。
知道为什么会话 id 在 java 发布请求中为空,而表单却不是吗?以及如何解决?
谢谢!
【问题讨论】:
标签: java spring rest authentication spring-security