spring security 3.1单点登录ldap

zlwx9yxi  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(264)

我正在尝试向不同域上的两个旧系统添加单点登录。目前有工作“定期”登录。我找到这个了https://stackoverflow.com/a/9925146 但我不确定第1步更具体地说是“实现将身份验证对象序列化并写入具有全局作用域的会话cookie的功能”。如果我理解正确,我应该提取sessionid并将其添加到具有全局作用域的新cookie。
我一开始就想这样提取会话ID

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws IOException,ServletException {
        Cookie cookie = null;
        UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;

        if (authentication.getDetails() != null) {
            WebAuthenticationDetails dets = (WebAuthenticationDetails) auth.getDetails();
            System.out.println("sessionID: " + dets.getSessionId());

        }

        response.addCookie(cookie);

        super.onAuthenticationSuccess(request,response,authentication);
    }
}

为了验证我是否在正确的轨道上,我将sessionid打印到终端,并将其与Spring Security 在浏览器中设置的sessionid进行比较。如果我理解正确,他们应该匹配。他们不匹配。我是否误解了答案中建议的解决方案?

w46czmvw

w46czmvw1#

单点登录是一个很难解决的问题。我真的不建议尝试实现它,除非你有一个很好的掌握问题和如何解决它。如果可以的话,我强烈建议您尝试使用oauth2,而不是自己实现它。
https://www.baeldung.com/sso-spring-security-oauth2 可能会给你一个起点。
如果您使用的是jboss或websphere之类的应用服务器,那么您可以使用它们的sso选项。

相关问题