我正在尝试将单点登录添加到不同域上的两个遗留系统。目前有正常的“常规”登录。我找到了这个https://stackoverflow.com/a/9925146,但我不确定步骤1,更具体地说,是“实现序列化功能,并将身份验证对象写入具有全局作用域的会话cookie”。如果我理解正确,我应该提取sessionID并将其添加到具有全局作用域的新cookie中。
我首先尝试提取sessionID,如下所示
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进行比较。如果我理解正确的话,它们应该是匹配的。它们不匹配。我是否误解了答案中提出的解决方案?
发布于 2020-09-04 23:37:23
单点登录是一个很难解决的问题。我真的不建议您尝试实现它,除非您很好地掌握了问题以及如何解决它。如果可以,我强烈建议您尝试使用Oauth2,而不是自己实现它。
https://www.baeldung.com/sso-spring-security-oauth2可能会给你一个起点。
如果您使用的是诸如JBoss或WebSphere之类的应用服务器,则可以使用它们的单点登录选项。
https://stackoverflow.com/questions/63744020
复制相似问题