SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例
1、使用Cookie解决单点登录
技术点:
1、设置Cookie的路径为setPath("/").即Tomcat的目录下都有效
2、设置Cookie的域setDomain(".itcast.com");即bbs.itcast.com,或是mail.itcast.com有效。即跨域。
3、设置Cookie的时间。即使用户不选择在几天内自动登录,也应该保存Cookie以保存在当前浏览器没有关闭的情况下有效。
4、使用Filter自动登录。
实现步骤
1:首先要准备出几个虚拟主机并配置hosts文件,即本机DNS修改本机的C:\Windows\System32\drivers\etc下的hosts文件。
- <span style="font-size:18px;"><span style="font-size:18px;"># localhost name resolution is handled within DNS itself.
- # 127.0.0.1 localhost
- # ::1 localhost
- 127.0.0.1 localhost
- 127.0.0.1 www.bbs.itcast.cn
- 127.0.0.1 www.news.itcast.cn
- 127.0.0.1 www.news.com
- 127.0.0.1 www.bbs.com
- 127.0.0.1 www.server.com
- </span></span>
增加几个Host节点,通过Cookie实现自动登录,必须配置的虚拟主页满足xxx.itcast.cn,即主域名必须保持一致。
一般web应用中一般部署在web.xml文件中,单点退出相关配置如下:
- <span style="font-size:18px;"><span style="font-size:18px;"><filter>
- <filter-name>CAS Authentication Filter</filter-name>
- <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
- <init-param>
- <!--到哪去登录-->
- <param-name>casServerLoginUrl</param-name>
- <param-value>http://www.server.com:8080/login</param-value>
- </init-param>
- <init-param>
- <!--我是谁-->
- <param-name>serverName</param-name>
- <param-value>http://www.bbs.com:8080</param-value>
- </init-param>
- <init-param>
- <param-name>renew</param-name>
- <param-value>false</param-value>
- </init-param>
- <init-param>
- <param-name>gateway</param-name>
- <param-value>false</param-value>
- </init-param>
- </filter>
- <filter>
- <filter-name>CAS Validation Filter</filter-name>
- <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
- <init-param>
- <param-name>casServerUrlPrefix</param-name>
- <param-value>http://www.server.com:8080</param-value>
- </init-param>
- <init-param>
- <param-name>serverName</param-name>
- <param-value>http://www.bbs.com:8080</param-value>
- </init-param>
- <!--
- <init-param>
- <param-name>proxyCallbackUrl</param-name>
- <param-value>https://localhost:8443/mywebapp/proxyCallback</param-value>
- </init-param>
- <init-param>
- <param-name>proxyReceptorUrl</param-name>
- <param-value>/mywebapp/proxyCallback</param-value>
- </init-param>
- -->
- </filter>
- <filter>
- <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
- <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
- </filter>
- <filter>
- <filter-name>CAS Assertion Thread Local Filter</filter-name>
- <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
- </filter>
- <!-- ************************* -->
- <!-- Sign out not yet implemented -->
- <!--
- <filter-mapping>
- <filter-name>CAS Single Sign Out Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- -->
- <filter-mapping>
- <filter-name>CAS Authentication Filter</filter-name>
- <url-pattern>/protected/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>CAS Validation Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>CAS Assertion Thread Local Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>CAS Validation Filter</filter-name>
- <url-pattern>/proxyCallback</url-pattern>
- </filter-mapping>
- <!-- *********************** -->
- <!-- Sign out not yet implemented -->
- <!--
- <listener>
- <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
- </listener>
- -->
- <!-- *********************** -->
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list></span></span>
说明:我们看到单点退出的相关类结构,web.xml配置了单点退出的相关类(1个监听器SingleSignOutHttpSessionListener,2个过滤器SingleSignOutFilter,SimpleServerLogoutHandler)。
实现利用了session存储机制,SessionStoreManager是个单例类,用于管理session的存储、删除;SessionMappingStorage是session的存储、删除的执行者,可以看到实际存储的结构是一个artifactId、sessionId为名值对的HashMap表;监听器SingleSignOutHttpSessionListener的作用是session销毁时,调用session管理单例类SessionStoreManager进行session的删除销毁;
SingleSignOutFilter的作用有2个:一个是在单点访问拦截安全资源时调用单例类SessionStoreManager存储session,另一个是在单点退出时调用单例类SessionStoreManager删除session;SimpleServerLogoutHandler的作用是将客户端的退出请求转发到SSO服务器端,集中处理做各个子系统的单点退出。
2、先在bbs(或是mail)虚拟目录下,开发一个可以自动登录的程序,使用Filter:
- <span style="font-size:18px;"><span style="font-size:18px;">1、登录的主页如下:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <html>
- <head>
- </head>
- <body>
- <p>在同一台服务器上,多个站点自动登录....>>:<%=session.getId()%></p>
- <c:if test="${empty sessionScope.user}">
- <form name="f" method="post" action="<c:url value='/login'/>">
- Name:<input type="text" name="name"/><br/>
- Pwd:<input type="text" name="pwd"/><br/>
- <input type="checkbox" name="chk" value="7">一周内自动登录<br/>
- <input type="submit" value="登录"/>
- </form>
- </c:if>
- <c:if test="${not empty sessionScope.user}">
- 欢迎你:${user}。<a href="<c:url value='/loginout'/>">安全退出</a>
- </c:if>
- <br/>
- 相关站点:(只要在一边登录成功,即可以自动登录到另一个程序)<br/>
- <a href="http://mail.itcast.com:7777">mail.itcast.com</a><br/>
- <a href="http://bbs.itcast.com:7777">bbs.itcast.com</a><br/>
- </body>
- </html>
- </span></span>
2、登录的Servlet程序如下:
- <span style="font-size:18px;"><span style="font-size:18px;">/**
- * 用户登录
- */
- public class LoginServlet extends HttpServlet{
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doPost(req, resp);
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String nm = req.getParameter("name");
- String pwd = req.getParameter("pwd");
- String chk = req.getParameter("chk"); //是否选中了7天自动登录
- String forward = "/index.jsp";
- if(nm!=null && !nm.trim().equals("") && nm.startsWith("it")//用户名是it开始,且密码是pwd开始的可以登录
- && pwd !=null && !pwd.trim().equals("") &&
- pwd.startsWith("pwd")){
- System.err.println("登录成功。。。。。");
- forward = "/jsps/welcome.jsp";
- //无论如何,都要设置cookie,如果没有选择自动登录,则只在当前页面的跳转时有效,否则设置有效期间为7天。
- Cookie cookie = new Cookie("autologin",nm+"@"+pwd);
- cookie.setPath("/"); //如果路径为/则为整个tomcat目录有用
- cookie.setDomain(".itcast.com"); //设置对所有*.itcast.com为后缀的域名效
- if(chk!=null){
- int time = 1*60*60*24*7; //1秒*60=1分*60分=1小时*24=1天*7=7天
- cookie.setMaxAge(time);
- }
- resp.addCookie(cookie);
- req.getSession().setAttribute("user", nm);
- }else{
- System.err.println("登录不成功。。。。。。");
- }
- req.getRequestDispatcher(forward).forward(req, resp);
- }
- }
- </span></span>
3、自动登录的Filter程序如下:
- <span style="font-size:18px;"><span style="font-size:18px;">/**
- * 自动登录
- */
- public class AutoLogin implements Filter {
- public void destroy() {}
- public void doFilter(ServletRequest req, ServletResponse resp,
- FilterChain chain) throws IOException, ServletException {
- System.err.println("开始自动登录验证.....");//此类中应该对登录的servlet直接放行。根据判断url决定。
- HttpServletRequest requ = (HttpServletRequest) req;
- HttpSession s = requ.getSession();
- if (s.getAttribute("user") != null) {//如果用户已经登录则直接放行
- System.err.println("用户已经登录,没有必须要再做自动登录。。。。");
- } else {
- Cookie[] cookies = requ.getCookies();
- if (cookies != null) {
- for (Cookie ck : cookies) {
- if (ck.getName().equals("autologin")) {// 是否是自动登录。。。。
- System.err.println("自动登录成功。。。。。");
- String val = ck.getValue();
- String[] vals = val.split("@");
- s.setAttribute("user", vals[0]);
- }
- }
- }
- }
- chain.doFilter(req, resp);
- }
- public void init(FilterConfig filterConfig) throws ServletException {}
- }
- </span></span>
4、正常退出的Servlet如下
- <span style="font-size:18px;"><span style="font-size:18px;">/**
- * 安全退出删除Cookie
- */
- public class LoginOutServlet extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- HttpSession s = req.getSession(); //获取Session
- Cookie cookie = new Cookie("autologin","");//必须声明一个完全相同名称的Cookie
- cookie.setPath("/");//路径也要完全相同
- cookie.setDomain(".itcast.com");//域也要完全相同
- cookie.setMaxAge(0);//设置时间为0,以直接删除Cookie
- resp.addCookie(cookie);
- s.removeAttribute("user");
- System.err.println("安全退出。。。。。");
- resp.sendRedirect(req.getContextPath()+"/index.jsp");
- }
- }
- </span></span>
这种是基于最简单的方式实现的单点登录
SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例的更多相关文章
- 基于cookie的用户登录状态管理
cookie是什么 先来花5分钟看完这篇文章:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies 看完上文,相信大家对cookie已经有 ...
- SSO 基于Cookie+fliter实现单点登录(SSO):工作原理
SSO的概念: 单点登录SSO(Single Sign-On)是身份管理中的一部分. SSO的一种较为通俗的定义是:SSO是指訪问同一server不同应用中的受保护资源的同一用户,仅仅须要登录一次,即 ...
- SSO 基于Cookie+fliter实现单点登录 实例解析(一)
接上文,SSO的理论讲解,接下来实践实践! 1.使用Cookie解决单点登录 技术点: 1.设置Cookie的路径为setPath("/").即Tomcat的目录下都有效 2.设置 ...
- Asp.Net Core基于Cookie实现同域单点登录(SSO)
在同一个域名下有很多子系统 如:a.giant.com b.giant.com c.giant.com等 但是这些系统都是giant.com这个子域. 这样的情况就可以在不引用其它框架的情况下, ...
- SSO单点登录学习总结(3)—— 基于CAS实现单点登录实例
第一: 本demo在一个机器上实现(三个虚拟主机),来看SSO单点登录实例(我们可以布到多个机器上使用都是同一个道理的),一个服务器主机,和两个客户端虚拟主机 [html] view plaincop ...
- ASP.NET Core Authentication系列(四)基于Cookie实现多应用间单点登录(SSO)
前言 本系列前三篇文章分别从ASP.NET Core认证的三个重要概念,到如何实现最简单的登录.注销和认证,再到如何配置Cookie 选项,来介绍如何使用ASP.NET Core认证.感兴趣的可以了解 ...
- 基于cookie的SSO单点登录系统
利用COOKIE实现单点登录功能 近期公司要求帮一个项目实现单点登录功能,在综合考量下决定采用cookie实现,大概的流程如下图所:
- 基于Cookie的SSO登录分析和实现
什么是SSO? 现在很多大的互联网公司都会有很多的应用,比如以下是淘宝网的截图: 天猫 聚划算 头条等都是不同的应用,有的甚至采用完全不同的域名,但是所有在淘宝注册的用户都是使用的一套用户名和口令,如 ...
- SSO 基于CAS实现单点登录 实例解析(二)
本文目录: 概述 演示环境 部署CAS-Server相关的Tomcat 部署CAS-Client相关的Tomcat 测试验证SSO 第一: 本demo在一个机器上实现(三个虚拟主机),来看SSO单点登 ...
随机推荐
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) ...
- Mac OSX 平台安装 MongoDB
Mac OSX 平台安装 MongoDB MongoDB 提供了 OSX 平台上 64 位的安装包,你可以在官网下载安装包. 下载地址:https://www.mongodb.com/download ...
- django 笔记2
默默坚持 :路由系统 URL :视图 request.GET request.POST request.FILES #checkbox等多选的内容 request.POST.getlist() #上传 ...
- BZOJ1189: [HNOI2007]紧急疏散evacuate(二分答案,最大流)
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是 ...
- 阿里&163 yum源
站点:https://opsx.alibaba.com/mirror #阿里云base cat /etc/yum.repos.d/alibase.repo [rhel7] name=ali-base ...
- tomcat 分别在window 和 Linux上配置SSL-安全问题
公司项目收尾后.通过压力測试后的安全測试.安全測试后中,对于网络传输中数据加密问题存在安全隐患. 须要配置SSL. 简介下SSL协议:SSL或者Secure Socket Layer,是一种同意web ...
- jquery20--animate() : 运动的方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Excel显示当前日期
https://zhidao.baidu.com/question/431460329693825764.html 直接选中单元格,在公示栏输入=now()
- 实用的 Python 包 —— 使用 win32 的剪贴板
1. usage >> import win32clipboard >> win32clipboard.OpenClipboard() >> win32clipbo ...
- BZOJ 1571 DP
思路: 预处理出在能力值为i的时候 滑雪一次的最小时间 f[i][j]表示i时间 j的能力值 最多的滑雪次数 我先用vector 把课程按起点push进去 1. for(int k=0;k<ve ...
