流程

上方实现了http://localhost:8083/ssoLogin?source=http://127.0.0.1:8085
输入链接 输入用户名密码

跳转到
http://127.0.0.1:8085/main?ticket=877b97ba-ad6a-410d-bf6c-0ef536a25c84&domains=http://localhost:8081

输入webapp1

此时执行

ticket = servletRequest.getParameter("ticket");
        if(!Objects.equals(null, ticket) && !Objects.equals("", ticket.trim())){
            // 超时判断
            ticket = ticket + ":" + (System.currentTimeMillis() + 1000);
            // 添加cookie
            ((HttpServletResponse)servletResponse).addCookie(new Cookie("ming", ticket));
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            // 跳转回登录界面进行正常的登录
            ((HttpServletResponse)servletResponse).sendRedirect(server + "/ssoLogin?source=" + app);
        }

由于为空值,使用 进行重定向

重定向发送到server

执行

else if(Objects.equals("/ssoLogin", req.getServletPath())){
            req.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(req, resp);

此时共享同一个req
输入完成用户名和密码以后,跳转到

// 请求为登录
        if(Objects.equals("/login", req.getServletPath())){
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String source = req.getParameter("source");

            // 得到浏览器路由栈的回退地址
          if(null == source || Objects.equals("", source)){
                source = req.getHeader("referer");
                source = source.substring(source.indexOf("source=") + 7);
            }

            if(Objects.equals(username, password)){
                // 生成uid
                String ticket = UUID.randomUUID().toString();
                System.out.println(ticket);
                // 登录成功跳转登录回页面
                resp.sendRedirect(source + "/main?ticket=" + ticket + "&domains=" + domains.replace(source + ",", "").replace("," + source, "").replace(source, ""));
            }else {
                // 登录失败转发到login.jsp
                req.setAttribute("source", source);
                req.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(req, resp);
            }

此时已经共享同一个req...

由于正确,,进行客户端302跳转 到source

过滤器过滤执行

 ticket = servletRequest.getParameter("ticket");
        if(!Objects.equals(null, ticket) && !Objects.equals("", ticket.trim())){
            // 超时判断
            ticket = ticket + ":" + (System.currentTimeMillis() + 1000);
            // 添加cookie
            ((HttpServletResponse)servletResponse).addCookie(new Cookie("ming", ticket));
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            // 跳转回登录界面进行正常的登录
            ((HttpServletResponse)servletResponse).sendRedirect(server + "/ssoLogin?source=" + app);
        }

在resp中add cookie

此时执行

if(Objects.equals("/main", req.getServletPath())){
            String domain = req.getParameter("domains");
            String ticket = req.getParameter("ticket");
            for(String server: domain.split(",")){
                // trim防止空串
                if(!Objects.equals(null, server) && !Objects.equals("", server.trim())){
                }
            }
            // 成功之后跳转
            req.getRequestDispatcher("/WEB-INF/views/main.jsp").forward(req, resp);

依旧共享同一个req,,设置cookie

此时cookie已经设置成功

cookie的作用域为server

删除server下的cookie即全部失效