春季安全 - 取得AuthenticationSuccessHandler会话cookie值

问题描述 投票:0回答:1

我知道春天安全创建成功的认证饼干名SESSION。是否有可能获得AuthenticationSuccessHandler该cookie值的保持。

我有下面的实现里面,我需要的是会话cookie值。我看上去的HttpServletResponse的响应头,但他们有XSRF-TOKEN的Set-Cookie头,

@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

  @Override
  public void onAuthenticationSuccess(
      HttpServletRequest request, HttpServletResponse response, Authentication authentication)
      throws IOException {

   // GET SESSION, COOKIE VALUE HERE
  }
}

能否请您帮忙。

spring spring-boot spring-security spring-session
1个回答
0
投票

会话cookie是由春季会议的DefaultCookieSerializer,这就是所谓的每一个新的Session创建时间,而不是认证成功后必然产生。

春季会议的SessionRepositoryFilter包装以这样的方式,只要你在你的应用程序的任何点获得请求一个HttpSession,你实际上得到一个春季会议对象HttpServletRequest的。然而,这个cookie被写入到您的处理后的反应被称为,你可以在SessionRepositoryFilter看到:

try {
        filterChain.doFilter(wrappedRequest, wrappedResponse);
    }
    finally {
        wrappedRequest.commitSession(); //the SESSION cookie is created if necessary
    }

因此,如果会议刚刚为这个请求创建...

  1. 在HttpServletRequest的cookie将无法使用,因为该cookie尚未发送,但(和因此浏览器不可能送的)
  2. 该Cookie不会HttpServletResponse的一个“设置Cookie”头,因为后您的应用程序处理的请求,它会被写入。

但是,你可以得到cookie值:

String cookieValue = request.getSession().getId();

注:上面的代码将迫使春季会议创建一个会话支持稍后将用于生成会话cookie的Redis / JDBC /等。

© www.soinside.com 2019 - 2024. All rights reserved.