我有以下功能
@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 都是 null。
当我通过表单进行身份验证时,它会创建一个 session ID。 当我设置
<security:http create-session="always" />
它确实有效。但我相信这每次都会打开一个 session ,并且可能效率很低。
知道为什么 java post 请求中的 session id 为空,而表单却不是吗?我该如何修复它?
谢谢!
最佳答案
如果没有看到应用程序的其余部分和 Spring Security 配置,很难说。您可以使用 <debug>
轻松启用 Spring Security 调试元素,它将提供在登录表单情况下创建 session 的堆栈跟踪。我猜测这可能是因为您的应用程序正在使用 JSP,并且当您查看登录表单时 JSP 正在创建 session 。在使用 RestTemplate 发布的实例中,只有在您提交用户名和密码之后,它才会创建 session (onSuccess 的 session 是您提交凭据之前的 session )。
关于java - spring security - 身份验证中的sessionId为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173869/